如何在VBA中用variables起始列编写公式?

我有一个运行4个公式的macros。

Sub Kit() Dim ws As Worksheet Dim LastRow As Long Dim i, n, x As Integer Set ws = ActiveWorkbook.Sheets("Report KIT (2)") ws.Select LastRow = Sheets("Report KIT (2)").Range("A" & Sheets("Report KIT (2)").Rows.Count).End(xlUp).Row For i = 3 To LastRow On Error Resume Next If Range("BR" & i) >= Range("AM" & i) Then Range("BS" & i) = "C" Else: Range("BS" & i) = "GA + C" End If Next i For i = 3 To LastRow On Error Resume Next Range("BT" & i).Select ActiveCell.FormulaR1C1 = _ "=IF(RC[-1]=""C"",(RC[-3]/SUMIFS(C[-3],C[-6],RC[-6]))*(VLOOKUP('Report KIT (2)'!RC[-6],GA_C!C[-71]:C[-68],4,0)),SUM((RC[-3]/SUMIFS(C[-3],C[-6],RC[-6]))*(VLOOKUP('Report KIT (2)'!RC[-6],GA_C!C[-71]:C[-68],4,0)),(RC[-3]/SUMIFS(C[-3],C[-6],RC[-6],C[-1],""GA + C""))*(VLOOKUP('Report KIT (2)'!RC[-6],GA_C!C[-71]:C[-69],3,0))))" Next i For i = 3 To LastRow On Error Resume Next Range("BU" & i).Select ActiveCell.FormulaR1C1 = "=RC[-4]+RC[-1]" Next i For i = 3 To LastRow On Error Resume Next Range("BV" & i).Select ActiveCell.FormulaR1C1 = "=(RC[-2]+RC[-5])*0.13" Next i End Sub 

我想修改它以重复相同的计算,但是在所有4个公式的每个完整循环之后移动起始列:BS; BT; BU; BV在4个单元格中转发(所以在下一个循环他们成为BW; BX; BY; BZ,然后在第三次运行CA; CB; CC; CD等)我想循环11次。 任何人都可以帮忙吗?

你可以试试下面。 我使用单元格属性引用了带数字的列。 在每个公式循环之后,列增加1。

还要记住,如果你声明像这样的variablesDim i, n, x As Integer它只会将x声明为一个整数, in将被声明为变体。

 Option Explicit Sub Kit() Dim ws As Worksheet Dim LastRow As Long Dim i As Integer, n As Integer, x As Integer, j As Integer, mcol As Integer Set ws = ActiveWorkbook.Sheets("Report KIT (2)") ws.Select LastRow = ws.Range("A" & ws.Rows.Count).End(xlUp).Row mcol = 71 For j = 1 To 11 For i = 3 To LastRow On Error Resume Next If Cells(i, mcol - 1) >= Range("AM" & i) Then Cells(i, mcol) = "C" Else Cells(i, mcol) = "GA + C" End If Next i mcol = mcol + 1 For i = 3 To LastRow On Error Resume Next Cells(i, mcol) = "D" ''formula using mcol Next i mcol = mcol + 1 For i = 3 To LastRow On Error Resume Next Cells(i, mcol) = "E" ''formula using mcol Next i mcol = mcol + 1 For i = 3 To LastRow On Error Resume Next Cells(i, mcol) = "F" ''formula using mcol Next i mcol = mcol + 1 Next j End Sub 

您需要2个新的循环 ,并将范围方法更改为单元格方法

 For mainLoop = 1 To 11 For newLoop = 0 To 4 'demonstration of the change 'in EDIT added the (newLoop * 4) * mainLoop into the column increment For i = 3 To LastRow If Cells(i, 70 + (newLoop * 4) * mainLoop ) >= Cells(i, 39) Then 'change the right part of compare >= as needed Cells(i, 71 + (newLoop * 4) * mainLoop ) = "C" Else: Cells(i, 71 + (newLoop * 4)*mainLoop ) = "GA + C" End If Next i 'repeat similar change in all other loops For i = 3 To LastRow '... Next i For i = 3 To LastRow '... Next i For i = 3 To LastRow '... Next i Next newLoop Next mainLoop 

编辑2

从问题的作者正确的评论…这应该做的伎俩。

  For mainLoop = 0 To 10 For newLoop = 0 To 3 'changed to 3 For i = 3 To LastRow If Cells(i, 70 + newLoop * 4 + 16 * mainLoop) >= Cells(i, 39) Then 'change the right part of compare >= as needed Cells(i, 71 + newLoop * 4 + 16 * mainLoop) = "C" Else: Cells(i, 71 + newLoop * 4 + 16 * mainLoop) = "GA + C" End If Next i 'repeat similar change in all other loops For i = 3 To LastRow '... Next i '... Next newLoop Next mainLoop