Trying to use a month name in my VLookup

Eric Penfold

Active Member
Joined
Nov 19, 2021
Messages
431
Office Version
  1. 365
Platform
  1. Windows
  2. Mobile
Trying to use a month name to vlookup but the error code 1004 comes up. What am i doing wrong with the vlookup function?

VBA Code:
Option Explicit

Sub Target_VLookup()

    Dim SourceWb As Workbook, DestWb As Workbook
    Dim ws     As Worksheet, wsDMU As Worksheet, wsSO As Worksheet
    Dim SourceLRow As Long, LRow  As Long, Row As Long
    Dim FileToOpen As Variant
    Dim MyDate As String, MyMonth As Date
    Dim FirstDayOfMonth As Date, CurrentDate As Date
    Dim r      As Integer
    Dim SourceRng As Range, DesRng As Range

    With Application
        .EnableEvents = False
        .ScreenUpdating = False
        .DisplayAlerts = False
        .Calculation = xlCalculationManual
    End With
   
    Set DestWb = Workbooks("DailyMail.xlsx")
    MyDate = Format(Date, "mMmm")
    Set wsDMU = DestWb.Worksheets("Daily Mail Update")
    Set DesRng = wsDMU.Range("A2:D2")
   
    FileToOpen = ("\\DF-AZ-FILE01\Company\SALES\REPORTING\2024\Sales Report 24.xlsx")
    Workbooks.Open FileToOpen
   
    Set SourceWb = Workbooks("Sales Report 24.xlsx")
    Set wsSO = SourceWb.Worksheets("Sales Order")
    LRow = wsSO.Cells(Rows.Count, 2).End(xlUp).Row
    Set SourceRng = wsSO.Range("B2:M" & LRow)
   
    DestWb.Activate
    wsSO.Activate
   
    With wsDMU
        .Cells(2, 4).Value = WorksheetFunction.VLookup(MyDate, _
                   SourceRng, 13, False)
    End With
   
    SourceWb.Close
   
    With Application
       
        .EnableEvents = True
        .ScreenUpdating = True
        .DisplayAlerts = True
        .Calculation = xlCalculationAutomatic
    End With
   
    MsgBox "VLookup Month Target Update"
   
End Sub
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Columns B to M in SourceRng, that's 12 columns, you're referring to the 13th column in your VLOOKUP
 
Upvote 0
Sorry yes that is that issue.
But now this
VBA Code:
 .Cells(2, 4).Value
says "Expression not defined in context"
 
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,152
Members
453,021
Latest member
Justyna P

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