Eric Penfold
Active Member
- Joined
- Nov 19, 2021
- Messages
- 431
- Office Version
- 365
- Platform
- Windows
- 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