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

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Try this
Change
VBA Code:
WS("D" & LRow2).Value = WorksheetFunction.VLookup(Range(2, ColNum).Value, Range("Purchasing_Analysis"), 4, 0)
as
VBA Code:
 WS("D" & LRow2).Value = WorksheetFunction.VLookup(Cells(2, ColNum).Value, Range("Purchasing_Analysis"), 4, 0)
 
Upvote 0
Thanks your right but i am trying to loop through all the sheets and using the vlookup to add data from the master sheet. I can`t get the loop to work for the worksheets??
Any help would be much appreciated.
 
Upvote 0

Forum statistics

Threads
1,214,622
Messages
6,120,585
Members
448,972
Latest member
Shantanu2024

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