VBA Automate copy and paste

%3CLINGO-SUB%20id%3D%22lingo-sub-3421078%22%20slang%3D%22en-US%22%3EVBA%20Automate%20copy%20and%20paste%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3421078%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20All%2C%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20would%20like%20to%20ask%20for%20help%20to%20more%20efficiently%20automate%20the%20transfer%20of%20data%20from%20one%20excel%20workbook%20to%20another.%20Is%20there%20a%20way%20to%20copy%20data%20from%20individual%20tab%20in%20workbook%20A%20to%20the%20corresponding%20tab%20in%20workbook%20B%20(append%20the%20data)%20without%20having%20to%20write%20separate%20macro%20for%20each%20tab%3F%20So%20far%20i%20am%20only%20seeing%20code%20examples%20where%20data%20from%20a%20single%20tab%20is%20transfer%20to%20another%20tab%20in%20a%20separate%20workbook%20but%20no%20example%20on%20how%20I%20can%20apply%20this%20to%20multiple%20tabs%20at%20one%20go..%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3421078%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EMacros%20and%20VBA%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3423077%22%20slang%3D%22en-US%22%3ERe%3A%20VBA%20Automate%20copy%20and%20paste%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3423077%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1399832%22%20target%3D%22_blank%22%3E%40peysg%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22%22%3E%3CSPAN%20class%3D%22%22%3E%3CSPAN%20class%3D%22%22%3EWith%20Macro%20recorder%20a%20simple%20VBA%20example...%20maybe%20it%20will%20help%20you%20with%20your%20project.%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E...%3CSPAN%20class%3D%22%22%3E%3CSPAN%20class%3D%22%22%3E%3CSPAN%20class%3D%22%22%3EAddendum...I%20just%20saw%20that%20it%20should%20be%20transferred%20to%20several%20workbooks.%3C%2FSPAN%3E%3C%2FSPAN%3E%20%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22%22%3E%3CSPAN%20class%3D%22%22%3E%3CSPAN%20class%3D%22%22%3EHere%2C%20however%2C%20all%20workbooks%20should%20be%20open.%3C%2FSPAN%3E%3C%2FSPAN%3E%20%3CSPAN%20class%3D%22%22%3E%3CSPAN%20class%3D%22%22%3EWith%20closed%20workbooks%2C%20the%20effort%20is%20correspondingly%20%22unclosed%22%20large.%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ethis%20code%20is%20with%20a%20close%20workbook%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3EPublic%20Sub%20Write()%0ADim%20sPath%20As%20String%20'%20the%20folder%20path%20of%20the%20Excel%20sheets%0ADim%20sFile%20As%20String%20'%20the%20file%20to%20write%20to%0ADim%20WkSh_Q%20As%20Worksheet%20'%20the%20source%20worksheet%20-%20the%20origin%0ADim%20WkSh_Z%20As%20Worksheet%20'%20the%20target%20worksheet%20-%20the%20result%0AsPath%20%3D%20%22D%3A%5CApplication%20Data%5CExcel%20Data%5CExcel%20Files%5C%22%0AsFile%20%3D%20%22Dialogdata.xlsm%22%0AApplication.ScreenUpdating%20%3D%20False%0AIf%20Dir(sPath%20%26amp%3B%20sFile)%20%26lt%3B%26gt%3B%20%22%22%20Then%0AWorkbooks.Open%20(sPath%20%26amp%3B%20sFile)%0AThisWorkbook.Activate%0A'Application.ActiveWindow.Visible%20%3D%20False%0Aelse%0AMsgBox%20%22The%20specified%20folder%20%22%22%22%20%26amp%3B%20sPath%20%26amp%3B%20%22%22%22%22%20%26amp%3B%20Chr(10)%20%26amp%3B%20_%0A%22and%2For%20the%20file%20you%20are%20looking%20for%20%22%22%22%20%26amp%3B%20sFile%20%26amp%3B%20%22%22%22%20does%20not%20exist!%22%2C%20_%0A16%2C%20%22%20Note%20for%20%22%20%26amp%3B%20Application.UserName%0AExit%20Sub%0AEnd%20If%0ASet%20WkSh_Q%20%3D%20ThisWorkbook.Worksheets(%22Default%20Data%22)%0ASet%20WkSh_Z%20%3D%20Workbooks(sFile).Worksheets(%22Default%20Data%22)%0AWkSh_Q.Cells.Range(%22B1%3AB27%22).Copy%20Destination%3A%3DWkSh_Z.Range(%22B1%3AB27%22)%0AWorkbooks(sFile).Close%20SaveChanges%3A%3DTrue%0AApplication.ScreenUpdating%20%3D%20True%0AMsgBox%20%22The%20data%20was%20submitted%20successfully.%22%2C%20_%0A64%2C%20%22%20Information%20for%20%22%20%26amp%3B%20Application.UserName%0Aend%20sub%0A%0A'Examble%20from%20internet%2C%20untested.%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Fwww.herber.de%2Fforum%2Farchiv%2F1464to1468%2F1465439_Daten_in_geschlossene_Datei_exportieren.html%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3EVBA%20Code%20Source%3C%2FA%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fexcel-blog%2Fmeet-niko-chatzoudis-excel-forum-contributor%2Fba-p%2F2941385%22%20target%3D%22_blank%22%3ENikolinoDE%3C%2FA%3E%3C%2FP%3E%3CP%3EI%20know%20I%20don't%20know%20anything%20(Socrates)%3C%2FP%3E%3C%2FLINGO-BODY%3E
New Contributor

Hi All,

 

I would like to ask for help to more efficiently automate the transfer of data from one excel workbook to another. Is there a way to copy data from individual tab in workbook A to the corresponding tab in workbook B (append the data) without having to write separate macro for each tab? So far i am only seeing code examples where data from a single tab is transfer to another tab in a separate workbook but no example on how I can apply this to multiple tabs at one go..

 

Thank you!

 

 

1 Reply

@peysg 

With Macro recorder a simple VBA example... maybe it will help you with your project.

...Addendum...I just saw that it should be transferred to several workbooks.

Here, however, all workbooks should be open. With closed workbooks, the effort is correspondingly "unclosed" large.

 

 

this code is with a close workbook

 

Public Sub Write()
Dim sPath As String ' the folder path of the Excel sheets
Dim sFile As String ' the file to write to
Dim WkSh_Q As Worksheet ' the source worksheet - the origin
Dim WkSh_Z As Worksheet ' the target worksheet - the result
sPath = "D:\Application Data\Excel Data\Excel Files\"
sFile = "Dialogdata.xlsm"
Application.ScreenUpdating = False
If Dir(sPath & sFile) <> "" Then
Workbooks.Open (sPath & sFile)
ThisWorkbook.Activate
'Application.ActiveWindow.Visible = False
else
MsgBox "The specified folder """ & sPath & """" & Chr(10) & _
"and/or the file you are looking for """ & sFile & """ does not exist!", _
16, " Note for " & Application.UserName
Exit Sub
End If
Set WkSh_Q = ThisWorkbook.Worksheets("Default Data")
Set WkSh_Z = Workbooks(sFile).Worksheets("Default Data")
WkSh_Q.Cells.Range("B1:B27").Copy Destination:=WkSh_Z.Range("B1:B27")
Workbooks(sFile).Close SaveChanges:=True
Application.ScreenUpdating = True
MsgBox "The data was submitted successfully.", _
64, " Information for " & Application.UserName
end sub

'Examble from internet, untested.

 

 

VBA Code Source

 

 

NikolinoDE

I know I don't know anything (Socrates)