+ Reply to Thread
Results 1 to 4 of 4

searching a range within a range

Hybrid View

  1. #1
    Registered User
    Join Date
    02-01-2009
    Location
    Winnipeg, Canada
    MS-Off Ver
    Excel 2003
    Posts
    9

    searching a range within a range

    Hi. Me = macro neophite, mostly just record macros and tweak as need be. But need help with this one.

    Trying to enter pay rates into a data sheet when an employee works in a specified department.

    My logic may be off, but what I want to do is this:

    for each cell in A:A
    the cell = employee#
    if (the cell in column D is not null AND the cell in column e is blank) then the cell in column D is the department

    go to sheet to and for each cell in range A:A of sheet 2
    if the active cell = employee and the corresponding cell in column c = dept, then copy the corresponding cell in column D of Sheet 2 and paste it into column E of the first sheet.

    I attached a sample of the data. The actual sheets are hundreds of pages long and will take me a couple days to go through without a macro - so if anyone is in a generous mood, I'd appreciate the help.

    Thanks.

    I know it is really wrong and its embarraising for me to post it, but this where I got so far and decided that I needed help

    Sub Macro2()
    '
    ' Macro2 Macro
    Dim ee
    Dim dept
    Dim x As Range
    Dim y As Range
    
    On Error Resume Next
    
    For Each x In Range("A:A")
    
    Set ee = ActiveCell.Value
    
    If Selection.Offset(0, 3) Is Not Null And Selection.Offset(0, 4) = "" Then Set dept = Selection.Offset(0, 3).Value
        Sheets("Sheet2").Select
        For Each y In Range("A:A")
            If y = ee And Selection.Offset(0, 2) = dept Then Selection.Offset(0, 3).Copy
            Sheets("Sheet1").Select
            Selection.Offset(0, 3).PasteSpecial
        Next
    Next
    End Sub
    Attached Files Attached Files
    Last edited by mr.alexander; 11-29-2010 at 12:29 PM.

  2. #2
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: searching a range within a range

    Hi

    How about building your own function.

    Using your example file, open a general module and enter the code
    Function myfunc(id, dept, ee)
      holder = 0
      Set findit = ee.Find(what:=id)
      If Not findit Is Nothing Then
        firstadd = findit.Address
        Do
          If findit.Offset(0, 2).Value = dept Then
            holder = findit.Offset(0, 3).Value
          End If
          Set findit = ee.Find(what:=id, LookIn:=xlValues, after:=findit)
        Loop Until findit.Address = firstadd
      End If
      
      myfunc = holder
    End Function
    Then in sheet1!E2 enter:
    =myfunc(A2,D2,Sheet2!$A$2:$A$46)
    Copy down as required.

    HTH

    rylo

  3. #3
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,523

    Re: searching a range within a range

    I am not exactly sure what you are doing
    This would work in E2 and dragged down
    =SUMPRODUCT(--(Sheet2!$A$2:$A$46=A2),--(Sheet2!$C$2:$C$46=D2), --(Sheet2!$D$2:$D$46))
    I do not see what determines true or false

  4. #4
    Registered User
    Join Date
    02-01-2009
    Location
    Winnipeg, Canada
    MS-Off Ver
    Excel 2003
    Posts
    9

    Re: searching a range within a range

    I tried the function but have no experience with them and couldn't get it to work. Then I tried davesexcel formula and it gave me errors, but it was a good start and helped me think better, especially trying to answer the true or false question, it helped me to realize that it wasn't relevant. I settled on using this formula instead of a macro or function;

    =SUMIFS(Sheet2!D:D,Sheet2!A:A,Sheet1!A5,Sheet2!C:C,D5)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1