Forum Discussion

Debs_au's avatar
Debs_au
Copper Contributor
Aug 24, 2020
Solved

Runtime error 1004: Unable to get the Xlookup property of the worksheetfunction class

I have the current MS Office 365, and am using Excel. I created a button on Sheet1 to click [Change Member Details]   On click, the fields on the form are initialized, with only those to use for t...
  • JKPieterse's avatar
    Aug 24, 2020

    Debs_au If you are doing an XLookup on the ID it will fail because you are getting the value from a textbox and the data in your worksheet contains numerical ID's. This means  XLookup will return #N/A, which results in a run-time error. In addition you must change your declaration of GetMemberData so XLOOKUP can add the found values to it. This appears to work:

     

    Private Sub Btn_Find_Member_Click()
        SetSwitch = True
        Dim GetMemberData As Variant
        Do Until SetSwitch = False
            If I_Mem_Num.Value > 0 Then
                MsgBox I_Mem_Num
                GetMemberData = WorksheetFunction.XLookup(I_Mem_Num * 1, Range("A:A"), Range("A:V")).Value
                ' MsgBox GetMemberData
                SetSwitch = False
                Exit Do
            ElseIf I_Phone > 0 Then
                GetMemberData = WorksheetFunction.XLookup(I_Phone, "H:H", "A:V")
                SetSwitch = False
                Exit Do
            ElseIf IsEmpty(I_Given) = False And IsEmpty(I_Surname) = False And IsEmpty(I_Street) = False Then
                GetMemberData = WorksheetFunction.XLookup(I_Given & I_Surname & I_Street, "F:F" & "G:G" & "J:J", "A:W")
                SetSwitch = False
                Exit Do
            Else
                SetSwitch = True
                MsgBox "There is not enough unique information to search. Please use [Member number] OR [Phone number] OR [Given-Name and Surname and Street]"
            End If
        Loop
        ' >>>>>  Make Text Boxes Visible <<<<<
        I_Suburb.Visible = True
        I_Postcode.Visible = True
        I_Birth.Visible = True
        I_Age.Visible = True
        I_Comment.Visible = True
        I_Email.Visible = True
        I_Mem_Status.Visible = True
        I_Mem_Receipt.Visible = True
        I_Mem_Date_Paid.Visible = True
        I_Joining_Date.Visible = True
        I_Film_Fee.Visible = True
        I_Film_Receipt.Visible = True
        I_Film_Date_Paid.Visible = True
        Mem_Category.Visible = True
        Btn_Gender_Male.Visible = True
        Btn_Gender_Female.Visible = True
        Btn_Gender_Other.Visible = True
        Btn_Vote_Yes.Visible = True
        Btn_Vote_No.Visible = True
        Btn_Photo_Yes.Visible = True
        Btn_Photo_No.Visible = True
        '
        ' >>>>> Put Retrieved Data into Text Boxes <<<<<<
        I_Mem_Num.Value = GetMemberData(1, 1)
        I_Mem_Status.Value = GetMemberData(1, 2)
        I_Mem_Receipt.Value = GetMemberData(1, 3)
        I_Mem_Date_Paid.Value = GetMemberData(1, 4)
        Mem_Category.Value = GetMemberData(1, 5)
        I_Given.Value = GetMemberData(1, 6)
        I_Surname.Value = GetMemberData(1, 7)
        I_Phone.Value = GetMemberData(1, 8)
        I_Email.Value = GetMemberData(1, 9)
        I_Street.Value = GetMemberData(1, 10)
        I_Suburb.Value = GetMemberData(1, 11)
        I_Postcode.Value = GetMemberData(1, 12)
        If GetMemberData(1, 13) = "Male" Then
            Btn_Gender_Male.Value = True
        ElseIf GetMemberData(1, 13) = "Female" Then
            Btn_Gender_Female.Value = True
        Else: Btn_Gender_Other.Value = True
        End If
        I_Birth.Value = GetMemberData(1, 14)
        I_Age.Value = GetMemberData(1, 15)
        I_Joining_Date.Value = GetMemberData(1, 16)
        If GetMemberData(1, 17) = "Yes" Then
            Btn_Vote_Yes.Value = True
        Else: Btn_Vote_No.Value = True
        End If
        If GetMemberData(1, 18) = "Yes" Then
            Btn_Photo_Yes.Value = True
        Else: Btn_Photo_No.Value = True
        End If
        ' Column S (19) >Special Needs = not used
        I_Comment.Value = GetMemberData(1, 20)
        I_Film_Fee.Value = GetMemberData(1, 21)
        I_Film_Receipt.Value = GetMemberData(1, 22)
        I_Film_Date_Paid.Value = GetMemberData(1, 23)
    End Sub

    Except for the last statement as it appears only 22 columns are in the source range of the XLOOKUP.

Resources