作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
嗨,我在以下代码的 for 循环中不断收到“变量或未设置 block 变量”的信息。谁能告诉我哪里出错了?谢谢
Public Sub TestFind()
Set wsNew = Worksheets.Add(after:=Worksheets(Worksheets.Count))
wsNew.Range(wsNew.Cells(1, 1), wsNew.Cells(1, 17)).Value _
= Array("ReturnId", "GridName", "Item", "TabName", "AltFldName", "FieldPos", "Reference", "Type", _
"SortPos", "FieldSize", "CalcField", "CellDesc", "DoNotExport", "SortStrategy", "Threshold", "IsInnerGridCell", "ReportLine")
Dim Nm As Name
Dim rng As Range
Dim wb As Workbook
Set wb = ThisWorkbook
For Each Nm In ThisWorkbook.Names
rng = Nm.RefersToRange.Value
Next
End Sub
最佳答案
您的代码中有两个问题:
1- 并非所有名称都一定指命名范围。例如,它们可能指的是常量。因此,在假设名称确实引用一个范围之前,您需要添加一些检查。
2-要将范围对象分配给命名范围,您需要使用Set
试试这个:
Dim Nm As Name, rng As Range
For Each Nm In ThisWorkbook.Names
Debug.Print Nm.Name
On Error Resume Next
Set rng = Nm.RefersToRange ' <-- Use Set to assign object references
If Err.Number <> 0 Then GoTo NextNm ' <-- This name does not refer to a named range
On Error GoTo 0
Debug.Print rng.Address
' ... Do whatever you want with the named range
NextNm:
On Error GoTo 0
Next Nm
关于excel - ReferesToRange 给出运行时错误 '91' :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43347301/
嗨,我在以下代码的 for 循环中不断收到“变量或未设置 block 变量”的信息。谁能告诉我哪里出错了?谢谢 Public Sub TestFind() Set wsNew = Workshee
我是一名优秀的程序员,十分优秀!