我选择了一系列单元格,我想用单词SUM连接所选单元格的单元格引用。
e.g。如果我选择单元格A1,我希望我的宏返回引用“A1SUM”
我的代码
Sub UpdateCellReference()
For Each mycell In Selection
mycell.Name (mycell.Name & "SUM")
Next mycell
End Sub
答案 0 :(得分:0)
您可以尝试以下代码:
Sub UpdateCellReference()
Dim mycell As Range
Dim MyStr As String
For Each mycell In Selection
MyStr = MyStr & mycell.Address(False, False) & "SUM"
Next mycell
MsgBox MyStr
End Sub
答案 1 :(得分:0)
使用Shai Rado的代码作为起点。
将他的MsgBox MyStr
行改为
AllocateNamedRange ThisWorkbook, MyStr, "='" & Selection.Parent.Name & "'!" & Selection.Address, "A1"
并将这两个过程添加到模块中:
Public Sub AllocateNamedRange(Book As Workbook, sName As String, sRefersTo As String, Optional ReferType = "R1C1")
With Book
If NamedRangeExists(Book, sName) Then .Names(sName).Delete
If ReferType = "R1C1" Then
.Names.Add Name:=sName, RefersToR1C1:=sRefersTo
ElseIf ReferType = "A1" Then
.Names.Add Name:=sName, RefersTo:=sRefersTo
End If
End With
End Sub
Public Function NamedRangeExists(Book As Workbook, sName As String) As Boolean
On Error Resume Next
NamedRangeExists = Book.Names(sName).Index <> (Err.Number = 0)
On Error GoTo 0
End Function