VBA代码返回字符串作为范围位置的变量

时间:2018-07-02 13:09:16

标签: excel-vba combobox userform vba excel

出现用户窗体,用户进行各种选择(其中之一是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

1 个答案:

答案 0 :(得分:0)

您需要的是快速检查功能,以查看指定的范围是否存在。如果未定义该名称,则对Range(LocationRow).Value <> 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