代码之家  ›  专栏  ›  技术社区  ›  mitchmitch24

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

  •  0
  • mitchmitch24  · 技术社区  · 6 年前

    出现一个用户窗体,用户进行各种选择(其中一个是CombobBox_Location),然后将其作为变量输入;例如“C18”

    然后将其与字符串“Location”和“UPC”组合,以创建引用同名命名范围的变量;因此Location_C18和UPC_C18是两个命名范围。

    我想参考范围位置_C18(由许多单元格组成,未合并),看看它们是否都是空的。

    我还想将Combobox_UPC.value设置为命名范围UPC_C18。

    当我运行代码时,我在网上得到一个错误 If Range(LocationRow).Value <> 0 Then ... 我想是因为我写得不对。(正常范围使用引号,即使是命名范围,但由于它是一个变量,我不确定。)

    我试着把这排、那排和那排乌鸦变暗,当作靶场和琴弦,但没有运气。

    感谢您的帮助!

    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 回复  |  直到 6 年前
        1
  •  0
  •   PeterT    6 年前

    您需要的是快速检查函数,以查看命名范围是否存在。你的测试 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