2017-06-22 28 views
0

我正在嘗試在工作簿的每個工作表上執行一些簡單的計算。但是,計算不會在所有工作表上更新。您能否幫助我將計算結果顯示在工作簿的所有表格中?方程式不使用Excel VBA在工作簿中的所有工作表上更新

這是我到目前爲止有:

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

更新了我的問題......對不起,它只是VBA – Joe

回答

1

內部For循環,Activate如下圖所示每個工作表。如果您沒有激活工作表,第一張表將始終保持激活狀態,變量'LastRow'中包含的值將不會是您想要的值(從第2次迭代開始),因爲它使用了活動表格。

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 
相關問題