VBA Error 91

kjuran

New Member
Joined
Apr 26, 2021
Messages
10
Office Version
  1. 2013
Platform
  1. Windows
I am creating a VBA for the VLOOKUP function. I want to be able to capture a name and grade from Book 2 and have it display on Book1 based on the ID number, however, I keep getting error 91 even though I initialized the sht variable. Here is the code:

Sub vlookup1()

'Initializing workbook connection
Dim PathName As String
Dim FileName As String
Dim wkbk As Workbook
Dim sht As Worksheet
PathName = "K:\Kayla Data Auto-Population\VBA Experiment\"
FileName = "Book 2.xlsx"
Set wkbk = Workbooks.Open(PathName & FileName)
Set sht = wkbk.Worksheets("Sheet1") <- where error is occurring

'Initializing Variables
Set myrange = sht.Range("A:C")
Set ID = Range("A2")
Set Name = Range("B2")
Set Grade = Range("C2")

'Initializing VLOOKUP function
Name.Value = Application.WorksheetFunction.VLookup(ID, myrange, 2, False)
Grade.Value = Application.WorksheetFunction.VLookup(ID, myrange, 3, False)

End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Is the sheet called Sheet 1 in "Book 2.xlsx"?
That type of error suggests it can't find the object with that name

This worked for me:
VBA Code:
Sub Map_Values()

    Const PathName As String = "C:\Users\Win\OneDrive\Desktop\"
    Const FileName As String = "Book 2.xlsx"
    Const SheetName As String = "Sheet 1"
    
    With [A2:C2]
        Workbooks.Open PathName & FileName
        .Offset(, 1).Resize(, 2).Value = ActiveWorkbook.Sheets(SheetName).[A:A].Find(.Cells(1, 1).Value, LookIn:=xlFormulas, lookat:=xlPart).Offset(, 1).Resize(, 2).Value
    End With
    
    ActiveWorkbook.Close False
    
End Sub
 
Upvote 0
Is the sheet called Sheet 1 in "Book 2.xlsx"?
That type of error suggests it can't find the object with that name

This worked for me:
VBA Code:
Sub Map_Values()

    Const PathName As String = "C:\Users\Win\OneDrive\Desktop\"
    Const FileName As String = "Book 2.xlsx"
    Const SheetName As String = "Sheet 1"
   
    With [A2:C2]
        Workbooks.Open PathName & FileName
        .Offset(, 1).Resize(, 2).Value = ActiveWorkbook.Sheets(SheetName).[A:A].Find(.Cells(1, 1).Value, LookIn:=xlFormulas, lookat:=xlPart).Offset(, 1).Resize(, 2).Value
    End With
   
    ActiveWorkbook.Close False
   
End Sub
Yes, it is definitely Sheet1.
Are you saying I use the Const function and have the variable sht be a string
 
Upvote 0
I meant Sheet 1, with a space between 'Sheet' and '1'

Nope, just one VBA suggestion to try, different ways of coding actions/using variables/syntax etc.

Try starting again and record a macro that copies your actions, e.g.
Start with the workbook open that needs the code
File->Open->Book 2.xlsx
Switch back to the first workbook and type in the formula "=VLOOKUP(A2," then select the sheet, then the table in Book 2, add rest of the parameters and hit enter
Stop the recorder and then review the resulting code, it should correctly pick up the variable names and then compare that to your own.
 
Upvote 0
While that is a brilliant idea, my record macros button seems to be grayed out.
 
Upvote 0
While that is a brilliant idea, my record macros button seems to be grayed out.
That often means that you are in the middle of VBA code that you have not run to completion or cancelled (i.e. you are in "break" mode, in the middle of VBA code, or haven't closed out of the error).
 
Upvote 0
Or Helpdesk response: "Have you tried restarting your application/PC?"
 
Upvote 0
I restarted it, which allowed me to record macros but the formatting of the code is wildly different from what I had
 
Upvote 0

Forum statistics

Threads
1,214,925
Messages
6,122,298
Members
449,077
Latest member
Rkmenon

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