my VB script obtains the sheet name of the most recent sheet in the workbook, storing the sheet name as a string, however when I reference this string in a Vlookup function, Excel cannot locate the sheet. The sheet name is in date format, so the most recent sheet is yesterday's date (in Australian date format), '08-06-2021'. The macro runs, but when it comes to running the VLookup function, it cannot find the sheet and interestingly the following message appears:
Cannot find "2021". Copy from:
And opens a window to locate the Spreadsheet, and then select the Worksheet within the spreadsheet. For fun I renamed the '08-06-2021' sheet to just '2021' to see if that would work. This also didn't work, and the error was even more unusual, as it is now trying to locate a sheet name that doesn't exist. The new message was:
Cannot find "C1322021". Copy from:
As mentioned, there is no sheet with the name C1322021. Here is the relevant VB code:
Dim wsData1 As Worksheet
Dim Firstrow As Long
Dim Lastrow As Long
Dim Lrow As Long
Dim rWorkSheet As String
Dim i
i = ActiveWorkbook.Sheets.Count - 1
rWorkSheet = Worksheets(i).Name
Firstrow = .UsedRange.Cells(1).Row + 1
Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
For Lrow = Lastrow To Firstrow Step -1
With ActiveSheet.Cells(Lrow, "M")
sNotes = "=VLookup(C" & Lrow & rWorkSheet & "!C:N, 12, False)"
ActiveSheet.Cells(Lrow, "M") = sNotes
End With
Appreciate any assistance with this, as it has me stumped!
TIA,
Michael
"=VLookup(C" & Lrow, rWorkSheet & "!C:N, 12, False)"? and if your worksheet contains a space it should be"=VLookup(C" & Lrow, "'" & rWorkSheet & "'!C:N, 12, False)"Also it should beActiveSheet.Cells(Lrow, "M").Formula = sNotes.