[英]How can I use VBA to ignore green triangle error in range without looping cell by cell?
[英]How can I use a Cell Value as a Named Range in VBA?
所以我的示例文檔有兩個工作表。 在Sheet2
,列A
填充了指向Sheet1
命名單元格的超鏈接。 作為參考,我需要將每個鏈接引用的命名范圍的名稱復制到另一個工作表中,我還將記錄其位置相對於指定單元格位置的單元格中的其他項目。 問題似乎是當我嘗試從其中包含命名單元格名稱的單元格值設置范圍時。
例
Sheet2(命名為“文檔圖”)
|A |B|
1|Link (address is #_123 which is a named cell) | |
2|Link 2 (address is #_89 which is a named cell)| |
3|Normal text cell, link location not recorded | |
工作表Sheet1
|A |B |C|
1|(named _123) |relevant info from _123| |
2|relevant info from _123 | | |
3| |relevant info from _123| |
4|relevant info from _123 | | |
5|(named _89) | | |
6| |relevant info from _89 | |
7|relevant info from _89 | | |
我的VBA
Option Explicit
Public Sub getOrderHeaderLocations()
Dim rng As range
Dim c As range
Dim a As Hyperlink
Dim r As Integer
Dim tr As range
Dim map As Worksheet
Dim transList As Worksheet
Dim table As Worksheet
Set map = Sheets("Document map")
Set transList = Sheets("Sheet1")
Set table = Sheets.Add
table.range("A1").Value = "Named Cell"
table.range("B1").Value = "Info 1"
table.range("C1").Value = "Info 2"
table.range("D1").Value = "Info 3"
map.Activate
Set rng = range(map.range("A1"), "A" & map.range("A1").CurrentRegion.Rows.Count)
'use a counter because not all lines have links and I don't want empty rows
r = 2 'start in the second row of the table sheet (after headers)
For Each c In rng.Cells
If c.Hyperlinks.Count > 0 Then
Set a = c.Hyperlinks.Item(1)
table.range("A" & r).Value = a.SubAddress
r = r + 1
End If
Next
' Glorious, I have a list of the cell names
table.Activate
' set range to the list of names that we made
Set rng = range(table.range("A2"), "A" & table.range("A2").CurrentRegion.Rows.Count)
For Each c In rng.Cells
r = c.Row
' set a range from the value of c which has the reference name of a cell)
tr = range(c.Value) ' Error 91: Object variable or With block variable not set
table.range("B" & r).Value = transList.range("B" & tr.Row).Value
Next
End Sub
我收到錯誤91的地方,我試圖從單元格的值設置一個范圍。 我知道單元格的名稱確實存在,我可以通過在excel的名稱框中鍵入值( _123
)來驗證。 我正在使用Excel 2010。
設置范圍時,您需要使用Set
Set tr = Range(c.Value)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.