名为ranged的通配符

时间:2017-07-18 12:42:23

标签: vba excel-vba excel

我的VBA中有以下代码:

Call add_validList (Sheet1.Range("MyCell_1_Type"), "=datalist"

如何为命名范围MyCell_1_Type?

引入通配符

我有许多这些命名范围,即MyCell_1_Type,MyCell_2_Type,MyCell_3_Type,MyCell_4_Type,.............,MyCell_1000_Type。

这是我追求的解决方案:

我尝试过以下操作但不起作用:

Call add_validList (Sheet1.Range("MyCell_&"*"&_Type"), "=datalist"  

谢谢大家。

2 个答案:

答案 0 :(得分:0)

for循环怎么样?

您可以使用for循环操作1到1000次,例如,

For i = 1 To 1000
    Call add_validList(Sheet1.Range("MyCell_" & i & "_Type"), "=datalist")
Next i

更改为以下内容,请注意这将引用整个工作簿中的命名范围。

Dim n
For Each n In ActiveWorkbook.Names
    Call add_validList(Sheet1.Range(n.Name), "=datalist")
Next i

答案 1 :(得分:0)

尝试此功能

Public Function GetNamedRanges(ByVal part1 As String, ByVal part2 As String) As Range
    Dim itm As Name, r As Range, found As Long

    For Each itm In Names
        found = InStr(itm.Name, part1)
        If found > 0 And InStr(found + 1, itm.Name, part2) > 0 Then
            If r Is Nothing Then Set r = Range(itm) Else Set r = Union(r, Range(itm))
        End If
    Next
    Set GetNamedRanges = r
End Function

要使用GetNamedRanges("MyCell_", "_Type")

来调用它