0
我有下面的公式Range("D3" , "D" & Total_Rows) = "=sum(A1:A10)"
如果我在D之前插入一列,現在這個公式被放在錯誤的列中。我被告知要爲列D使用一個命名範圍,但是對於這種類型的代碼,我沒有看到如何合併命名範圍,因爲在一個實例中,我需要它來引用單個單元格,而在另一個實例中,我需要它需要它引用一列。VBA代碼和命名範圍中的相對範圍
我有下面的公式Range("D3" , "D" & Total_Rows) = "=sum(A1:A10)"
如果我在D之前插入一列,現在這個公式被放在錯誤的列中。我被告知要爲列D使用一個命名範圍,但是對於這種類型的代碼,我沒有看到如何合併命名範圍,因爲在一個實例中,我需要它來引用單個單元格,而在另一個實例中,我需要它需要它引用一列。VBA代碼和命名範圍中的相對範圍
我不明白我如何可以合併命名範圍,因爲在一個實例中,我需要它引用單個單元格,而在另一個實例中,我需要它引用列。
那麼你可以嘗試用Name
的範圍性質的工作,然後你可以使用普通範圍的方法類似.Resize
或.Offset
等
Sub Test()
'Assume there is a named range in the worksheet
Dim nm As Name
Dim rngName As Range
Dim rngCell As Range
Dim rngColumn As Range
Set nm = ActiveSheet.Names(1)
Set rngName = Range(nm)
MsgBox "myName address: " & rngName.Address
Set rngCell = Range(nm).Resize(1, 1)
MsgBox "the first cell in myRange is " & rngCell.Address
Set rngCell = Range(nm).Resize(1, 1).Offset(3)
MsgBox "the third cell in myRange is " & rngCell.Address
Set rngColumn = Range(nm).EntireColumn
MsgBox "the column of myRange is " & rngColumn.Address
'Now insert a column in front of D
rngName.Insert
'Then view the addresses again, see that they have changed
Set rngName = Range(nm)
MsgBox "myName address: " & rngName.Address
Set rngCell = Range(nm).Resize(1, 1)
MsgBox "the first cell in myRange is " & rngCell.Address
Set rngCell = Range(nm).Resize(1, 1).Offset(3)
MsgBox "the third cell in myRange is " & rngCell.Address
Set rngColumn = Range(nm).EntireColumn
MsgBox "the column of myRange is " & rngColumn.Address
End Sub
是什麼代碼看起來的單細胞部分像目前? – Blackhawk
你嘗試過什麼嗎? –