Where to place Application.ScreenUpdating function to stop flicker

redspanna

Well-known Member
Joined
Jul 27, 2005
Messages
1,604
Office Version
  1. 365
Platform
  1. Windows
No matter where I put the Application.Screenupdating function in this code, it still flickers when running,

Where should it be placed to avoid the 'flicker'

VBA Code:
Sub CopyEbayData()

   
    Dim sourceWorkbook As Workbook
    Dim targetWorkbook As Workbook
    Dim sourceWorksheet As Worksheet
    Dim copiedWorksheet As Worksheet
   
   Application.ScreenUpdating = False
   
    ' Set the source workbook (workbook with "ebay-orders" in its name)
    Set sourceWorkbook = GetWorkbookWithMatchingName("ebay-Listings")
    
    ' Check if the source workbook was found
    If Not sourceWorkbook Is Nothing Then
        ' Set the source worksheet (the only worksheet in the source workbook)
        Set sourceWorksheet = sourceWorkbook.Worksheets(1)
        
        ' Set the target workbook (workbook named "Sales4")
        Set targetWorkbook = ThisWorkbook ' Assuming this code is in the Sales4 workbook
        
        ' Copy the source worksheet to the target workbook
        sourceWorksheet.Copy After:=targetWorkbook.Sheets(targetWorkbook.Sheets.Count)
        
        ' Set the copied worksheet object reference
        Set copiedWorksheet = targetWorkbook.Sheets(targetWorkbook.Sheets.Count)
        
        ' Rename the copied worksheet to "ebay"
        copiedWorksheet.Name = "ebay"
        
         
        ' Clean up
        Set sourceWorksheet = Nothing
        sourceWorkbook.Close False ' Close the source workbook without saving changes
        Set sourceWorkbook = Nothing
        
        'run code to add VLOOKUP formula
        'If_string_match_found_place_formula
        'run code to change neagative numbers to positive
        'ChangeNegativeValues
        
  Application.ScreenUpdating = True
        
    Else
        MsgBox "Source workbook not found."
        
       
    End If
    
End Sub

Thanks in advance
 
It isn't a brief flicker, the updates last for a few seconds which I really don't want to have to see
any ideas where to insert the Application.screenupdating to stop it?

So a couple of things:

1. In terms of 'where do I place it', you have to first identify where in the code the flicker occurs. I usually do this by placing temporary End statements, first near the beginning of the code and then running the macro, and then moving End to a new location, depending on what I find out about where the flicker happens.

2. If is lasting a few seconds, you may also need to place some DoEvents statements just before the code that causes the flicker.

There is no one single answer. You have to experiment.
 
Upvote 0

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
I sometimes use an API based alternative to the native Excel ScreenUpdating Property. Not sure if this will help but you can give it a shot.

In a Standard Module:
VBA Code:
Option Explicit

#If VBA7 Then
    Private Declare PtrSafe Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As LongPtr, ByVal hWnd2 As LongPtr, ByVal lpsz1 As String, ByVal lpsz2 As String) As LongPtr
    Private Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As LongPtr, ByVal wMsg As Long, ByVal wParam As LongPtr, lParam As Any) As LongPtr
#Else
    Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long, ByVal hWnd2 As Long, ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
#End If

Public Property Let ScreenUpdatingAPI(ByVal vNewValue As Boolean)
    Const WM_SETREDRAW = &HB
    #If Win64 Then
        Dim hwnd As LongLong
    #Else
        Dim hwnd As Long
    #End If
    hwnd = FindWindowEx(Application.hwnd, 0, "XLDESK", vbNullString)
    hwnd = FindWindowEx(hwnd, 0, "EXCEL7", vbNullString)
    Call SendMessage(hwnd, ByVal WM_SETREDRAW, ByVal vNewValue, 0)
    If vNewValue Then Application.ScreenUpdating = True
End Property


Code Usage example:
VBA Code:
Sub Test()

    ScreenUpdatingAPI = False
  
        ' run your code here ...
  
    ScreenUpdatingAPI = True

End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top