简体   繁体   中英

VBA, Calculation for multiple worksheets/workbook

I have the following Macro:

Sub PercentCalc()

Dim xrng As Range, lrw As Long, lrng As Range, i As Long

With Application
    .ScreenUpdating = False
    .Calculation = xlCalculationManual
End With

For i = 1 To 25
    With Columns(i)
        .TextToColumns Destination:=.Cells(1, 1), DataType:=xlDelimited, TrailingMinusNumbers:=True
    End With
Next

lrw = Columns("A:Y").Find("*", , xlValues, , xlRows, xlPrevious).Row
Set lrng = Range("A" & lrw + 2)

With Range("A2:A" & lrw)
    lrng.Formula = "=COUNTA(" & .Address(0, 0) & ")/ROWS(" & .Address(0, 0) & ")"
End With

Set xrng = Range(lrng, Cells(lrng.Row, "Y"))

lrng.AutoFill xrng, Type:=xlFillDefault
xrng.Style = "Percent"

With Application
    .ScreenUpdating = True
    .Calculation = xlCalculationAutomatic
End With 
End Sub

How can I apply this across multiple worksheets in a workbook? Or multiple workbooks with multiple worksheets? I have a macro to open all Excel files in my directory. Even better if I could bypass opening all Excel files.

Pretty much want to automate this macro within a large amount of files/sheets.

You can't calculate without opening all the workbooks, but there is a simple command for what you are looking for: Application.CalculateFull . It re-calculates all sheets in all open workbooks. Be aware that this may take a long time and may make Excel seem like it is not responding until it finishes. In addition, if the open sheets are in a different instance of Excel from your macro above, they will not calculate.

So I would imagine your process to look like this:

  • Run your macro to open all the files
  • Run your macro above, with .CalculateFull just after .Calculation = xlCalculationAutomatic and just before End With , End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM