我有一个脚本,它创建一个包含数据的工作表副本,查询新数据,然后使用查找(在这种情况下为SUMIFS)将旧工作表中的任何手动条目提取到新工作表上。我在行上删除了一个应用程序定义的/对象定义的错误消息,将公式丢弃到页面上(下面的最后一行)。我尝试直接添加公式,使用.Cells等,但它必须使用公式本身的语言,因为我可以用这种方式删除直接公式(例如.Range("N2:N" & LastRow) = "=M2*H2"
)
Dim CopySheet As String
Dim QSheet As String
Dim FormulaS As String
Dim LastRow As Integer
Dim CoreWB As Workbook
Set CoreWB = ActiveWorkbook
QSheet = "QData"
CopySheet = Format(Date, "mm-dd-yy")
With CoreWB
.Sheets.Add(After:=.Sheets("HistSum")).Name = CopySheet
.Sheets(QSheet).Cells.Copy _
Destination:=Sheets(CopySheet).Cells
End With
FormulaS = "=SUMIFS(" + CopySheet + "!M:M," + CopySheet + "!A:A, A:A," + CopySheet + "!C:C, C:C," + CopySheet + "!J:J, J:J," + CopySheet + ",!E:E, E:E)"
With Sheets(QSheet)
LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
.Range("M2:M" & LastRow).Formula = FormulaS
答案 0 :(得分:1)
尝试
'
destination Worksheets(CopySheet).Range("A1")
- 但我可能错了。代码:
Option Explicit
Sub TEST()
Dim CopySheet As String
Dim QSheet As String
Dim FormulaS As String
Dim LastRow As Long
Dim CoreWB As Workbook
Set CoreWB = ActiveWorkbook
QSheet = "QData"
CopySheet = Format$(Date, "mm-dd-yy")
With CoreWB
.Sheets.Add(After:=.Worksheets("HistSum")).Name = CopySheet
.Sheets(QSheet).Cells.Copy _
Destination:=Worksheets(CopySheet).Cells
End With
FormulaS = "=SUMIFS(" & "'" & CopySheet & "'!M:M,'" & CopySheet & "'!A:A, A:A,'" & CopySheet & "'!C:C, C:C,'" & CopySheet & "'!J:J, J:J,'" & CopySheet & "'!E:E, E:E)"
With Worksheets(QSheet)
LastRow = .Range("A" & .Rows.Count).End(xlUp).Row
' LastRow > 1
.Range("M2:M" & LastRow).Formula = FormulaS
End With
End Sub