Excel VBA:加快设置列宽和视图设置的宏

我的团队和我有一个excel文件,其中使用了A到L列。我们使用此文件发送不同的预算,因此具有该文件的多个版本。发生的事情是人们开始调整列宽,然后打印区域不再适合。

我们希望始终确保标准版式。

我设置了一个VBA,以确保在打开文件时实现预选的列宽。如果有人更改了宽度,则当有人再次打开文件时,该宽度将自动调整。

我的代码工作正常,但是,一个问题是运行2分30秒需要很长时间。关于如何加快速度的任何建议?

Sub Workbook_Open()

'-----START TIMER-----
Dim StartTime As Double
Dim Timetaken As String
Dim ws As Worksheet

StartTime = Timer

    Application.ScreenUpdating = True
    Application.EnableEvents = True
    Application.AskToUpdateLinks = True
    Application.DisplayAlerts = True
    Application.Calculation = xlAutomatic
    ThisWorkbook.Date1904 = False
    Application.StatusBar = False

On Error Resume Next

'For Each ws In activeWorkbook.Worksheets ' Start of the VBA loop
For Each ws In Worksheets ' Start of the VBA loop
    With ws
    ws.activate 'this part ensures each seperate tab is activated and the below code is run through
    Columns("A").ColumnWidth = 0.94 'this line determines the column width
    Columns("B").ColumnWidth = 6.56 'this line determines the column width
    Columns("C").ColumnWidth = 13.56
    Columns("D").ColumnWidth = 13.56
    Columns("E").ColumnWidth = 13.56
    Columns("F").ColxumnWidth = 10.11
    Columns("G").ColumnWidth = 6.11
    Columns("H").ColumnWidth = 10.11
    Columns("I").ColumnWidth = 10.11
    Columns("J").ColumnWidth = 13.56
    Columns("K").ColumnWidth = 6.56
    Columns("L").ColumnWidth = 6.56
    Wsh.Range("A1").Select 'this part ensure each worksheet view start position is A1
    activeWindow.View = xlPageBreakPreview 'Set activesheet to Page Break Preview Mode
    activeWindow.zoom = 114 'this line sets the permanent zoom % for all tabs
    activeWindow.ScrollColumn = 1
    activeWindow.ScrollRow = 1
    End With

Next ws
Application.Goto ThisWorkbook.Sheets("resume").Range("A1"),True 'starting position upon opening the file
'Worksheets(1).activate 'this line make sure view is at first tab

    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Application.AskToUpdateLinks = False
    Application.DisplayAlerts = False
    Application.Calculation = xlAutomatic
    ThisWorkbook.Date1904 = False
    activeWindow.View = xlNormalView

'------ END TIMER------
Timetaken = Format((Timer - StartTime) / 86400,"hh:mm:ss")
MsgBox "Running time was " & Timetaken & " (hours,minutes,seconds)"

End Sub
sxdtzzf 回答:Excel VBA:加快设置列宽和视图设置的宏

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3158953.html

大家都在问