有人可以让我知道如何在一行中编写以下两组代码(每组)?
newb.sheets("Sheet1").Cells.EntireColumn.AutoFit
newb.sheets("Sheet2").Cells.EntireColumn.AutoFit
newb.sheets("Sheet3").Cells.EntireColumn.AutoFit
newb.sheets("Sheet4").Cells.EntireColumn.AutoFit
LastRow1 = sheets("Sheet1").Range("A1000000").End(xlUp).Row
LastRow2 = sheets("Sheet2").Range("A1000000").End(xlUp).Row
LastRow3 = sheets("Sheet3").Range("A1000000").End(xlUp).Row
LastRow4 = sheets("Sheet4").Range("A1000000").End(xlUp).Row
答案 0 :(得分:2)
你也可以这样做:
Dim LastRow() As Long
Dim i As Integer
For i = 1 To ThisWorkbook.Worksheets.Count
With ThisWorkbook.Worksheets(i)
ReDim Preserve LastRow(i-1)
LastRow(i-1) = .Range("A" & .Rows.Count).End(xlUp).Row
.Cells.EntireColumn.AutoFit
End With
Next i
答案 1 :(得分:1)
简单来说,你可以使用For Loop
For i = 1 To 4
newb.sheets("Sheet" & i).Cells.EntireColumn.AutoFit
LastRow1 = sheets("Sheet" & i).Range("A1000000").End(xlUp).Row
Next i
此处,4
表示张数
另外,您可以使用 ThisWorkbook.Worksheets.Count
For i = 1 To ThisWorkbook.Worksheets.Count
newb.sheets("Sheet" & i).Cells.EntireColumn.AutoFit
LastRow & i = sheets("Sheet" & i).Range("A1000000").End(xlUp).Row
Next i