自动填充直到最后一行,countif,准备好宏观调整

时间:2013-12-09 17:38:28

标签: excel excel-vba vba

我想通过一个简单的宏请求帮助。它为我的队友配合比较数据并应用countif功能并带来正确的数据。但是,宏将自动填充设置为D108。我想稍微更改它以自动填充它直到上一个填充行。谁能帮我修改它才能正常工作?

Sub Countif_function()

Sheets("Account Campaign Member").Select
Range("D1").Select
ActiveCell.FormulaR1C1 = "How Many Contacts do we have?"
Range("D2").Select
ActiveCell.FormulaR1C1 = "=COUNTIF('Campaign Member'!C[-2],RC[-1])"
Selection.AutoFill Destination:=Range("D2:D108")
Range("D2:D108").Select
Columns("D:D").Select
Selection.Copy
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=False
Columns("B:D").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("Comparison").Select
Range("A1").Select
ActiveSheet.Paste
Columns("A:B").EntireColumn.AutoFit
Columns("A:B").EntireColumn.AutoFit
Columns("B:B").Select
End Sub

2 个答案:

答案 0 :(得分:0)

尝试此更新

Sub Countif_function()

Sheets("Account Campaign Member").Range("D1")= "How Many Contacts do we have?"
with Sheets("Account Campaign Member").Range("D2:D" & .cells(rows.count,3).end(xlup).row)
    .FormulaR1C1 = "=COUNTIF('Campaign Member'!C[-2],RC[-1])"
    .value=.value
end With

Sheets("Account Campaign Member").Columns("B:D").copy Sheets("Comparison").Range("A1")
Sheets("Comparison").Columns("A:B").EntireColumn.AutoFit
Sheets("Comparison").Columns("B:B").Select

End Sub

答案 1 :(得分:0)

使用此代码
LastRow = Range("A2").End(xlDown).Row
Range("D2").AutoFill Destination:=Range(Range("D2"), Range("D" & LastRow))

而不是

Selection.AutoFill Destination:=Range("D2:D108")