我正在尝试对工作簿的每个工作表进行一些简单的计算。但是,计算不会在所有工作表上更新。你能帮我把计算出现在工作簿的所有工作表上吗?
这是我到目前为止所做的:
Sub SearchFolders()
'UpdatebySUPERtoolsforExcel2016
Dim xOut As Worksheet
Dim xWb As Workbook
Dim xWks As Worksheet
Dim xRow As Long
Dim xFound As Range
Dim xStrAddress As String
Dim xFileDialog As FileDialog
Dim xUpdate As Boolean
Dim xCount As Long
Application.ScreenUpdating = False
Set xWb = ActiveWorkbook
For Each xWks In xWb.Sheets
xRow = 1
With xWks
.Cells(xRow, 12) = "Meas-LO"
.Cells(xRow, 13) = "Meas-Hi"
.Cells(xRow, 14) = "Marginal"
LastRow = ActiveSheet.UsedRange.Rows.Count
Range("L2").Formula = "=G2+I2"
Range("L2").AutoFill Destination:=Range("L2:L" & LastRow)
Range("M2").Formula = "=I2-F2"
Range("M2").AutoFill Destination:=Range("M2:M" & LastRow)
End With
Application.ScreenUpdating = True 'turn it back on
Next xWks
End Sub
答案 0 :(得分:1)
在For循环内,Activate
每个工作表如下所示。如果您没有激活工作表,第一张工作表将始终保持激活状态,并且变量“LastRow”中包含的值将不是您想要的值(从第二次迭代开始),因为它使用了活动表。
Sub SearchFolders()
'UpdatebySUPERtoolsforExcel2016
Dim xOut As Worksheet
Dim xWb As Workbook
Dim xWks As Worksheet
Dim xRow As Long
Dim xFound As Range
Dim xStrAddress As String
Dim xFileDialog As FileDialog
Dim xUpdate As Boolean
Dim xCount As Long
Application.ScreenUpdating = False
Set xWb = ActiveWorkbook
For Each xWks In xWb.Sheets
xRow = 1
With xWks
.Activate 'Activating the worksheet
.Cells(xRow, 12) = "Meas-LO"
.Cells(xRow, 13) = "Meas-Hi"
.Cells(xRow, 14) = "Marginal"
LastRow = ActiveSheet.UsedRange.Rows.Count
Range("L2").Formula = "=G2+I2"
Range("L2").AutoFill Destination:=Range("L2:L" & LastRow)
Range("M2").Formula = "=I2-F2"
Range("M2").AutoFill Destination:=Range("M2:M" & LastRow)
End With
Application.ScreenUpdating = True 'turn it back on
Next xWks
End Sub