简体   繁体   中英

Excel VBA Compile error Listbox

I'm nearing completion on a custom excel workbook. I'm having a irritating issue where the below code works but gives a compile error on load. I've tried searching for the solution but being so new to VBA I'm not even sure what could be causing the issue. It highlights listbox1 but I have listbox1 in the sheet noted.

(Compile Error "Method or Data member not found")

Private Sub ListBox1_Click()
Sheet2.TextBox1.Value = " "
Dim i As Long
i = Sheet2.ListBox1.ListIndex
If i < -1 Then Exit Sub
Sheet2.TextBox1.Value = Sheet1.Range("C" & (i + 4))
End Sub

Thank You

It is probably due to the loading of the values into the list. Try something like this.

Create a global boolean variable

Private bDoneLoading as Boolean

Set it to true in the workbook open function after anything else you may have i this function

Private Sub Workbook_Open()

    'Any other code

    bDoneLoading = True
End Sub

Add a check to make sure the workbook has loaded.

Private Sub ListBox1_Click()

    If bDoneLoading = false Then
        Exit sub
    End If

    Sheet2.TextBox1.Value = " "
    Dim i As Long
    i = Sheet2.ListBox1.ListIndex
    If i < -1 Then Exit Sub
    Sheet2.TextBox1.Value = Sheet1.Range("C" & (i + 4))
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM