有没有一种方法可以简化我的代码,而不必为每一列复制以下代码?
我能够遍历一列中的一系列行并应用公式(在这种情况下为计数)。我如何对AA:AZ列应用相同的内容?
我当前的代码如下:
Sub CountIfsFormula2()
Dim lstrow As Long
Dim i As Long
Application.DisplayAlerts = False
Application.AskToUpdateLinks = False
Application.Calculation = xlCalculationManual
lstrow = Cells(Rows.Count, "B").End(xlUp).Row
For i = 2 To lstrow
Range("C" & i).Formula = "=COUNTIFS('Agent_Detail_Data'!C,Sheet1!R1C3,'Agent_Detail_Data'!C[1],"">=""&Sheet1!RC[-1],'Agent_Detail_Data'!C[1],""<""&Sheet1!R[1]C[-1],'Agent_Detail_Data'!C[11],Sheet1!R1C1)"
Range("D" & i).Formula = "=COUNTIFS('Agent_Detail_Data'!C[-1],Sheet1!R1C4,'Agent_Detail_Data'!C,"">=""&Sheet1!RC[-2],'Agent_Detail_Data'!C,""<""&Sheet1!R[1]C[-2],'Agent_Detail_Data'!C[10],Sheet1!R1C1)"
Range("E" & i).Formula = "=COUNTIFS('Agent_Detail_Data'!C3,Sheet1!R1C5,'Agent_Detail_Data'!C[-1],"">=""&Sheet1!RC[-3],'Agent_Detail_Data'!C[-1],""<""&Sheet1!R[1]C[-3],'Agent_Detail_Data'!C[9],Sheet1!R1C1)"
Range("F" & i).Formula = "=COUNTIFS('Agent_Detail_Data'!C3,Sheet1!R1C6,'Agent_Detail_Data'!C[-2],"">=""&Sheet1!RC[-4],'Agent_Detail_Data'!C[-2],""<""&Sheet1!R[1]C[-4],'Agent_Detail_Data'!C[8],Sheet1!R1C1)"
Range("G" & i).Formula = "=COUNTIFS('Agent_Detail_Data'!C3,Sheet1!R1C7,'Agent_Detail_Data'!C[-3],"">=""&Sheet1!RC[-5],'Agent_Detail_Data'!C[-3],""<""&Sheet1!R[1]C[-5],'Agent_Detail_Data'!C[7],Sheet1!R1C1)"
Range("H" & i).Formula = "=COUNTIFS('Agent_Detail_Data'!C3,Sheet1!R1C8,'Agent_Detail_Data'!C[-4],"">=""&Sheet1!RC[-6],'Agent_Detail_Data'!C[-4],""<""&Sheet1!R[1]C[-6],'Agent_Detail_Data'!C[6],Sheet1!R1C1)"
Range("I" & i).Formula = "=COUNTIFS('Agent_Detail_Data'!C3,Sheet1!R1C9,'Agent_Detail_Data'!C[-5],"">=""&Sheet1!RC[-7],'Agent_Detail_Data'!C[-5],""<""&Sheet1!R[1]C[-7],'Agent_Detail_Data'!C[5],Sheet1!R1C1)"
Range("J" & i).Formula = "=COUNTIFS('Agent_Detail_Data'!C3,Sheet1!R1C10,'Agent_Detail_Data'!C[-6],"">=""&Sheet1!RC[-8],'Agent_Detail_Data'!C[-6],""<""&Sheet1!R[1]C[-8],'Agent_Detail_Data'!C[4],Sheet1!R1C1)"
Next i
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.AskToUpdateLinks = True
Application.Calculation = xlCalculationAutomatic
Exit Sub
End Sub