
嗨,我有一个数据库,它有91个列,每个列都有从第8行到第21行(固定的)的行。91列包含部门标题,行内容依赖于部门标头。这些列以另一种方式进行,其中dept 1在C列,dept 2在E列,它遵循所有91列的模式。我想使用嵌套循环对给定的公式"=SUMIFS('L4 - Data Sheet'!$Q:$Q,'L4 - Data Sheet'!$R:$R,'WF - L4'!BY$5,'L4 - Data Sheet'!$P:$P,$A8,'L4 - Data Sheet'!$A:$A,'WF - L4'!$A$3)"进行自动化操作,但它似乎不起作用,我也不清楚如何实现它。因为硬编码91 did列单元格和每列各自的行范围是一项非常繁琐的工作:/这是我当前工作的努力代码。
Sub WFCorp4()
    With Sheets("WF - L4 (2)") '91 columns * 2  because there is two different subsectors qty and direct assigned space
        '.Range("BY8:BY21").Formula = "=SUMIFS('L4 - Data Sheet'!$Q:$Q,'L4 - Data Sheet'!$R:$R,'WF - L4'!BY$5,'L4 - Data Sheet'!$P:$P,$A8,'L4 - Data Sheet'!$A:$A,'WF - L4'!$A$3)"
        '.Range("CA8:CA21").Formula = "=SUMIFS('L4 - Data Sheet'!$Q:$Q,'L4 - Data Sheet'!$R:$R,'WF - L4'!CA$5,'L4 - Data Sheet'!$P:$P,$A8,'L4 - Data Sheet'!$A:$A,'WF - L4'!$A$3)"
        '.Range("CN8:CN21").Formula = "=SUMIFS('L4 - Data Sheet'!$U:$U,'L4 - Data Sheet'!$R:$R,'WF - L4'!CM$5,'L4 - Data Sheet'!$P:$P,$A8,'L4 - Data Sheet'!$A:$A,'WF - L4'!$A$3)"
    End With
End Sub这是我为工作表尝试过的嵌套循环代码。
Sub ShortcutWFCorp4()
Dim lastcol As Long
Dim lastrow As Long
lastcol = Cells(8, Columns.Count).End(xlToLeft).Column
lastrow = Cells(21, "C").End(xlUp).Row
Dim i As Long
Dim j As Long
For i = i + 2 To lastcol
    For j = 8 To lastrow
  Sheets("WF - L4 (2)").Cells(i, j).Range.FormulaR1C1 = "=SUMIFS('L4 - Data Sheet'!R4C17:R132C17,'L4 - Data Sheet'!R4C18:R132C18,'WF - L4 (2)'!i,'L4 - Data Sheet'!R4C16:R132C16,'L4 - Data Sheet'R4C1,R132C1,'WF - L4(2)'!R3C1)"
Next
Next
End Sub如果有人能帮忙就好了!谢谢
附件是它的大致样子的图像。每个细胞都有我问题中所述的公式。我想使用嵌套循环自动在qty列中列出输出!各部门按另一种顺序进行。
发布于 2016-04-14 03:51:40
我想我从你提供的公式中破译了你的公式。嵌套循环没有必要使用精心设计的公式,只需将其同时放入所有单元格中,就像填充向下一样。
Option Explicit
Sub ShortcutWFCorp4()
    Dim i As Long, j As Long
    'cannot use lastrow until there is something in column C
    Dim lastCol As Long ', lastRow As Long
    With Worksheets("WF - L4 (2)")  '<~~ define it ONCE here
        'you want to put formulas into all the columns with QTY in row 7
        lastCol = .Cells(7, Columns.Count).End(xlToLeft).Column
        'I guess column C is the left-most in your image but you canot
        'get last row list this until there is something in it
        'I'll use 21
        'lastrow = .Cells(21, "C").End(xlUp).Row
        'columns 3, 5, 7, etc
        For i = 3 To lastCol Step 2
            '"=SUMIFS('L4 - Data Sheet'!$Q:$Q,
            '         'L4 - Data Sheet'!$R:$R, 'WF - L4'!BY$5,
            '         'L4 - Data Sheet'!$P:$P, $A8,
            '         'L4 - Data Sheet'!$A:$A, 'WF - L4'!$A$3)
            .Range(.Cells(8, i), .Cells(21, i)).FormulaR1C1 = _
                        "=SUMIFS('L4 - Data Sheet'!C17, " & _
                                "'L4 - Data Sheet'!C18, R5C" & 74 + i & ", " & _
                                "'L4 - Data Sheet'!C16, RC1, " & _
                                "'L4 - Data Sheet'!C1, R3C1)"
        Next i
    End With
End Sub我猜第一个Qty是在C7。您说公式将进入第8-21行。如果C列下到第21行没有什么东西,那么查找“最后一行”就没有意义了,所以我硬编码了8到21行。
https://stackoverflow.com/questions/36612751
复制相似问题