基于标记为日期的新创建工作表在VBA中发布连锁公式

时间:2018-05-18 14:34:50

标签: excel vba excel-vba

我有一个脚本,它创建一个包含数据的工作表副本,查询新数据,然后使用查找(在这种情况下为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

1 个答案:

答案 0 :(得分:1)

尝试

  1. LastRow应该是> 1
  2. 使用Long而不是Integer
  3. 有一个拼写错误","最后
  4. 在工作表名称
  5. 之前和之后添加'
  6. 与&连接(根据评论)
  7. 使用工作表集合
  8. 我想你可以发帖到destination Worksheets(CopySheet).Range("A1") - 但我可能错了。
  9. 代码:

    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