Consulting

Results 1 to 4 of 4

Thread: Vlookup in Different Workbook - Sheet Looping

  1. #1

    Vlookup in Different Workbook - Sheet Looping

    Hi,

    I have two workbooks with 60+ Tabs with the same names. I am trying to get data from the one Workbook to the other Workbook using vlookup. It needs to loop through the sheets of both workbooks to know which data needs to be added to which sheet.

    Currently, my code is this:

    Sub Vlookup_Jan23()
    ' Vlookup_Jan23 Macro
    Dim i As Long
    Dim shtCount As Long
    shtCount = Sheets.Count
    For i = 1 To shtCount
       If Sheets(i).Name <> "Workbook Contents" Then
          If Sheets(i).Name <> "Portfolio" Then
             Application.DisplayAlerts = False
             Sheets(i).Select
             Range("CS12").Select
             ActiveCell.FormulaR1C1 = "'Jan 23"
             Range("CS15").Select
             Range("CS15").Select
             ActiveCell.FormulaR1C1 = "=IFNA(VLOOKUP(""Monthly Offtaker Revenue"",'[Report - 31 Jan 2023.xlsx]170'!R11C2:R49C17,14,FALSE),0)"
             Range("CS19").Select
             ActiveCell.FormulaR1C1 = "=IFNA(VLOOKUP(""Cell Owner Distributions"",'[Report - 31 Jan 2023.xlsx]170'!R11C2:R49C17,14,FALSE),0)"
             Range("CR17").Select
          End If
       End If
      Next i
    End Sub
    It works fine, but the Green highlighted Sheet name needs to preferably also be sheet(i)

    How can this be corrected.

    Thanks

    Chacha
    Last edited by Aussiebear; 02-10-2023 at 02:29 AM. Reason: Added code tags to supplied code

  2. #2
    try:

    ...
    ...
             ActiveCell.FormulaR1C1 = "=IFNA(VLOOKUP(""Monthly Offtaker Revenue"",'[Report - 31 Jan 2023.xlsx]" & Sheets(i).Name & "'!R11C2:R49C17,14,FALSE),0)"
             Range("CS19").Select
             ActiveCell.FormulaR1C1 = "=IFNA(VLOOKUP(""Cell Owner Distributions"",'[Report - 31 Jan 2023.xlsx]" & Sheets(i).Name & "'!R11C2:R49C17,14,FALSE),0)"
    ...
    ...

  3. #3
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,058
    Location
    One assumes here that you only have three sheets per workbook, if so then why not name the actual sheet?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  4. #4
    Amazing! Thanks so much!

Tags for this Thread

Posting Permissions

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