我正在尝试确定电子表格中是否存在命名范围。如果它不存在,我不想为其设置变量。如果确实存在,我想为其设置一个变量。
我尝试了以下方法,但似乎措辞不太正确。
If RangeExist("Table1") <> 0 Then
Set QuBuild = ThisWorkbook.Worksheets("Features").Range("Table1")
Else
End If
如果存在命名范围“ Table1”,我想将其设置为变量“ QuBuild”,以便以后做一些事情(将表粘贴到word中)。如果它不存在,则我不想将“ QuBuild”设置为等于任何值。
任何帮助将不胜感激!
答案 0 :(得分:1)
以下是“命名范围”的专门答案。
Public Sub ExampleUsage()
Debug.Print DoesNameRangeExist("Table1")
End Sub
Public Function DoesNameRangeExist(ByVal NameOfRange As String) As Boolean
Dim NamedRanges As Names
Dim Name As Name
Set NamedRanges = ThisWorkbook.Names
DoesNameRangeExist = False
For Each Name In NamedRanges
If Name.Name = NameOfRange Then
DoesNameRangeExist = True
Exit For
End If
Next
End Function
答案 1 :(得分:0)
您是否尝试过像这样编写if语句
werkzeug
检查是否已设置Table1。
答案 2 :(得分:0)
尝试:
Option Explicit
Sub test()
Dim tbl As ListObject
Dim ws As Worksheet
'Loop all worksheets
For Each ws In ThisWorkbook.Worksheets
'Loop all tables - Listobjects
For Each tbl In ws.ListObjects
If tbl.Name = "Table1" Then
End If
Next tbl
Next ws
End Sub
或:
Sub test()
Dim strNameRange
Dim ws As Worksheets
For Each strNameRange In ThisWorkbook.Names
If strNameRange.Name = "Table1" Then
End If
Next strNameRange
End Sub