2017-08-03 200 views
0

我有一個很長的宏,它的這一塊不能正常工作。我已經通過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 
+0

除了使用FillDown的,只是套用公式整個範圍在一次:'工作簿(「主客戶聲明文件」)表(「通過客戶摘要」)範圍(「E3:E」。 &ByCustRow).Formula = ...' –

+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 
+0

感謝您的建議!我一定會考慮使用前進的方塊,並且我會改變放棄剪貼板。 這條線是幹什麼的? (「B3」)。Resize(LastRow - 2,3).Value = wb.Sheets(「Scrubbed Data」)。範圍(「C3:E」和LastRow)。值 對不起,我可以'不知道如何像你這樣的代碼來阻止它。 –

+0

它使用Resize將範圍(「C3:E」和LastRow)的值分配給以相同大小的B3開始的範圍。這需要on cell,並使得範圍與我們獲取值的範圍一樣大。當直接賦值時,範圍需要是相同的大小。 –

+0

你也可以做'.Range(「B3:D」&LastRow).Value = ...'也是。這是相同的。 –