【问题标题】:VBA Code Returns String as Variable for Range LocationVBA 代码返回字符串作为范围位置的变量
【发布时间】: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


【解决方案1】:

您需要一个快速检查功能来查看命名范围是否存在。如果未定义该名称,您对 Range(LocationRow).Value &lt;&gt; 0 的测试将引发错误。

试试这个

Public Function NameIsDefined(ByVal name As String) As Boolean
    On Error Resume Next
    Dim notUsed As Long
    notUsed = Range(name).Rows.Count

    If Err = 1004 Then
        NameIsDefined = False
        Exit Function
    Else
        NameIsDefined = True
    End If
    On Error GoTo 0
End Function

接下来,您必须检查该范围内的所有单元格的数据(从您的示例中推测,所有单元格的值都必须大于零):

Public Function CellsHaveData(ByRef dataArea as Range) As Boolean
    CellsHaveData = True
    Dim dataCell As Variant
    For Each dataCell In dataArea
        If dataCell.Value = 0 Then
            '--- if only one cell in the range is not zero, then
            '    we don't have data
            CellsHaveData = False
            Exit Function
        End If           
    Next dataCell
End Function

然后你自己的子会看起来像

Private Sub OK_Click()
    Application.ScreenUpdating = False
    Row = ComboBox_Location.Value
    LocationRow = "Location_" & Row
    UPCRow = "UPC_" & Row

    If NameIsDefined(LocationRow) Then
        If Not CellsHaveData(Range(LocationRow)) Then
            Dim answer As VbMsgBoxResult
            answer = MsgBox("This row already has data. Are you sure you want to " & _
                            "clear it and begin with a new UPC?", vbYesNo)
            If answer = vbYes Then
                Range(UPCRow).Value = ComboBox_UPC.Value
                Range(LocationRow).Value = 0
            ElseIf answer = vbNo Then
                'Do Nothing
            End If
        Else
            Range(UPCRow).Value = ComboBox_UPC.Value
            Range(LocationRow).Value = 0
        End If
    Else
        MsgBox "Named Range " & LocationRow & " does not exist.", vbOK
    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 并声明所有变量。
  • 我完全同意。编辑和更新。 (假设ComboBox_Location 和其他是具有定义名称的控件)
  • 嗨 PeterT- 非常感谢您编写此代码!我遇到的唯一问题是它是一个包含多个单元格的范围。我认为这是把它扔掉了,因为即使它们是空白的,它也会给我你确定的信息。
  • 我只是将您对数据的检查替换为检查命名范围是否存在。您需要对范围内的所有单元格执行检查以查看它们是否有数据。示例已更新。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-19
  • 2011-10-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多