如何在VBA中将动态更改列号定义为公式?

时间:2015-08-04 12:33:09

标签: excel vba excel-vba

我有一个计算公式的VBA代码(我知道它很长):

Cells(i, mcol) = "=IF(RC[-1]=""C"",(RC[-3]/SUMIFS(C[-3],C66,RC66))*(VLOOKUP('Sheet1'!RC66,GA_C!C1:C4,4,0)),SUM((RC[-3]/SUMIFS(C[-3],C66,RC66))*(VLOOKUP('Sheet1'!RC66,GA_C!C1:C4,4,0)),(RC[-3]/SUMIFS(C[-3],C66,RC66,C[-1],""GA + C""))*(VLOOKUP('Sheet1'!RC66,GA_C!C1:C3,3,0))))"

在Vlookup中,它位于C1:C4范围内的第4列和范围C1:C3中的第3列。

直到列号(4和3)固定为止。 现在它每次运行时都会改变。 例如,第二次运行的列号为5和4,第三次运行为6和5,依此类推至12。 有没有办法将动态变化的列号集成到上面的公式中?

非常感谢!

我也提出了整个代码。

Sub AutoCalcV2()

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("Sheet1")
ws.Select
LastRow = Sheets("Sheet1").Range("A" & Sheets("Sheet1").Rows.Count).End(xlUp).Row

mcol = 71
For j = 1 To 11

    mcol = mcol + 1
    For i = 3 To LastRow
        On Error Resume Next
        Cells(i, mcol) = "=IF(RC[-1]=""C"",(RC[-3]/SUMIFS(C[-3],C66,RC66))*(VLOOKUP('Sheet1'!RC66,GA_C!C1:C4,4,0)),SUM((RC[-3]/SUMIFS(C[-3],C66,RC66))*(VLOOKUP('Sheet1'!RC66,GA_C!C1:C4,4,0)),(RC[-3]/SUMIFS(C[-3],C66,RC66,C[-1],""GA + C""))*(VLOOKUP('Sheet1'!RC66,GA_C!C1:C3,3,0))))"
Range("BT4").Select
    Next i
Next j
End Sub

3 个答案:

答案 0 :(得分:1)

Dim iColumn as Integer

mcol = 71
For j = 1 To 11

    iColumn = 4
    mcol = mcol + 1

    For i = 3 To LastRow

        On Error Resume Next
        Cells(i, mcol) = "=IF(RC[-1]=""C"",(RC[-3]/SUMIFS(C[-3],C66,RC66))*(VLOOKUP('Sheet1'!RC66,GA_C!C1:C4," & str(iColumn) & ",0)),SUM((RC[-3]/SUMIFS(C[-3],C66,RC66))*(VLOOKUP('Sheet1'!RC66,GA_C!C1:C4," & str(iColumn) & ",0)),(RC[-3]/SUMIFS(C[-3],C66,RC66,C[-1],""GA + C""))*(VLOOKUP('Sheet1'!RC66,GA_C!C1:C3,3,0))))"
        Range("BT4").Select

        iColumn = iColumn + 1

    Next i
Next j

答案 1 :(得分:0)

所以根据我的理解,你有3个vlookups,你想使用4(4 + 1,5 + 1,6 + 1)前两个vlookups和3(3 + 1,4 + 1,5 + 1)为第三个。

如果是这样,那么你可以在这里增加你的4和3。

Sub AutoCalcV2()
    Dim ws As Worksheet
    Dim LastRow As Long
    Dim i, n, x, j, mcol, iCol As Integer  '<-- Changed here
    Set ws = ActiveWorkbook.Sheets("Sheet1")
    ws.Select
    LastRow = Sheets("Sheet1").Range("A" & Sheets("Sheet1").Rows.Count).End(xlUp).Row

    mcol = 71
    iCol = 4  '<-- Newly added
    For j = 1 To 11
        mcol = mcol + 1
        For i = 3 To LastRow
            On Error Resume Next
            'Changed the formula
            Cells(i, mcol) = "=IF(RC[-1]=""C"",(RC[-3]/SUMIFS(C[-3],C66,RC66))" & _
                "*(VLOOKUP('Sheet1'!RC66,GA_C!C1:C4," & iCol & ",0)),SUM((RC[-3]/SUMIFS(C[-3],C66,RC66))" & _
                "*(VLOOKUP('Sheet1'!RC66,GA_C!C1:C4," & iCol & ",0)),(RC[-3]/SUMIFS(C[-3],C66,RC66,C[-1],""GA + C""))" & _
                "*(VLOOKUP('Sheet1'!RC66,GA_C!C1:C3," & i & ",0))))"
            Range("BT4").Select
            iCol = iCol + 1
        Next i
    Next j
End Sub

答案 2 :(得分:0)

好的,看一看。我可以给你一个建议。不是整个公式,只是VLOOKUP的一部分。

我知道这是你的循环中的公式:

Cells(i, mcol) = "=IF(RC[-1]=""C"",(RC[-3]/SUMIFS(C[-3],C66,RC66))*(VLOOKUP('Sheet1'!RC66,GA_C!C1:C4,4,0)),SUM((RC[-3]/SUMIFS(C[-3],C66,RC66))*(VLOOKUP('Sheet1'!RC66,GA_C!C1:C4,4,0)),(RC[-3]/SUMIFS(C[-3],C66,RC66,C[-1],""GA + C""))*(VLOOKUP('Sheet1'!RC66,GA_C!C1:C3,3,0))))"

现在您想根据循环动态更改列。我理解列对如下:

C1:C4 & C1:C3
C1:C5 & C1:C4
C1:C6 & C1:C5
C1:C7 & C1:C6
C1:C8 & C1:C7
C1:C9 & C1:C8
C1:C10 & C1:C9
C1:C11 & C1:C10
C1:C12 & C1:C11

实际上,你的循环不清楚,我无法使用它。所以,我使用如下:

For column = 3 To 11

    mcol = mcol + 1

    For row = 1 To lastRow

        Cells(row , mcol) = "=IF(RC[-1]=""C"",(RC[-3]/SUMIFS(C[-3],C66,RC66))*" & _
                            "(VLOOKUP('Sheet1'!RC66,GA_C!C1:C" & column + 1 & "," & column + 1 & ",0))" & _
                            ",SUM((RC[-3]/SUMIFS(C[-3],C66,RC66))*" & _
                            "(VLOOKUP('Sheet1'!RC66,GA_C!C1:C" & column + 1 & "," & column + 1 & ",0))" & _
                            ",(RC[-3]/SUMIFS(C[-3],C66,RC66,C[-1],""GA + C""))*" & _
                            "(VLOOKUP('Sheet1'!RC66,GA_C!C1:C" & column & "," & column & ",0))))"
    Next row

Next column

尝试上述方法,对您有所帮助。