自动填充到工作表中间的工作表结束

时间:2018-03-02 17:49:57

标签: excel vba excel-vba

我正在尝试创建一个曾经使用的模板可以有任意数量的行。我遇到的最大问题是我的数据直到A32才开始。此行上方有信息但与宏无关。宏将进入公式,我需要它自动填充到最后一行。当我从测试表中的第1行开始时,我可以让它工作,但是当移动到第32行时没那么多。

Range("U32").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-13]=""CM"", -RC[-8],RC[-8])"
Range("V32").Select
ActiveCell.FormulaR1C1 = _
  "=IFERROR(VLOOKUP(RC[-13],Sheet1!R80C7:R1048576C7,1,FALSE),"""")"
Range("W32").Select
ActiveCell.FormulaR1C1 = _
  ="=IFERROR(VLOOKUP(RC[-14],Sheet2!R80C7:R1048576C7,1,FALSE),"""")"
Range("X32").Select
ActiveCell.FormulaR1C1 = "=IF(RC[1]<>"""", CONCATENATE('Recon _
  Form'!R2C23,'Recon Form'!R4C23,'Recon Form'!R3C23,'Recon _
  Form'!R5C23,RC[1]),"""")"
Range("Y32").Select
ActiveCell.FormulaR1C1 = "=IF(RC[-16]="""","""",IF(RC[-3]<>"""",""Sheet1 "", 
IF(RC[-2]<>"""",""Sheet2 "", IF(RC[1]<>"""",""Sheet4 "", _
  IF(RC[2]<>"""",""Sheet3 "", ""REVIEW"")))))"
Range("Z32").Select
ActiveCell.FormulaR1C1 = 
"=IFERROR(VLOOKUP(RC[-17],Sheet4!R159C4:R1048576C4,1,FALSE),"""")"
Range("AA32").Select
ActiveCell.FormulaR1C1 = _
  "=IFERROR(VLOOKUP(RC[-18],Sheet3!R159C4:R1048576C4,1,FALSE),"""")"
Range("A32:AA32").AutoFill Destination:=Range("A32:AA" & _
  Cells(Rows.Count, "A").End(xlUp).Row)`

1 个答案:

答案 0 :(得分:0)

摆脱所有选择,使用R1C1,不需要填写,只需直接分配。

Dim LRow As Long
With ActiveSheet
    LRow = .Cells(.Rows.Count, "A").End(xlUp).Row

    .Range(.Cells(32, 21), .Cells(LRow, 21)).FormulaR1C1 = _
        "=IF(RC[-13]=""CM"", -RC[-8],RC[-8])"

    .Range(.Cells(32, 22), .Cells(LRow, 22)).FormulaR1C1 = _
        "=IFERROR(VLOOKUP(RC[-13],Sheet1!R80C7:R1048576C7,1,FALSE),"""")"

    .Range(.Cells(32, 23), .Cells(LRow, 23)).FormulaR1C1 = _
        "=IFERROR(VLOOKUP(RC[-14],Sheet2!R80C7:R1048576C7,1,FALSE),"""")"

    .Range(.Cells(32, 24), .Cells(LRow, 24)).FormulaR1C1 = _
        "=IF(RC[1]<>"""", CONCATENATE('Recon Form '!R2C23,'Recon Form'!R4C23," & _
        "'Recon Form'!R3C23,'Recon Form '!R5C23,RC[1]),"""")"

    .Range(.Cells(32, 25), .Cells(LRow, 25)).FormulaR1C1 = _
        "=IF(RC[-16]="""","""",IF(RC[-3]<>"""",""Sheet1 "", IF(RC[-2]<>""""," & _
        """Sheet2 "", IF(RC[1]<>"""",""Sheet4 "", IF(RC[2]<>"""",""Sheet3 "", ""REVIEW"")))))"

    .Range(.Cells(32, 26), .Cells(LRow, 26)).FormulaR1C1 = _
        "=IFERROR(VLOOKUP(RC[-17],Sheet4!R159C4:R1048576C4,1,FALSE),"""")"

    .Range(.Cells(32, 27), .Cells(LRow, 27)).FormulaR1C1 = _
        "=IFERROR(VLOOKUP(RC[-18],Sheet3!R159C4:R1048576C4,1,FALSE),"""")"

    .Range("A32:T32").AutoFill Destination:=.Range("A32:T" & LRow)
End With