嗨,我是VBA的新手,所以如果我的代码看起来不好,我会提前道歉。我的问题是,当我使用按钮运行宏时,我想阻止屏幕闪烁。但是,我尝试了多种代码来解决此问题,但无济于事。我在子菜单的开头尝试过Application.ScreenUpdating = False
,并在结束子菜单之前切换回True,但是屏幕仍然闪烁。还有其他解决方案吗?预先感谢!
代码:
Sub UpdateData()
Application.ScreenUpdating = False
Debug.Print Application.ScreenUpdating
Application.DisplayStatusBar = False
Application.Calculation = xlManual
Application.EnableEvents = False
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim ws1 As Worksheet
Dim EMS As Worksheet
Dim TD As Worksheet
Dim JV1 As Worksheet
Set wb1 = ActiveWorkbook
Workbooks.Open ("HR Headcount Report 2018 Australia SEPTEMBER.XLSX")
Set wb2 = ActiveWorkbook
Set EMS = Sheets("Employee Movement Summary")
EMS.Activate
Range("J19").Copy
wb1.Activate
Range("J34").PasteSpecial xlPasteValues
wb2.Activate
Set TD = Sheets("Turnover Dashboard")
TD.Activate
Range("J44").Copy
wb1.Activate
Range("J2").PasteSpecial xlPasteValues
wb2.Activate
Range("J47").Copy
wb1.Activate
Range("J3").PasteSpecial xlPasteValues
P.S。这是我的代码的前半部分。我怀疑多次使用.Activate
导致了问题。如果是这种情况,我该如何重写代码以避免使用.Activate
?
答案 0 :(得分:1)
下面的代码将指导您删除所有的Activate语句:
Sub UpdateData()
Application.ScreenUpdating = False
Debug.Print Application.ScreenUpdating
Application.DisplayStatusBar = False
Application.Calculation = xlManual
Application.EnableEvents = False
Dim wb1 As Workbook
Dim wb2 As Workbook
Dim ws1 As Worksheet
Dim EMS As Worksheet
Dim TD As Worksheet
Dim JV1 As Worksheet
Set wb1 = ThisWorkbook
Set ws1 = ActiveSheet
'better to refer to the actual sheet name rather than the Active Sheet, amend as required
Set wb2 = Workbooks.Open("HR Headcount Report 2018 Australia SEPTEMBER.XLSX")
Set EMS = wb2.Sheets("Employee Movement Summary")
EMS.Range("J19").Copy
ws1.Range("J34").PasteSpecial xlPasteValues
Set TD = wb2.Sheets("Turnover Dashboard")
TD.Range("J44").Copy
ws1.Range("J2").PasteSpecial xlPasteValues
TD.Range("J47").Copy
ws1.Range("J3").PasteSpecial xlPasteValues