我正在通过将原始文件中的数据粘贴到名为PA Reach的模板中来自动化某些报告。我已经命名了一个范围“Sumrow”,我需要VBA在粘贴数据之后在每个循环中自动清除上面的数字。这就是我所拥有的并且我遇到错误:
Workbooks.Open datapath & datafile1 & OutputFileExt
Sheets("Rep").Activate
ActiveSheet.UsedRange.AutoFilter
For i = 1 To terrs_to_generate
Workbooks(builder).Sheets("Control").Activate
Range("Reportnum") = i
currfile = Range("CurrFile").Value
currterr = Range("CurrTerr").Value
currRep = Range("currRep").Value
terrname = Range("terrname").Value
DMName = Range("DMName").Value
TMStartDate = Range("TMstartdate").Value
'open template
Workbooks.Open templatepath & templatefile & OutputFileExt
Set currtemplatefile = ActiveWorkbook
Sheets("Control").Select
Sheets("control").Range("terrname") = terrname
Sheets("control").Range("repname") = currRep
Sheets("control").Range("reportdate") = reportdate
Sheets("control").Range("TMstartdate") = TMStartDate
Sheets("control").Range("DMName") = DMName
Sheets("control").Range("Territory") = currterr
Workbooks(datafile1 & OutputFileExt).Activate
Selection.AutoFilter field:=1, Criteria1:=currterr
Range("A1").Select
Range(Selection.Offset(1, 1).End(xlDown), Selection.End(xlToRight)).Copy
currtemplatefile.Activate
Sheets("PA Reach").Select
Range("pasterange").Select
Selection.PasteSpecial Paste:=xlPasteValues
currtemplatefile.Activate
Range("formatrow").Copy
Range("pasterange").Select
Range(Selection.End(xlToRight), Selection.End(xlDown).Offset(0, 0)).Select
Selection.PasteSpecial Paste:=xlPasteFormats
'Delete PasteRange
Range("pasterange").Select
Selection.EntireRow.Delete
'Value Range Sheet so no formulas show
ActiveSheet.UsedRange.Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues
Range("A1").Select
Sheets("control").Delete
'need to add in sum total
Range("sumrow1").End(xlDown).Offset(1, 0) = "=Sum(" & Range("sumrow1").Address(True, True) & ")"
'save as and close
ActiveWorkbook.SaveAs Filename:=(reportpath & currfile & OutputFileExt) 'not working
答案 0 :(得分:0)
无需Select
Range("sumrow")
,然后使用Selection
,您可以直接将Formula
修改为Range
。
尝试下面的代码,它会将Sum
公式放在" sumrow"的最后一行下方的行中。命名范围:
Range("sumrow").End(xlDown).Offset(1, 0) = "=Sum(" & Range("sumrow").Address(True, True) & ")"