【发布时间】:2018-07-02 13:09:16
【问题描述】:
出现一个用户表单,用户做出各种选择(其中之一是 CombobBox_Location),然后将其作为变量输入;例如“C18”
然后将其与字符串“Location_”和“UPC_”组合以创建引用同名命名范围的变量;所以 Location_C18 和 UPC_C18 是两个命名范围。
我想引用范围Location_C18(由很多单元格组成,未合并),看看它们是否都是空的。
我还想将 Combobox_UPC.value 设置为命名范围 UPC_C18。
当我运行代码时,我在If Range(LocationRow).Value <> 0 Then 行出现错误...我认为这是因为我没有正确编写它。 (正常范围使用引号,即使对于命名范围也是如此,但由于它是一个变量,我不确定。)
我尝试将 Row、LocationRow 和 UPCRow 作为范围和字符串进行调暗,但没有成功。
感谢任何帮助!
Private Sub OK_Click()
Application.ScreenUpdating = False
Row = ComboBox_Location.Value
LocationRow = "Location_" & Row
UPCRow = "UPC_" & Row
If Range(LocationRow).Value <> 0 Then
If MsgBox("This row already has data. Are you sure you want to clear it and begin with a new UPC?", vbYesNo) = vbYes Then
Range(UPCRow).Value = ComboBox_UPC.Value
Range(LocationRow).Value = 0
ElseIf MsgBox("This row already has data. Are you sure you want to clear it and begin with a new UPC?", vbYesNo) = vbNo Then
'Do Nothing
End If
Else
Range(UPCRow).Value = ComboBox_UPC.Value
Range(LocationRow).Value = 0
End If
ComboBox_Location.Clear
ComboBox_UPC.Clear
Corresponding_Material.Value = ""
Corresponding_Material_Description.Value = ""
Change_Material.Hide
Application.ScreenUpdating = True
End Sub
【问题讨论】:
-
哪个错误在哪一行? • 请使用
Option Explicit并声明所有变量,然后更新问题中的代码并告诉您在哪一行得到哪条错误消息。 -
从 Option Explicit 开始并定义您的每个术语...行,在这种情况下,可以是文本
-
它们应该被声明为
String类型,因为ComboBox不能返回Range对象。那么你需要确保combobox和你的VBA指定的范围确实存在,否则会报错。
标签: excel combobox userform vba