Flickering when copying from one workbook to another VBA

nikimise

New Member
Joined
Jan 24, 2023
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hello,
I need help with elimination of flickering in the sub below, application.Screenupadting does not work as it seems to be reseted each time the sub CopyManyRanges is called. Purpose of the code is to copy certain values from one workbook to another based on certain condition(always copying to the last row). Once the values are copied, blank rows need to be deleted as they are added if certain YC is not copied as it is already existing in planning.(If I dont write for each YC lDestLastRow+x, then all values are just overwriting themselves and instead of 4 rows I get only 1row with last values.... I'm new to VBA and don't know if this can be handled on a different way... )
After the blank rows are removed a filter and sorting is applied.

In case the cod is run when the Planning is closed and all 4YC need to be copied as none is existing in the planning then there is no flickering. If for example 2YC are already existing and 2 still need to be added then the flickering appears. Flickering also appears if Planning is opened. Each YC has a unique number based on which it is determined if it is already existing in the planning.
Thank you

VBA Code:
Sub Send_to_Planning()
'Find the last used row in both sheets and copy and paste data below existing data.
Application.ScreenUpdating = False
Dim wsCopy As Worksheet
Dim wsDest As Worksheet
Dim lCopyLastRow As Long
Dim lDestLastRow As Long
Dim YCPath, YCNew, Planning As String


YCPath = ThisWorkbook.FullName
YCNww = ThisWorkbook.Name
Planning = "C:\Users\Desktop\YC_Planning"
  'Set variables for copy and destination sheets

Set wsDest = Workbooks.Open(Planning).Worksheets("Plan")
Set wsCopy = Workbooks(YCNew).Worksheets("New 3")
Workbooks(YCNew).Worksheets("New 3").Activate
Application.ScreenUpdating = False
  '2. Find first blank row in the destination range based on data in column A
  'Offset property moves down 1 row
  
  lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Offset(1).Row

'  3. Copy & Paste Data + Check if YC exists
    'YC1
If Range("E8") <> "" And Range("AP56").Value = 1 Then
    MsgBox "1 YC_ID already exists in planning)"
    Application.ScreenUpdating = False
    
ElseIf Range("E8").Value <> 0 And Range("AP56").Value = 0 Then
wsCopy.Range("C11").Copy
wsDest.Range("A" & lDestLastRow).PasteSpecial Paste:=xlPasteValues

    Call CopyManyRanges("C11", "A" & lDestLastRow, wsCopy, wsDest) 'unique number
    Call CopyManyRanges("D23", "B" & lDestLastRow, wsCopy, wsDest) 
    Call CopyManyRanges("G22", "C" & lDestLastRow, wsCopy, wsDest) 
    Call CopyManyRanges("F7", "D" & lDestLastRow, wsCopy, wsDest)
End If

    'YC2
If Range("O8") <> "" And Range("AQ56").Value = 1 Then
    MsgBox "2 YC_ID already exists in planning)"
ElseIf Range("O8") <> "" And Range("AQ56").Value = 0 Then
    Call CopyManyRanges("M11", "A" & lDestLastRow + 1, wsCopy, wsDest) 'unique number
    Call CopyManyRanges("N23", "B" & lDestLastRow + 1, wsCopy, wsDest) 
    Call CopyManyRanges("Q22", "C" & lDestLastRow + 1, wsCopy, wsDest) 
    Call CopyManyRanges("P7", "D" & lDestLastRow + 1, wsCopy, wsDest) 
End If

    'YC3
If Range("Y8") <> "" And Range("AR56").Value = 1 Then
    MsgBox "3 YC_ID already exists in planning)"
ElseIf Range("Y8") <> "" And Range("AR56").Value = 0 Then
    Call CopyManyRanges("W11", "A" & lDestLastRow + 2, wsCopy, wsDest) 'unique number
    Call CopyManyRanges("X23", "B" & lDestLastRow + 2, wsCopy, wsDest) '
    Call CopyManyRanges("AA22", "C" & lDestLastRow + 2, wsCopy, wsDest)
    Call CopyManyRanges("Z7", "D" & lDestLastRow + 2, wsCopy, wsDest)
End If

    'YC4
If Range("AI8") <> "" And Range("AS56").Value = 1 Then
    MsgBox "4 YC_ID already exists in planning)"
ElseIf Range("AI8") <> "" And Range("AS56").Value = 0 Then
    Call CopyManyRanges("AG11", "A" & lDestLastRow + 3, wsCopy, wsDest) 'unique number
    Call CopyManyRanges("AH23", "B" & lDestLastRow + 3, wsCopy, wsDest)
    Call CopyManyRanges("AK22", "C" & lDestLastRow + 3, wsCopy, wsDest)
    Call CopyManyRanges("AJ7", "D" & lDestLastRow + 3, wsCopy, wsDest)
    End If

     'to delete blank rows in Planning
Workbooks("YC_Planning.xlsm").Activate
    Sheets("BREAKERS").Select
    Range("a2:A15000").Select
    On Error Resume Next
    Selection.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    On Error GoTo 0
If Worksheets("BREAKERS").AutoFilterMode = False Then
            Worksheets("BREAKERS").Rows(1).Select
    Worksheets("BREAKERS").Range("A1:E1").AutoFilter Field:=5, Criteria1:=""
    ElseIf Worksheets("BREAKERS").AutoFilterMode = True Then
    Worksheets("BREAKERS").Rows(1).Select
    Worksheets("BREAKERS").Range("A1:E1").AutoFilter Field:=5, Criteria1:=""
    End If

 Range("A2:D150000", Range("A2:D150000").End(xlDown)).Sort Key1:=Range("D2"), Order1:=xlAscending, Header:=xlYes
Application.ScreenUpdating = True
Workbooks("YC_Planning.xlsm").Save
End Sub
[CODE=vba]
Sub CopyManyRanges(Range_Orig As String, Range_Dest As String, wsCopy As Worksheet, wsDest As Worksheet)
wsCopy.Range(Range_Orig).Copy
wsDest.Range(Range_Dest).PasteSpecial Paste:=xlPasteValues
End Sub



[/CODE]
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
As I cannot edit, here is the correct code:
VBA Code:
Sub Send_to_Planning()
'Find the last used row in both sheets and copy and paste data below existing data.
Application.ScreenUpdating = False
Dim wsCopy As Worksheet
Dim wsDest As Worksheet
Dim lCopyLastRow As Long
Dim lDestLastRow As Long
Dim YCPath, YCNew, Planning As String


YCPath = ThisWorkbook.FullName
YCNww = ThisWorkbook.Name
Planning = "C:\Users\Desktop\YC_Planning"
 'Set variables for copy and destination sheets

Set wsDest = Workbooks.Open(Planning).Worksheets("Plan")
Set wsCopy = Workbooks(YCNew).Worksheets("New 3")
Workbooks(YCNew).Worksheets("New 3").Activate
Application.ScreenUpdating = False
'2. Find first blank row in the destination range based on data in column A
'Offset property moves down 1 row
  
lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Offset(1).Row

'  3. Copy & Paste Data + Check if YC exists
'YC1
If Range("E8") <> "" And Range("AP56").Value = 1 Then
MsgBox "1 YC_ID already exists in planning)"
Application.ScreenUpdating = False
    
ElseIf Range("E8").Value <> 0 And Range("AP56").Value = 0 Then
wsCopy.Range("C11").Copy
wsDest.Range("A" & lDestLastRow).PasteSpecial Paste:=xlPasteValues

Call CopyManyRanges("C11", "A" & lDestLastRow, wsCopy, wsDest) 'unique number
Call CopyManyRanges("D23", "B" & lDestLastRow, wsCopy, wsDest)
Call CopyManyRanges("G22", "C" & lDestLastRow, wsCopy, wsDest)
Call CopyManyRanges("F7", "D" & lDestLastRow, wsCopy, wsDest)
End If

'YC2
If Range("O8") <> "" And Range("AQ56").Value = 1 Then
MsgBox "2 YC_ID already exists in planning)"
ElseIf Range("O8") <> "" And Range("AQ56").Value = 0 Then
Call CopyManyRanges("M11", "A" & lDestLastRow + 1, wsCopy, wsDest) 'unique number
Call CopyManyRanges("N23", "B" & lDestLastRow + 1, wsCopy, wsDest)
Call CopyManyRanges("Q22", "C" & lDestLastRow + 1, wsCopy, wsDest)
Call CopyManyRanges("P7", "D" & lDestLastRow + 1, wsCopy, wsDest)
End If

'YC3
If Range("Y8") <> "" And Range("AR56").Value = 1 Then
MsgBox "3 YC_ID already exists in planning)"
ElseIf Range("Y8") <> "" And Range("AR56").Value = 0 Then
Call CopyManyRanges("W11", "A" & lDestLastRow + 2, wsCopy, wsDest) 'unique number
Call CopyManyRanges("X23", "B" & lDestLastRow + 2, wsCopy, wsDest) '
Call CopyManyRanges("AA22", "C" & lDestLastRow + 2, wsCopy, wsDest)
Call CopyManyRanges("Z7", "D" & lDestLastRow + 2, wsCopy, wsDest)
End If

'YC4
If Range("AI8") <> "" And Range("AS56").Value = 1 Then
MsgBox "4 YC_ID already exists in planning)"
ElseIf Range("AI8") <> "" And Range("AS56").Value = 0 Then
Call CopyManyRanges("AG11", "A" & lDestLastRow + 3, wsCopy, wsDest) 'unique number
Call CopyManyRanges("AH23", "B" & lDestLastRow + 3, wsCopy, wsDest)
Call CopyManyRanges("AK22", "C" & lDestLastRow + 3, wsCopy, wsDest)
Call CopyManyRanges("AJ7", "D" & lDestLastRow + 3, wsCopy, wsDest)
End If

'to delete blank rows in Planning
Workbooks("YC_Planning.xlsm").Activate
Sheets("Plan").Select
Range("a2:A15000").Select
On Error Resume Next
Selection.SpecialCells(xlCellTypeBlanks).EntireRow.Delete
On Error GoTo 0
If Worksheets("Plan").AutoFilterMode = False Then
Worksheets("Plan").Rows(1).Select
Worksheets("Plan").Range("A1:E1").AutoFilter Field:=5, Criteria1:=""
ElseIf Worksheets("Plan").AutoFilterMode = True Then
Worksheets("Plan").Rows(1).Select
Worksheets("Plan").Range("A1:E1").AutoFilter Field:=5, Criteria1:=""
End If

Range("A2:D150000", Range("A2:D150000").End(xlDown)).Sort Key1:=Range("D2"), Order1:=xlAscending, Header:=xlYes
Application.ScreenUpdating = True
Workbooks("YC_Planning.xlsm").Save
End Sub

Sub CopyManyRanges(Range_Orig As String, Range_Dest As String, wsCopy As Worksheet, wsDest As Worksheet)
wsCopy.Range(Range_Orig).Copy
wsDest.Range(Range_Dest).PasteSpecial Paste:=xlPasteValues
End Sub
 
Upvote 0
What was the edit that fixed it?
A bit hard looking at the phone.
Mine does the same thing
 
Upvote 0
No solution yet .
I made a mistake when copying the code in the first post, so i made a second post to paste the correct one.
 
Upvote 0
On a copy of can you see if this makes any difference.
(and make sure I haven't broken anything)
Mostly just removed selects and activates.
I have not touched your copy sub so haven't included it here.

VBA Code:
Sub Send_to_Planning_v03()
'Find the last used row in both sheets and copy and paste data below existing data.
Application.ScreenUpdating = False
Dim wsCopy As Worksheet
Dim wsDest As Worksheet
Dim lCopyLastRow As Long
Dim lDestLastRow As Long
Dim YCPath, YCNew, Planning As String

YCPath = ThisWorkbook.FullName
YCNew = ThisWorkbook.Name
Planning = "C:\Users\Desktop\YC_Planning"      ' XXXX
 
'Set variables for copy and destination sheets
Application.ScreenUpdating = False
Set wsDest = Workbooks.Open(Planning).Worksheets("Plan")
Set wsCopy = Workbooks(YCNew).Worksheets("New 3")

With wsCopy
    '2. Find first blank row in the destination range based on data in column A
    'Offset property moves down 1 row
     
    lDestLastRow = wsDest.Cells(wsDest.Rows.Count, "A").End(xlUp).Offset(1).Row
   
    '  3. Copy & Paste Data + Check if YC exists
    'YC1
    If .Range("E8") <> "" And .Range("AP56").Value = 1 Then
        MsgBox "1 YC_ID already exists in planning)"
    ElseIf .Range("E8").Value <> 0 And .Range("AP56").Value = 0 Then
        wsCopy.Range("C11").Copy
        wsDest.Range("A" & lDestLastRow).PasteSpecial Paste:=xlPasteValues
       
        Call CopyManyRanges("C11", "A" & lDestLastRow, wsCopy, wsDest) 'unique number
        Call CopyManyRanges("D23", "B" & lDestLastRow, wsCopy, wsDest)
        Call CopyManyRanges("G22", "C" & lDestLastRow, wsCopy, wsDest)
        Call CopyManyRanges("F7", "D" & lDestLastRow, wsCopy, wsDest)
    End If
   
    'YC2
    If .Range("O8") <> "" And .Range("AQ56").Value = 1 Then
        MsgBox "2 YC_ID already exists in planning)"
    ElseIf .Range("O8") <> "" And .Range("AQ56").Value = 0 Then
        Call CopyManyRanges("M11", "A" & lDestLastRow + 1, wsCopy, wsDest) 'unique number
        Call CopyManyRanges("N23", "B" & lDestLastRow + 1, wsCopy, wsDest)
        Call CopyManyRanges("Q22", "C" & lDestLastRow + 1, wsCopy, wsDest)
        Call CopyManyRanges("P7", "D" & lDestLastRow + 1, wsCopy, wsDest)
    End If
   
    'YC3
    If .Range("Y8") <> "" And .Range("AR56").Value = 1 Then
        MsgBox "3 YC_ID already exists in planning)"
    ElseIf .Range("Y8") <> "" And .Range("AR56").Value = 0 Then
        Call CopyManyRanges("W11", "A" & lDestLastRow + 2, wsCopy, wsDest) 'unique number
        Call CopyManyRanges("X23", "B" & lDestLastRow + 2, wsCopy, wsDest) '
        Call CopyManyRanges("AA22", "C" & lDestLastRow + 2, wsCopy, wsDest)
        Call CopyManyRanges("Z7", "D" & lDestLastRow + 2, wsCopy, wsDest)
    End If
   
    'YC4
    If .Range("AI8") <> "" And .Range("AS56").Value = 1 Then
        MsgBox "4 YC_ID already exists in planning)"
    ElseIf .Range("AI8") <> "" And .Range("AS56").Value = 0 Then
        Call CopyManyRanges("AG11", "A" & lDestLastRow + 3, wsCopy, wsDest) 'unique number
        Call CopyManyRanges("AH23", "B" & lDestLastRow + 3, wsCopy, wsDest)
        Call CopyManyRanges("AK22", "C" & lDestLastRow + 3, wsCopy, wsDest)
        Call CopyManyRanges("AJ7", "D" & lDestLastRow + 3, wsCopy, wsDest)
    End If
End With

'to delete blank rows in Planning
Dim wbYCPlan As Workbook
Dim wsPlan As Worksheet
 
Set wbYCPlan = Workbooks("YC_Planning.xlsm")
Set wsPlan = Worksheets("Plan")

With wsPlan
    On Error Resume Next
    .Range("a2:A15000").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    On Error GoTo 0
    If .AutoFilterMode = False Then
        .Range("A1:E1").AutoFilter Field:=5, Criteria1:=""
    ElseIf Worksheets("Plan").AutoFilterMode = True Then
        .Range("A1:E1").AutoFilter Field:=5, Criteria1:=""
    End If
   
    .Range("A2:D150000", .Range("A2:D150000").End(xlDown)).Sort Key1:=.Range("D2"), Order1:=xlAscending, Header:=xlYes
End With
Application.ScreenUpdating = True
wbYCPlan.Save
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,195
Members
452,616
Latest member
intern444

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