用不同的行数循环?

时间:2018-08-27 14:51:29

标签: excel vba excel-vba

我有这段代码可以正常工作,但是范围是I2:I800,在某些工作表中,该行少于800行,因此excel将用“ 0”填充其余行。有时有800多条线。什么循环可以帮助我填写尽可能多的行?

With Worksheets("Sheet1").Range("I2:I800")
    .FormulaR1C1 = "=RC[-1] & TEXT(RC[-2],""00"")"
    .Value = .Value
End With
Worksheets("Sheet1").Columns("G:H").Delete

With Worksheets("Sheet2").Range("I2:I800")
    .FormulaR1C1 = "=RC[-1] & TEXT(RC[-2],""00"")"
    .Value = .Value
End With
Worksheets("Sheet2").Columns("G:H").Delete

1 个答案:

答案 0 :(得分:3)

无需循环就可以找到最后一行。

Dim LstRow as Long
LstRow = Worksheets("Sheet1").Cells(Rows.Count,8).End(xlup).Row

然后在您的引用中使用它

Dim LstRow as Long
LstRow = Worksheets("Sheet1").Cells(Rows.Count,8).End(xlup).Row
With Worksheets("Sheet1").Range("I2:I" & LstRow)
    .FormulaR1C1 = "=RC[-1] & TEXT(RC[-2],""00"")"
    .Value = .Value
End With
Worksheets("Sheet1").Columns("G:H").Delete

LstRow = Worksheets("Sheet2").Cells(Rows.Count,8).End(xlup).Row
With Worksheets("Sheet2").Range("I2:I" & LstRow)
    .FormulaR1C1 = "=RC[-1] & TEXT(RC[-2],""00"")"
    .Value = .Value
End With
Worksheets("Sheet2").Columns("G:H").Delete