Sub MakeSum(sBookName As String, sSheetName As String)
Dim rMyRange As Range
Set rMyRange = Workbooks(sBookName).Sheets(sSheetName).Range("H11:H32")
If rMyRange.Parent.Parent.Name = ActiveWorkbook.Name Then
With ActiveSheet
'same sheet, so just use address
If rMyRange.Parent.Name = .Name Then
.Range("B2").Formula = "=SUM(" & rMyRange.Address & ")"
Else
'same workbook, different sheet, so prepend sheet name
'single quotes prevent error when there's a space in the sheet name
.Range("B2").Formula = "=SUM('" & rMyRange.Parent.Name & "'!" & rMyRange.Address & ")"
End If
End With
Else
'not the same workbook, use external address
ActiveSheet.Range("B2").Formula = "=SUM(" & rMyRange.Address(, , , True) & ")"
End If
End Sub
不,我已經嘗試過與實際..我想通過這種粘貼以字符串編輯單元格...我需要的...如果我這樣做「=」&範圍1它的工作原理..這是奇怪的..所以當我添加一個Excel函數將停止工作... – Matias 2011-05-18 11:58:12
這是錯誤的。 'MyRange'是一個變量,該行將正確計算爲= = SUM([MyRange的值])。其他答案解釋瞭如何解決這個問題。 – Gaffi 2012-07-26 13:48:06