Consulting

Page 1 of 2 1 2 LastLast
Results 1 to 20 of 23

Thread: Solved: Cut and Paste using Offset and Vlookup

  1. #1

    Solved: Cut and Paste using Offset and Vlookup

    Hi Guys,

    I have a Workbook that details Customers and the Newspapers they receive for each day of the week. The Customers Sheet has a Matrix of Name, Address, Monday, Tuesday etc across the columns, and for each Row The Name, and a paper code for each day of the week ie. Tel for Telegraph etc.

    On the Data Sheet I have a lookup Table detailing paper code against paper.

    What I am trying to do is this:-

    For each Day say Monday, create a list of Names and the Papers that they receive and paste this onto a Sheet called Monday.

    I have managed to crib together the following code, and have suceeded in finding what papers are required, but can only paste the codes I find onto the Monday Sheet, not the corresponding names.

    I would appreciate any help.


    SD


    [VBA]

    Option Explicit

    Private Sub cmdMonday_Click()
    Dim ws1 As Worksheet
    Set ws1 = Sheets("Customers")
    ws1.Activate
    'Call the macro with Monday range.
    'This will change for each Day
    With ws1
    Call SelectPaper(.Range(.Cells(5, 5), .Cells(.Rows.Count, 5).End(xlUp)))
    End With
    End Sub


    'Called from the cmdMonday_Click() Routine on the 'Orders' Sheet
    Sub SelectPaper(rngSearch As Range)
    Dim wb As Workbook, ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet
    Dim rngMonday As Range, Cell As Object

    Application.EnableEvents = False
    On Error GoTo ErrUm

    Set wb = ActiveWorkbook
    Set ws1 = Sheets("Customers")
    Set ws2 = Sheets("Monday")
    Set ws3 = Sheets("Data")

    'Check every cell in the range to see who gets a paper.
    For Each Cell In rngSearch
    If Cell.Value <> "\" Then
    If rngMonday Is Nothing Then
    Set rngMonday = Range(Cell.Address)
    Else
    Set rngMonday = Union(rngMonday, Range(Cell.Address))
    End If
    End If
    Next
    'Select the new range of only those who get a paper
    rngMonday.Select
    'Copy Selection to Monday Sheet
    Selection.Copy ws2.Range("B1")
    ws2.Activate

    Application.EnableEvents = True
    Exit Sub

    ErrUm:
    MsgBox "Doh! Another Error!" & vbNewLine & Err.Description
    Err.Clear
    Application.EnableEvents = True

    End Sub

    [/VBA]

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Can you post your workbook, it is tricky envisaging the data.

  3. #3
    No Problem!

    Yer Tiz.


    SD

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Private Sub cmdMonday_Click()
    Dim aryDays
    Dim i As Long
    Dim rng As Range
    Dim iCol As Long

    Application.EnableEvents = False
    On Error GoTo ErrUm

    aryDays = Array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")
    Dim ws1 As Worksheet
    Set ws1 = Sheets("Customers")
    For i = LBound(aryDays) To UBound(aryDays)
    iCol = i - LBound(aryDays) + 5
    With ws1
    Set rng = .Range(.Cells(5, iCol), .Cells(.Rows.Count, iCol).End(xlUp))
    End With
    Call SelectPaper(rng, Worksheets(aryDays(i)))
    Next i

    Exit Sub

    ErrUm:
    MsgBox "Doh! Another Error!" & vbNewLine & Err.Description
    Err.Clear
    Application.EnableEvents = True
    End Sub
    'Called from the cmdMonday_Click() Routine on the 'Orders' Sheet
    Sub SelectPaper(rngSearch As Range, sh As Worksheet)
    Dim wb As Workbook, ws1 As Worksheet, ws2 As Worksheet, ws3 As Worksheet
    Dim rng As Range, Cell As Range
    Set wb = ActiveWorkbook
    Set ws1 = Sheets("Customers")
    Set ws3 = Sheets("Data")

    'Check every cell in the range to see who gets a paper.
    For Each Cell In rngSearch
    If Cell.Value <> "\" Then
    If rng Is Nothing Then
    Set rng = Cell
    Else
    Set rng = Union(rng, Cell)
    End If
    End If
    Next
    'Select the new range of only those who get a paper
    rng.Copy sh.Range("B1")
    Application.EnableEvents = True
    Exit Sub

    End Sub
    [/vba]

  5. #5
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Can you attach your workbook?, this can probably be solved with a worksheet function copied and pasted in to the sheets named days of the week so when ever the master sheet is changed then so is the week day sheet. You could of course use an Indirect VlookUp for the name of the person if they appear for say a Monday....or something along those lines!

    Regards,
    Simon
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  6. #6
    Hi Simon,

    See earlier post for Workbook.

    SD

  7. #7
    Hi XLD,

    Copied Code to a new Module and ran it, but I get a subscript out of range error. The Monday paper codes still get copied to the Monday Sheet, but no Names or Papers from the Data Sheet.

    SD

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Nor did your code do that, I just utilised your code and extended it for more sheets.

    Ti get past the subscript error, you have to add tabs for Tuesday, Wednesday etc.

  9. #9
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    I Can't believe it...........i've been dissapointed by the Great "El Xid"!, i was kinda hoping that the solution would have been a single sheet with formula in, perhaps a dropdown in A1 to select the day and on selecting the day the relevant data appearing at the formula cells, i have been fiddling around with limited sucess with Match and Offset, as
    =HLOOKUP(A1,INDIRECT(C1),5,FALSE)
    this kind of formula didnt seem to work too well....i am trying to concoct something and still will.

    Still trying to be imaginative but limited ability!

    Regards,
    Simon
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  10. #10
    Thanks Guys,

    Really appreciate it.

    SD

  11. #11
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Simon,

    I think it is a bit mor complex than that.

    The list on Monday would be constructed with this array formula

    =IF(ISERROR(SMALL(IF((Customers!$E$5:$E$50<>"\")*(Customers!$E$5:$E$50<>"") ,ROW($E$5:$E$50)-MIN(ROW($E$5:$E$50))+1,""),ROW($A1))),"",
    INDEX(Customers!$E$5:$E$50,SMALL(IF((Customers!$E$5:$E$50<>"\")*(Customers! $E$5:$E$50<>""),ROW($E$5:$E$50)-MIN(ROW($E$5:$E$50))+1,""),ROW($A1))))
    and the name is

    =IF(B1="","",VLOOKUP(B1,Data!B458,2,FALSE))

  12. #12
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Simon,

    I think it is a bit mor complex than that.

    The list on Monday would be constructed with this array formula

    =IF(ISERROR(SMALL(IF((Customers!$E$5:$E$50<>"\")*(Customers!$E$5:$E$50<>"") ,ROW($E$5:$E$50)-MIN(ROW($E$5:$E$50))+1,""),ROW($A1))),"",
    INDEX(Customers!$E$5:$E$50,SMALL(IF((Customers!$E$5:$E$50<>"\")*(Customers! $E$5:$E$50<>""),ROW($E$5:$E$50)-MIN(ROW($E$5:$E$50))+1,""),ROW($A1))))
    and the name is

    =IF(B1="","",VLOOKUP(B1,Data!B458,2,FALSE))

  13. #13
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    I was fiddling with a multiple result array formula
    =IF(ROWS(A1:A1)<=COUNTIF(Customers!A2:N2,$A$1),INDEX(Customers!E2:N2,SMALL( IF(Customers!A2:N2=$A$1,ROW(Customers!A2:N2)-ROW(A2)+1),ROW(Customers!A1))),"")
    however this only produces the results for the text typed in A1 so if i typed 6 then (as long as i had copied the formula down) this would produce 3 lots of Zebedee, i am trying to adapt it but hitting brick walls!

    Thanks Bob made me smile when you replied!, wasn't being horrible just following some advice a friend gave me in a PM once!

    Regards,
    Simon
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  14. #14
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by smugdruggler
    Thanks Guys,

    Really appreciate it.

    SD
    Does that mean you are sorted?

  15. #15
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    I have been thinking again about Simon's musings, and as usual he is right.

    Attached is a workbook, where you can select the day from the dropdown in C1, and the Daily sheet will reflect that day's papers.

    BTW, I see the Salisbury Journal in your list. Are you in that area? That is just up the road from me, I am in Poole.

  16. #16
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Oops, didn't make a formula absolute.

  17. #17
    Yeah I'm just north of Shaftesbury small world!

    That is really clever by the way, thanks a huge amount El Xid, would not have got there without your help.

    SD

  18. #18
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Not alway right Bob - Just sometimes imaginative!!!! Lol....i knew that the index formula wasn't excatly the way to go but thought match was but couldn't get close enough with a solution myself to try and make it work in the application _ My faith is now restored!!

    Regards,
    Simon
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  19. #19
    Moderator VBAX Guru Simon Lloyd's Avatar
    Joined
    Sep 2005
    Location
    UK
    Posts
    3,003
    Location
    Bob, looking at the formulae you devised i can see elements of what i was trying to do with the array formula - naturally yours is far beyond me so a little explanation if you would?

    Quote Originally Posted by Excerpt!
    Customers!$E$3:$K$3,0)-1,46,1)<>"\")*(OFFSET(Customers!$E$5,0
    why the "*"?, i couldn't see the relationship in multiplying the ranges or is it used as a wild card? similarly with this next excerpt the "-MIN"
    Quote Originally Posted by Excerpt
    ROW(Customers!$E$5:$E$50)-MIN(ROW(Customers!$E$5:$E$50))+1,""),
    with the rest of the formula i can work out what its looking at where and why but i wasnt entirely sure with this combination
    Quote Originally Posted by Excerpt
    IF(ISERROR(SMALL(IF((OFFSET
    , however your formulae work very neatly!

    Regards,
    Simon
    Regards,
    Simon
    Please read this before cross posting!
    In the unlikely event you didn't get your answer here try Microsoft Office Discussion @ The Code Cage
    If I have seen further it is by standing on the shoulders of giants.
    Isaac Newton, Letter to Robert Hooke, February 5, 1675 English mathematician & physicist (1642 - 1727)

  20. #20
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Quote Originally Posted by Simon Lloyd
    Bob, looking at the formulae you devised i can see elements of what i was trying to do with the array formula - naturally yours is far beyond me so a little explanation if you would?

    why the "*"?, i couldn't see the relationship in multiplying the ranges or is it used as a wild card?
    That is effectively an AND Simon. I want to make two tests, that the cell is not a \ and it is not blank. If both conditions are met it will give me a 1, if either is not met, it will give me a 0. As you know, any non-zero value in Excel is a TRUE, so the true action of the IF formula is taken when it equals 1.

    Quote Originally Posted by Simon Lloyd
    similarly with this next excerpt the "-MIN"
    The MIN is just to handle the fact that the data might start in row 1, row 2, or row 5, etc. ROW(E5:E50) gives me an array of 5,.6,7,..., MIN(ROW(E5:E50)) gives me 5 every time, ROW(E5:E50)-ROW(ROW(E5:E50))+1 gives me an array of 1,2,3,..., which I can pass to SMALL to get the values in order. I could have just used ROW(A1:A46), but that is neither intuitive (what does A1:A46 have to do with E5:E50?), and would fail if a row were inserted before 1. Totally dynamic.

    Quote Originally Posted by Simon Lloyd
    with the rest of the formula i can work out what its looking at where and why but i wasnt entirely sure with this combination , however your formulae work very neatly!
    The OFFSET is used because I need to test different columns based upon the day, so I offset E5 based upon the day number -1 (Monday is OFFSET 0), and use the number of rows to size the array to test. I was a bit lazy here, I should have used INDEX, but OFFSET is easier, so I didn't spend the time on INDEX.

    In essence, all my formula does is to test build an array of row indexes for each row that meets the test criteria (not \ and not blank) for the chosen day, these are then passed one at a time to the INDEX function to return the customer and paper id.


    I think your formulae were trying to follow the same principles, you just don't have the skills (YET!) to follow it through.

    Bob

Posting Permissions

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