2

I am still very new to VBA and am trying to combine certain worksheets from different workbooks.

For example:

  • I have a workbook called "One" with multiple worksheets (A,B,C,D).
  • I have another workbook called "Two" with multiple worksheets (E,F,G,H).

I want to take worksheet A from workbook One and worksheets F and G from workbook Two. I wish to put these different worksheets in a new workbook called "Three."

My fields in worksheets A and F are in the exact same format, so I also wish to combine these two worksheets and put F data in the same fields under the A data, as soon as my cells containing A data finishes.

Could anyone help me with this code??
If anyone also has any links to VBA for beginners that would be highly appreciated.

Community
  • 1
  • 1
cthallofamer
  • 141
  • 2
  • 14
  • This is covered extensively through out this site. However, you can google a few things that will help you piece it together. 1) how to open a workbook 2) how to move contents from sheet to sheet, 3) how to reference worksheets objects and workbook objects. That should get you there – Doug Coats Jun 13 '17 at 14:44
  • You can also record macros in Excel, and it will write the code for you. It's a good way to learn some of the basics. – braX Jun 13 '17 at 14:46
  • As a beginner you can also read [VBA Best Practices](https://stackoverflow.com/documentation/excel-vba/1107/vba-best-practices) so you start doing it right and don't run into all the issues many beginners do. If you got stuck come back with your code and a detailed error description so people can help if you ask a good and specific enough question. – Pᴇʜ Jun 13 '17 at 14:47

1 Answers1

2

Take a look at example:

'enforce declaration of variables 
Option Explicit

Sub CombineWorkbooks()
Dim sWbkOne As String, sWbkTwo As String
Dim wbkOne As Workbook, wbkTwo As Workbook, wbkThree As Workbook
Dim wshSrc As Worksheet, wshDst As Worksheet

On Error GoTo Err_CombineWorkbooks

'get the path
sWbkOne = GetWbkPath("Open workbook 'One'")
sWbkTwo = GetWbkPath("Open workbook 'Two'")
'in case of "Cancel"
If sWbkOne = "" Or sWbkTwo = "" Then
    MsgBox "You have to open two workbooks to be able to continue...", vbInformation, "Information"
    GoTo Exit_CombineWorkbooks
End If

'open workbooks: 'One' and 'Two'
Set wbkOne = Workbooks.Open(sWbkOne)
Set wbkTwo = Workbooks.Open(sWbkTwo)
'create new one - destination workbook
Set wbkThree = Workbooks.Add

'define destination worksheet
Set wshDst = wbkThree.Worksheets(1)

'start copying worksheets
'A
Set wshSrc = wbkOne.Worksheets("A")
wshSrc.UsedRange.Copy wshDst.Range("A1")
'F
Set wshSrc = wbkTwo.Worksheets("F")
wshSrc.UsedRange.Copy wshDst.Range("A1").End(xlDown)
'G
Set wshSrc = wbkTwo.Worksheets("G")
wshSrc.UsedRange.Copy wshDst.Range("A1").End(xlDown)

'done!

Exit_CombineWorkbooks:
    On Error Resume Next
    Set wbkThree = Nothing
    If Not wbkTwo Is Nothing Then wbkTwo.Close SaveChanges:=False
    Set wbkTwo = Nothing
    If Not wbkOne Is Nothing Then wbkOne.Close SaveChanges:=False
    Set wbkOne = Nothing
    Set wshDst = Nothing
    Set wshSrc = Nothing
    Exit Sub

Err_CombineWorkbooks:
    MsgBox Err.Description, vbExclamation, Err.Number
    Resume Exit_CombineWorkbooks


End Sub


Function GetWbkPath(ByVal initialTitle) As String
Dim retVal As Variant

retVal = Application.GetOpenFilename("Excel files(*.xlsx),*.xlsx", 0, initialTitle, , False)
If CStr(retVal) = CStr(False) Then retVal = ""

GetWbkPath = retVal

End Function

Note: Above code has been written ad-hoc, so it may not be perfect.

[EDIT2] If you would like to copy data into different sheets, please, replace corresponding code with below, but firstly remove these lines:

'define destination worksheet
Set wshDst = wbkThree.Worksheets(1)

later:

'start copying data 
'A
Set wshDst = wbkThree.Worksheets.Add(After:=wbkThree.Worksheets(wbkThree.Worksheets.Count))
wshDst.Name = "A"
Set wshSrc = wbkOne.Worksheets("A")
wshSrc.UsedRange.Copy wshDst.Range("A1")
'F
Set wshSrc = wbkTwo.Worksheets("F")
Set wshDst = wbkThree.Worksheets.Add(After:=wbkThree.Worksheets(wbkThree.Worksheets.Count))
wshDst.Name = "F"
wshSrc.UsedRange.Copy wshDst.Range("A1")
'G
Set wshSrc = wbkTwo.Worksheets("G")
Set wshDst = wbkThree.Worksheets.Add(After:=wbkThree.Worksheets(wbkThree.Worksheets.Count))
wshDst.Name = "G"
wshSrc.UsedRange.Copy wshDst.Range("A1")

Good luck!

Maciej Los
  • 8,468
  • 1
  • 20
  • 35
  • hello, thank you for your help! when I ran this it said Error: variable is not defined.. and it highlighted "dstwsh" for when you tried to define the destination worksheet – cthallofamer Jun 13 '17 at 16:30
  • Change it to `wshDst` as it has been declared in `Dim ...` section. Check updated solution. Please, accept it if it was helpful. – Maciej Los Jun 13 '17 at 17:29
  • okay, we are so close. That worked, but it put everything in the same sheet and it looked very confusing. could you just help me put everything in separate sheets? – cthallofamer Jun 13 '17 at 17:52
  • Yes, we can, but you wrote: `under the A data`. – Maciej Los Jun 13 '17 at 18:00
  • so when I tried running the full code I got "Error: 1004 Method 'Add' of 'Object' Sheets failed." – cthallofamer Jun 13 '17 at 18:20
  • Sorry, no :(. The same error appeared. Do you think it is an issue of another worksheet not added in workbook three? – cthallofamer Jun 13 '17 at 18:31
  • You shouldn't post my code as your own, especialy, when you're not an author of that code. – Maciej Los Jun 13 '17 at 18:56
  • Don't worry. Can you share your files through the free file sharing service? Note: i've tested it and it works fine. – Maciej Los Jun 13 '17 at 19:03
  • OK. Please, check my code again in 5 minutes. I'll revise it again. – Maciej Los Jun 13 '17 at 19:18
  • 1
    Done! Check `EDIT2` section. – Maciej Los Jun 13 '17 at 19:25
  • IT WORKED! Seriously, thank you so much for your help and effort. You're a very kind person and it means a lot that you helped me given how difficult I must've been lol. thank you again. youre amazing – cthallofamer Jun 13 '17 at 19:33
  • This is where I get there error: `Set wshSrc = wbkThree.Worksheets("Information") wshSrc.UsedRange.Copy wshDst.Range("A1") ` – cthallofamer Jun 15 '17 at 20:07
  • Sorry, you've wrote that everything is OK, but now, you've got an error? Can you share your workbooks? Use free share file services. I need to revise your code and the structure of data. Cheer, Maciej – Maciej Los Jun 15 '17 at 21:03
  • Everything worked perfectly, but then I tried to add a third workbook in the macro, so I updated what you sent me (which is shown above), and now I keep getting an error. I cannot share the workbooks as they are for my job. The error comes right here: `Set wshSrc = wbkThree.Worksheets("Information") wshSrc.UsedRange.Copy wshDst.Range("A1")` – cthallofamer Jun 16 '17 at 12:18
  • 1
    Let's continue our discussion [here](https://chat.stackoverflow.com/rooms/146878/further-discussion-about-combining-workbooks). You'll be able to insert your code. – Maciej Los Jun 16 '17 at 12:38