填写不起作用

时间:2017-08-03 16:37:25

标签: excel vba excel-vba

我有一个很长的宏,这一部分不能正常工作。我已经检查了Stack Overflow和其他来源,但无法弄清楚问题是什么。当我在一个单独的宏中运行此代码时,它似乎工作正常,但当我将它作为较大的宏的一部分包含它,因为它意味着它不再有效。最终结果是我的公式仅在第3行中,并未按预期填充到底部。这是宏与此工作表或数据交互的唯一部分。除此之外的任何事情都不会导致问题。我复制+粘贴特殊值的末尾部分是通过冻结数据来减少处理时间。以下是有问题的代码:

Dim ByCustRow As Long
'Populate the By Customer Summary

Workbooks("Master Customer Statement File").Sheets("Scrubbed Data").Range("C3:E" & LastRow).Copy
Workbooks("Master Customer Statement File").Sheets("By Customer Summary").Range("B3").PasteSpecial xlPasteValues
Workbooks("Master Customer Statement File").Sheets("By Customer Summary").Range("B3:D" & LastRow).RemoveDuplicates Columns:=Array(1, 2, 3)

ByCustRow = Workbooks("Master Customer Statement File").Sheets("By Customer Summary").Range("B" & Rows.Count).End(xlUp).Row

Workbooks("Master Customer Statement File").Sheets("By Customer Summary").Range("E3").Formula = "=SUMIFS('Scrubbed Data'!$J:$J,'Scrubbed Data'!$C:$C,B3,'Scrubbed Data'!$I:$I,"">=""&'By Customer Summary'!$C$1)": Range("E3:E" & ByCustRow).FillDown
Workbooks("Master Customer Statement File").Sheets("By Customer Summary").Range("F3").Formula = "=SUMIFS('Scrubbed Data'!$J:$J,'Scrubbed Data'!$C:$C,B3,'Scrubbed Data'!$I:$I,""<""&'By Customer Summary'!$C$1,'Scrubbed Data'!$I:$I,"">=""&'By Customer Summary'!$D$1)": Range("F3:F" & ByCustRow).FillDown
Workbooks("Master Customer Statement File").Sheets("By Customer Summary").Range("G3").Formula = "=SUMIFS('Scrubbed Data'!$J:$J,'Scrubbed Data'!$C:$C,B3,'Scrubbed Data'!$I:$I,""<""&'By Customer Summary'!$D$1,'Scrubbed Data'!$I:$I,"">=""&'By Customer Summary'!$E$1)": Range("G3:G" & ByCustRow).FillDown
Workbooks("Master Customer Statement File").Sheets("By Customer Summary").Range("H3").Formula = "=SUMIFS('Scrubbed Data'!$J:$J,'Scrubbed Data'!$C:$C,B3,'Scrubbed Data'!$I:$I,""<""&'By Customer Summary'!$E$1,'Scrubbed Data'!$I:$I,"">=""&'By Customer Summary'!$F$1)": Range("H3:H" & ByCustRow).FillDown
Workbooks("Master Customer Statement File").Sheets("By Customer Summary").Range("I3").Formula = "=SUMIFS('Scrubbed Data'!$J:$J,'Scrubbed Data'!$C:$C,B3,'Scrubbed Data'!$I:$I,""<""&'By Customer Summary'!$F$1)": Range("I3:I" & ByCustRow).FillDown


Workbooks("Master Customer Statement File").Sheets("By Customer Summary").Range("B3:I" & ByCustRow).Copy
Workbooks("Master Customer Statement File").Sheets("By Customer Summary").Range("B3:I" & ByCustRow).PasteSpecial xlPasteValues
Application.CutCopyMode = False

'End Populate the By Customer Summary

1 个答案:

答案 0 :(得分:1)

不要使用FillDown,只需立即将公式应用于整个范围

还要了解With块,它会减少输入的数量。

同样,只粘贴值时,只需直接分配值并跳过剪贴板。

Dim ByCustRow As Long
'Populate the By Customer Summary
Dim wb As Workbook
Set wb = Workbooks("Master Customer Statement File")

With wb.Sheets("By Customer Summary")
    .Range("B3").Resize(LastRow - 2, 3).Value = wb.Sheets("Scrubbed Data").Range("C3:E" & LastRow).Value
    .Range("B3:D" & LastRow).RemoveDuplicates Columns:=Array(1, 2, 3)

    ByCustRow = .Range("B" & Rows.Count).End(xlUp).Row

    .Range("E3:E" & ByCustRow).Formula = "=SUMIFS('Scrubbed Data'!$J:$J,'Scrubbed Data'!$C:$C,B3,'Scrubbed Data'!$I:$I,"">=""&'By Customer Summary'!$C$1)"
    .Range("F3:F" & ByCustRow).Formula = "=SUMIFS('Scrubbed Data'!$J:$J,'Scrubbed Data'!$C:$C,B3,'Scrubbed Data'!$I:$I,""<""&'By Customer Summary'!$C$1,'Scrubbed Data'!$I:$I,"">=""&'By Customer Summary'!$D$1)"
    .Range("G3:G" & ByCustRow).Formula = "=SUMIFS('Scrubbed Data'!$J:$J,'Scrubbed Data'!$C:$C,B3,'Scrubbed Data'!$I:$I,""<""&'By Customer Summary'!$D$1,'Scrubbed Data'!$I:$I,"">=""&'By Customer Summary'!$E$1)"
    .Range("H3:H" & ByCustRow).Formula = "=SUMIFS('Scrubbed Data'!$J:$J,'Scrubbed Data'!$C:$C,B3,'Scrubbed Data'!$I:$I,""<""&'By Customer Summary'!$E$1,'Scrubbed Data'!$I:$I,"">=""&'By Customer Summary'!$F$1)"
    .Range("I3:I" & ByCustRow).Formula = "=SUMIFS('Scrubbed Data'!$J:$J,'Scrubbed Data'!$C:$C,B3,'Scrubbed Data'!$I:$I,""<""&'By Customer Summary'!$F$1)"


    .Range("B3:I" & ByCustRow).Value = .Range("B3:I" & ByCustRow).Value

End With