【问题标题】:Creating Multiple Data Validation Lists Without Referring to Same Range EXCEL VBA创建多个数据验证列表而不引用相同范围的 EXCEL VBA
【发布时间】:2011-07-28 19:11:51
【问题描述】:

我正在用 Excel VBA 编写一个宏,它在指定的单元格中创建一个数据验证列表。然后程序提示用户输入包含数据验证列表内容的单元格。然后将从视图中隐藏包含列表内容的相同行。但是,当我尝试多次重新运行宏时,每次为内容选择一个新范围时,每个进程列表都会引用该范围。 我不希望这种情况发生。

我写了这行代码来防止这种情况:

For Each nm In ThisWorkbook.Names
    strRngNumLbl = strRngNmLbl + 1
Next nm
strRange = strRange & strRngNumLbl

其中 strRng 是添加到数据验证时要引用的范围的名称。但是,由于某种原因,这不起作用。我认为这会起作用,因为它会为要添加到列表中的每个范围创建独立的名称。但它没有......

这是完整的代码:

Sub CreatDropDownList()
Dim strRange As String
Dim celNm As Range
Dim celNm2 As Range 'use only if necessary
Dim celRng As Range
Dim strRngNumLbl As Integer
Dim nm As Name


On Error GoTo pressedCancel:

Set celNm = Application.InputBox(Prompt:= _
                "Please select a cell to create a list.", _
                   Title:="SPECIFY Cell", Type:=8)

If celNm Is Nothing Then Exit Sub

'Inserts a copy of the row where the drop down list is going to be
celNm.EntireRow.Copy
ActiveCell.Offset(1).EntireRow.Insert '?


'moves the cell to the appropriate location
celNm.Offset(0, -1).Value = "N/A"

'cell range equal to nothing
Set celRng = Nothing

'asks user to determine range of strings
Set celRng = Application.InputBox(Prompt:= _
    "Please select the range of cells to be included in list.", _
        Title:="SPECIFY RANGE", Type:=8)

If celRng Is Nothing Then Exit Sub
On Error GoTo 0

strRange = "DataRange"
strRngNumLbl = 1

'Increments strRngNumLblb for the number of names present in the workbook to
'ensure list is not referring to duplicate ranges
For Each nm In ThisWorkbook.Names
    strRngNumLbl = strRngNmLbl + 1
Next nm
strRange = strRange & strRngNumLbl

'user defined data range is now called strRange, refer to it as Range(strRange)
ThisWorkbook.Names.Add Name:=strRange, RefersTo:=celRng

'format the refernce name for use in Validation.add
strRange = "=" & strRange

celNm.Offset(-1, 0).Select

'Add the drop down list to the target range using the list range
celNm.Validation.Delete
celNm.Validation.Add xlValidateList, , , strRange

'hide the range where the list came from
celRng.EntireRow.Hidden = True

pressedCancel:
End Sub

有什么建议吗?

【问题讨论】:

    标签: excel list vba drop-down-menu named-ranges


    【解决方案1】:

    解决您的问题

    代替:

    For Each nm In ThisWorkbook.Names
        strRngNumLbl = strRngNmLbl + 1
    Next nm
    

    你应该有:

    strRngNumLbl = ThisWorkbook.Names.Count + 1
    

    关于您的代码的一些提示或问题

    我不明白这部分代码有什么用:

    'Inserts a copy of the row where the drop down list is going to be
    celNm.EntireRow.Copy
    ActiveCell.Offset(1).EntireRow.Insert '?
    
    'moves the cell to the appropriate location
    celNm.Offset(0, -1).Value = "N/A"
    

    这部分我也不明白。此外,如果用户选择 A 列中的单元格,这可能会导致错误

    celNm.Offset(0, -1).Value = "N/A"
    

    希望对你有帮助,

    【讨论】:

    • 有一个罕见的情况(当我测试代码时)我在工作簿中存储了三个名称。在不断迭代测试旧代码后,引用名称中的数字会增加,但每次都会替换旧引用。碰巧的是,当我认为我修复了代码时,我之前在“strRange4”处停了下来,而工作簿中还有另外两个命名引用。因此,程序不断替换相同的引用。
    • 此外,用户永远不会像设置工作表的方式那样选择“a”列中的单元格。这部分代码是复制我选择的行,因为它将有一个度量类别,该类别将包含在列表中,但我不想显示。因此,复制这一行是有利的,只需在副本中将此单元格更改为“N/A”即可。请注意,单元格将始终位于我选择的左侧。
    【解决方案2】:

    我可以通过检查 strRange 名称是否已经在 ThisWorkbook.names 中来解决这个问题。这是对上面代码的修改:

    For Each nm In ThisWorkbook.Names
        strRngNumLbl = strRngNumLbl + 1
        strRange = strRange & strRngNumLbl
        If strRange = nm Then
            strRngNumLbl = strRngNumLbl + 1
            strRange = strRange & strRngNumLbl
        End If
    Next nm
    

    【讨论】:

    • 很高兴您找到了问题的答案。您可以接受自己的答案(选中答案左侧的勾号),这样该主题就会以某种方式“关闭”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-19
    • 1970-01-01
    • 1970-01-01
    • 2012-10-20
    相关资源
    最近更新 更多