Office 360, 64 bit, Excel.
The subroutine, Private Sub Card_FindCardNumber_AfterUpdate() is a textbox (Card_FindCardNumber) that after I have entered in the number I am querying, the code should be looking to see if it exists.
Whilst testing the code, it doesn't matter whether it is a number that exists or doesn't exist, I get the same error message "Runtime Error 91", Object Variable or With Block variable not set.
I'm a amateur with VBA and can't work out why there is a problem with the particular line of code.
I have adapted the code from Trevor Easton,(Online PC Learning) "Excel VBA with Userform Vlookup" from YouTube and his website:
The Ws01 is actually the Code name of the spreadsheet representing cnVisitorCard_Database worksheet.
The Vlookup is referring to the named range: "LookupCardNo" which has an offset function covering data of 9 columns (from column AC (Card #) to the last column of the name range AK)
Private Sub Card_FindCardNumber_AfterUpdate()
'Define the Variables
Dim ws01 As Worksheet
Dim cnVisitorCard_Database As Worksheet
Set ws01 = cnVisitorCard_Database
'Check to see if value exists
If WorksheetFunction.CountIf(ws01.Range("AC:AC"), Me.Card_FindCardNumber.Value) = 0 Then
MsgBox "This is an incorrect ID"
Me.Card_FindCardNumber.Value = ""
Exit Sub
End If
'Lookup values based on first control
With Me
.Card_ExpiryDate = Application.WorksheetFunction.VLookup(CLng(Me.Card_FindCardNumber), ws01.Range("LookupCardNo"), 2, 0)
.Card_Status = Application.WorksheetFunction.VLookup(CLng(Me.Card_FindCardNumber), ws01.Range("LookupCardNo"), 3, 0)
.Card_ReturnDate = Application.WorksheetFunction.VLookup(CLng(Me.Card_FindCardNumber), ws01.Range("LookupCardNo"), 4, 0)
.Card_Description = Application.WorksheetFunction.VLookup(CLng(Me.Card_FindCardNumber), ws01.Range("LookupCardNo"), 5, 0)
.Card_TypeCode_Hidden = Application.WorksheetFunction.VLookup(CLng(Me.Card_FindCardNumber), ws01.Range("LookupCardNo"), 6, 0)
.Card_ValidNo_ofDays_Hidden = Application.WorksheetFunction.VLookup(CLng(Me.Card_FindCardNumber), ws01.Range("LookupCardNo"), 7, 0)
.Card_UpdatedInHW = Application.WorksheetFunction.VLookup(CLng(Me.Card_FindCardNumber), ws01.Range("LookupCardNo"), 8, 0)
.Card_UpdatedInFF = Application.WorksheetFunction.VLookup(CLng(Me.Card_FindCardNumber), ws01.Range("LookupCardNo"), 9, 0)
End With
End Sub
I get the error 91 at the line:
If WorksheetFunction.CountIf(ws01.Range("AC:AC"),
Me.Card_FindCardNumber.Value) = 0 Then
I am of course either expecting it to return the values into the textboxes on the Userform if the number exists, but if it doesn't exist, then I'd expect it to slam me with an pop-up message telling me the number doesn't exist.
I'd be most grateful for any assistance :) of course I'd don't know if there will be other errors once this is cleared up.
Set ws01 = cnVisitorCard_Databaseand check if it's running. Also drop theDim ws01 as Worksheet.ws01is the code name of the spreadsheet, you don't need to set it.