我如何使用单元值作为VBA中的命名范围?

所以我的示例文档有两个工作表。 在工作表Sheet2 ,列A填充超链接到Sheet1中的已命名的单元格。 作为参考,我需要将每个链接引用的命名范围的名称复制到另一个工作表中,我还将logging其位置与命名单元位置相关的其他单元。 这个问题似乎是当我尝试设置范围从一个单元格的名称,其中的命名单元格的值。

Sheet2(被命名为“Document map”)

  |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的名称框中input值( _123 )来validation。 我正在使用Excel 2010。

设置范围时,您需要使用Set

 Set tr = Range(c.Value)