Forum Discussion
Jn12345
Oct 18, 2024Brass Contributor
Removing Screen flicker and adding efficiency to Copy Paste Macro
Hello smart excel users, I have a macro that I've made that pulls data from other reports. I typically get to use .range = .value2 because I use table 99% of the time, however, in the case of my ...
NikolinoDE
Oct 19, 2024Gold Contributor
In the inserted file you will find the changes in the existing code that was attempted to adapt to your requirements.
Hope that it helps you😊
- Jn12345Oct 20, 2024Brass ContributorNiko! thanks! Looks like it works well. I have tested it in multiple scenarios. The only thing i am wondering is if there are any scenarios where it wouldnt be good to continue with opening the source book if it is already open. like if I am working on the source book and I go to upload into this report and it closes without saving and I lose data. Is it better to just close with saving or maybe if its already open have a pop up saying it is open and to close and try again??
- Jn12345Oct 20, 2024Brass ContributorActually I think the perfect thing to do would be to make it open and close without saving if the file is closed prior to opening it, and remain open without saving if the file is already open. Does that make sense?
- NikolinoDEOct 20, 2024Gold Contributor
Yes, that makes sense. The goal is to avoid unintentional data loss when the source workbook is already open. This macro has the change to check if the source workbook is already open before continuing with the rest of the logic.
Sub OptimizedCopyPaste() Dim wbSource As Workbook Dim wbDest As Workbook Dim wsSource As Worksheet Dim wsDest As Worksheet Dim DataRange As Range Dim DestRange As Range Dim FileToOpen As Variant Dim TempArray() As Variant Dim IsAlreadyOpen As Boolean ' Disable screen updating, events, and alerts to prevent flickering Application.ScreenUpdating = False Application.EnableEvents = False Application.DisplayAlerts = False ' Set the destination workbook and worksheet (the one with the button) Set wbDest = ThisWorkbook Set wsDest = wbDest.Sheets("Sheet1") ' Adjust to your destination sheet ' Prompt user to select the source workbook FileToOpen = Application.GetOpenFilename("Excel Files (*.xlsm), *.xlsm") If FileToOpen = False Then Exit Sub ' User canceled ' Check if the source workbook is already open IsAlreadyOpen = IsWorkbookOpen(FileToOpen) ' Open the workbook only if it's not already open If Not IsAlreadyOpen Then Set wbSource = Workbooks.Open(FileToOpen) Else Set wbSource = Workbooks(FileNameFromPath(FileToOpen)) End If Set wsSource = wbSource.Sheets("Sheet1") ' Adjust to your source sheet ' Define the range you want to copy (adjust as needed) Set DataRange = wsSource.Range("A1:D10") ' Adjust the range you want to copy ' Load the data into an array for faster processing TempArray = DataRange.Value ' Define the destination range Set DestRange = wsDest.Range("A1").Resize(UBound(TempArray, 1), UBound(TempArray, 2)) ' Transfer the data from the array to the destination DestRange.Value = TempArray ' Close the source workbook without saving if it was opened by the macro If Not IsAlreadyOpen Then wbSource.Close SaveChanges:=False End If ' Re-enable screen updating, events, and alerts Application.ScreenUpdating = True Application.EnableEvents = True Application.DisplayAlerts = True MsgBox "Data transfer completed!", vbInformation End Sub ' Function to check if a workbook is already open Function IsWorkbookOpen(workbookName As String) As Boolean Dim wb As Workbook IsWorkbookOpen = False ' Loop through open workbooks to see if the name matches For Each wb In Workbooks If wb.FullName = workbookName Then IsWorkbookOpen = True Exit Function End If Next wb End Function ' Function to get just the file name from a full path Function FileNameFromPath(fullPath As String) As String FileNameFromPath = Mid(fullPath, InStrRev(fullPath, "\") + 1) End Function
The VBA code is untested and serves as an example only, please backup your file in advance as a precaution.
My answers are voluntary and without guarantee!
Hope this will help you.
Was the answer useful? Mark as best response and Like it!
This will help all forum participants.
- Jn12345Oct 20, 2024Brass ContributorThere seems to be a mis match error and unfortunately I am not good enough at VBA to figure it out. But i will keep trying.
Another thing, In this code will i be able to add multiple ranges to the set as Data range portion?