1

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

3
  • 1
    shouldn't it be "=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 be ActiveSheet.Cells(Lrow, "M").Formula = sNotes. Commented Jun 9, 2021 at 10:59
  • You're right, there was a comma missing, and I also needed the parentheses even though there aren't spaces in the sheet name. This is what is working for me, following your suggestions: sNotes = "=VLookup(C" & Lrow & "," & "'" & rWorkSheet & "'" & "!C:N, 12, False)" Btw this lines seems to work without the '.Formula' on the end: ActiveSheet.Cells(Lrow, "M") = sNotes Thanks for your excellent and timely help! Commented Jun 9, 2021 at 11:21
  • I wrote it as an answer so you can mark this question as solved. Commented Jun 9, 2021 at 12:06

1 Answer 1

1

You need an additional comma and the worksheet name needs to be in single quotes.

sNotes = "=VLookup(C" & Lrow & "," & "'" & rWorkSheet & "'" & "!C:N, 12, False)"
ActiveSheet.Cells(Lrow, "M").Formula = sNotes

Also ActiveSheet.Cells(Lrow, "M") defaults to ActiveSheet.Cells(Lrow, "M").Value but to write formulas you should use .Formula. Even if it might work without this way it is more reliable and clear to everyone reading the code.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.