Find sheet then add vlookup value from mastersheet

Eric Penfold

Active Member
Joined
Nov 19, 2021
Messages
424
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
The VLookup does not work. Should take value from "purchaseanalysis" sheet on to any sheets that match with the column A value. Says range of object global failed?
Look forward to any help


VBA Code:
Sub vLookupWorkbookSheets()

    Dim Src As Workbook
    Dim Des As Workbook
    Dim FileToOpen As Variant
    Dim SName As Variant
    Dim MyArray As Variant
    Dim MyDate As String
    Dim LRow1 As Long
    Dim LRow2 As Long
    Dim ColNum As String
    Dim I As Long
    Dim x As Long
    Dim PurAnalysis As ListObject
    Dim WS As Worksheet
    
    With Application
    .ScreenUpdating = False
    .EnableEvents = False
    End With

    FileToOpen = ("\\DC01\Company\PURCHASING\Forecasting\Brett Martin Template for Vlookup.xlsm")
    Workbooks.Open FileToOpen
    Set Src = Workbooks("Brett Martin Template for Vlookup.xlsm")
    Set Des = Workbooks("Brett Martin Forecast 2022.xlsm")
    Set SASheet = Src.Sheets("Sales Analysis")
    Set PurAnalysis = Des.Sheets("PurchasingAnalysis").ListObjects("Purchasing_Analysis")
    


    LCol = PurAnalysis.Range.Columns.Count

    
       LastMonth = Format(DateSerial(Year(Date), Month(Date) - 1, 1), "mmmm")
            
       MyDate = LastMonth
       ColNum = Month(DateValue("01-" & MyDate & "-1900"))
       ColNum = ColNum + 1

      On Error Resume Next
      
        MyArray = PurAnalysis.DataBodyRange
     
        LRow1 = PurAnalysis.DataBodyRange.Rows.Count
        
        
        For Each WS In Des.Worksheets
        
        LRow2 = WS.Cells(WS.Rows.Count, 4).End(xlUp).Row + 1
        
        For x = LBound(MyArray) To UBound(MyArray)
        SName = MyArray(x, 1)
        
        If SName = WS Then
        
        WS("D" & LRow2).Value = WorksheetFunction.VLookup(Range(2, ColNum).Value, Range("Purchasing_Analysis"), 4, 0)
        
        End If
        
        Next x
        Next WS
        
        Workbooks("Brett Martin Template for Vlookup.xlsm").Close _
        SaveChanges:=False

         With Application
        
        .ScreenUpdating = True
        .EnableEvents = True
        End With
    
    End Sub
 

Excel Facts

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
Sorry forgot to loop the ws this is revised code

VBA Code:
Sub vLookupWorkbookSheets()

    Dim Src As Workbook
    Dim Des As Workbook
    Dim FileToOpen As Variant
    Dim SName As Variant
    Dim MyArray As Variant
    Dim MyDate As String
    Dim LRow1 As Long
    Dim LRow2 As Long
    Dim i As Long
    Dim x As Long
    Dim PurAnalysis As ListObject
    Dim WS As Worksheet
    
    With Application
    .ScreenUpdating = False
    .EnableEvents = False
    End With

    FileToOpen = ("\\DC01\Company\PURCHASING\Forecasting\Brett Martin Template for Vlookup.xlsm")
    Workbooks.Open FileToOpen
    Set Src = Workbooks("Brett Martin Template for Vlookup.xlsm")
    Set Des = Workbooks("Brett Martin Forecast 2022.xlsm")
    Set PurSheet = Des.Sheets("PurchasingAnalysis")
    Set PurAnalysis = Des.Sheets("PurchasingAnalysis").ListObjects("Purchasing_Analysis")
    
       LastMonth = Format(DateSerial(Year(Date), Month(Date) - 1, 1), "mmmm")
            
       MyDate = LastMonth
       ColNum = Month(DateValue("01-" & MyDate & "-1900"))
       ColNum = ColNum + 1

      On Error Resume Next
      
        MyArray = PurAnalysis.DataBodyRange
     
        LRow1 = PurAnalysis.DataBodyRange.Rows.Count
        
        
        For Each WS In Des.Worksheets
        
        LRow2 = WS.Cells(WS.Rows.Count, 4).End(xlUp).Row + 1
        
        For x = LBound(MyArray) To UBound(MyArray)
        SName = MyArray(x, 1)
        
        

        For i = 2 To LRow1
        
        If SName = WS Then
        
        WS("D" & LRow2).Value = WorksheetFunction.VLookup(Range(i, ColNum).Value, Range("Purchasing_Analysis"), 4, 0)
        
        End If
        
        Next i
        Next x
        Next WS

        
        Workbooks("Brett Martin Template for Vlookup.xlsm").Close _
        SaveChanges:=False

         With Application
        
        .ScreenUpdating = True
        .EnableEvents = True
        End With
    
    End Sub
 
Upvote 0

Forum statistics

Threads
1,214,982
Messages
6,122,573
Members
449,089
Latest member
Motoracer88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top