新手问题:我有模块, 最初由 Roger Govier 制作。
它使用输入单元格header
并为位于header
. 创建的命名范围将被标记为标题单元格的值。
Private Sub CreateNamedRange(header As range)
Dim wb As Workbook
Dim WS As Worksheet
Dim rStartCell As range
Dim rData As range
Dim rCol As range
Dim lCol As Long
Dim sSheet As String
Dim Rowno As Long
' get table location
Set rStartCell = header
Set WS = rStartCell.Worksheet
Set wb = WS.Parent
sSheet = "'" & WS.Name & "'"
With rStartCell
Rowno = .row
Set rData = .CurrentRegion
End With
Set rData = WS.range(rStartCell, WS.Cells(Rowno, rStartCell.Column))
Set rCol = rData.Columns
lCol = rCol.Column
wb.Names.Add Name:=Replace(rCol.Cells(1).Value, " ", "_"), _
RefersToR1C1:="=" & sSheet & "!" & rCol.Cells(2).Address(ReferenceStyle:=xlR1C1) & ":INDEX(C" & lCol & ",LOOKUP(2,1/(C" & lCol & "<>""""),ROW(C" & lCol & ")))"
End Sub
我想修改此代码,以便它不创建命名范围,而是仅返回返回的范围是命名范围的范围。
示例:我们在 中有一个标头,在 中有A1
数据A2:A5
。
现在:如果我们调用CreateNamedRange(.range("A1"))
,它会为A2:A5
.
目标:如果我们调用 CreateNamedRange(.range("A1"))
它返回.range("A2:A5")
VBA代码中的一个变量:
dim myRange As Range
set myRange = CreateNamedRange(.range("A1"))