简体   繁体   English

Excel VBA编译错误列表框

[英]Excel VBA Compile error Listbox

I'm nearing completion on a custom excel workbook. 我的自定义Excel工作簿即将完成。 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. 我曾尝试寻找解决方案,但对于VBA还是那么陌生,我什至不知道是什么原因引起的。 It highlights listbox1 but I have listbox1 in the sheet noted. 它突出显示了listbox1,但是我在工作表中有listbox1。

(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 在工作簿打开功能中将其设置为true以后,您可能需要此功能

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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