Copy and combine sheets to a workbook - vba

I need VBA code for Excel which: will be activated by a button in an empty workbook, loop through open workbooks, copies only sheets called "specificsheetname" from workbooks and pastes it into a new worksheet in the button activator workbook. So idea is that it will combine many worksheets from different workbooks into a one workbook. I tried this:
Sub workbookFetcher()
Dim book As Workbook, sheet, wsNew, wsCurr As Worksheet
Set wsCurr = ActiveSheet
For Each book In Workbooks
For Each sheet In book.Worksheets
If sheet.Name = "COOLING_RAW" Then
Set wsNew = Sheets.Add(After:=wsCurr)
book.Worksheets("COOLING_RAW").Copy
Set wsNew = book.Worksheets("COOLING_RAW")
End If
Next sheet
Next book
End Sub
It kind of works but it pastes all the copied worksheets to a new workbook. That's not what I want, I want them to pasted in the same workbook.

As I said in my comment:
Sub workbookFetcher()
Dim book As Workbook, sheet as Worksheet
For Each book In Workbooks
For Each sheet In book.Worksheets
If sheet.Name = "COOLING_RAW" Then
book.Worksheets("COOLING_RAW").Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
End If
Next sheet
Next book
End Sub
If you want it to be after the ActiveSheet and the ActiveSheet is in the middle of other sheets, you can still use your wsCurr and just increment the index.

If you've got Excel 2016, then the newly bundled PowerQuery functionality under the Get & Transform part of the ribbon is by far the best way to do this. Suggest you google something like PowerQuery Combine Workbooks and you'll see heaps of great tutorials showing you exactly what to do. It pretty much makes lots of VBA redundant, and it is childs-play to learn compared to VBA.
If you've got any other version of Excel from 2010 up and have admin rights on your machine, you can download and install PowerQuery from Microsoft's site...it's a free add-in

you don't need to iterate through each single workbook worksheets, while you just try to get the wanted sheet and copy it if it actually exists
moreover you want to avoid searching ThisWorkbook itsel for wanted worksheet, too!
Option Explicit
Sub workbookFetcher()
Dim book As Workbook, sht As Worksheet
For Each book In Workbooks
If book.Name <> ThisWorkbook.Name Then ' skip ThisWorkbook and avoid possible worksheet duplication
If GetWorksheet(book, "COOLING_RAW", sht) Then sht.Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count) ' if currently searched workbook has wanted worksheet then copy it to ThisWorkbook
End If
Next
End Sub
Function GetWorksheet(book As Workbook, shtName As String, sht As Worksheet) As Boolean
On Error Resume Next ' prevent subsequent statement possible error from stoping the function
Set sht = book.Worksheets(shtName) ' try getting the wanted sheet in the passed workbook
GetWorksheet = Not sht Is Nothing ' return 'True' if successfully got your sheet in the passed workbook
End Function

Related

VBA to copy and past in two different workbooks

I want to copy all the data and paste it in new workbook. With the below coding, I am able to paste all the values but it is creating two workbooks and pasting the data in one workbook.
I want to create only one new workbook and past the data. Not sure as to what went wrong.
On Error Resume Next
ThisWorkbook.Sheets(2).Copy
Dim wkb As Workbook
Set wkb = Workbooks.Add
wkb(1).PasteSpecial xlPasteValues
On Error GoTo 0
The Copy method has a different meaning when applied to a Worksheet and when applied to a Range:
The Copy method of a Worksheet creates a copy of the sheet; in the absence of any parameter, the copy is placed in a new workbook.
The Copy method of a Range puts a copy of the Range on the clipboard, from where you can then Paste it somewhere else.
So in your case, the statement
ThisWorkbook.Sheets(2).Copy
already makes a copy the Worksheet into a new workbook.
If you want to create the new workbook explicitly then you should copy the used range to the clipboard:
ThisWorkbook.Sheets(2).UsedRange.Copy
It's not the answer, but let me do it instead of yourself:
On Error Resume Next
ThisWorkbook.Sheets(2).Copy
Dim wkb As Workbook
Set wkb = Workbooks.Add
wkb(1).PasteSpecial xlPasteValues
On Error GoTo 0
.

Excel macro modification ( or maybe VBA ) to update the last included worksheet

Situation
Trying to copy a range of cells that include formulas from worksheet called "Sheet1" to the rest of other worksheets I found that I could do it performing "Fill Across Worksheets". It worked fine, so my next step was to record a Macro for it to be more efficient and worked just fine too.
The Problem
The problem is that when I include a new worksheet and run the Macro, the Macro does not consider the new worksheet so this last worksheet doesn't get updated.
I am including below the code created by the macro. In it I can see that it's including only the worksheets I have now in the workbook, so this is where I need help.
( My excel is in Spanish so when you read Ctrl+Mayus+Q, Mayus means Shiftkey )
Help
What I need is a way to modify this Macro so when it runs it will check and update all worksheets. Or, maybe it's because a Macro can't do this I may need a VBA code ? If this VBA is the way to resolve it, can you help me here with this ?
I appreciate all help
Thank you
Javier
Sub Macro2()
'
' Macro2 Macro
'
' Acceso directo: Ctrl+Mayús+Q
'
Range("A5:D12").Select
Sheets(Array("Sheet1", "Sheet2", "Sheet3", "Sheet4")).Select
Sheets("Sheet1").Activate
ActiveWindow.SelectedSheets.FillAcrossSheets Range:=Selection,
Type:=xlAll
Sheets("Sheet1").Select
End Sub
This is a work around, concerning that you want all the worksheets to have the value of the first worksheet in range A5:D12:
Sub TestMe()
Dim ws As Worksheet
Dim selAddress As String
selAddress = "A5:D12"
For Each ws In Worksheets
'ws.Range(selAddress).Value2 = Worksheets(1).Range(selAddress).Value2
ws.Range(selAddress).Formula = Worksheets(1).Range(selAddress).Formula
Next ws
End Sub
See How to avoid using Select in Excel VBA.

Copy / paste same range of cells of many sheets from one book to another

I'm rather new to VBA and I'm trying hard to solve the following problem. I usually get an excel file with 10-20 sheets. Then, I copy the same range of cells (i76:i133) of every sheet from one book to another book that has the same sheet structure.
I'm trying to make a loop to code this easily but I'm failing.
Sub copy()
Dim Sourcebook As Workbook
Dim Destinationbook As Workbook, mysheet As Worksheet
Set Sourcebook = Workbooks("Quarterly.xlsx")
Set Destinationbook = Workbooks("Master.xlsx")
For Each mysheet In Sourcebook.Worksheets
Sourcebook.Sheets(mysheet).Range("I76:I133").Copy
Destinationbook.Sheets(mysheet).Range("I76").Paste
Next
End Sub
I get a
run-time error 13, type mismatch
in Sourcebook.Sheets(mysheet).Range("I76:I133").Copy
Any help would be very welcome!
Your mySheet variable is of Worksheet type while you are trying to use it as a String. Here is your loop improved:
For Each mysheet In Sourcebook.Worksheets
'Sourcebook.Sheets(mysheet).Range("I76:I133").Copy 'instead of this...
mysheet.Range("I76:I133").Copy '...use this
Destinationbook.Sheets(mysheet.Name).Range("I76").Paste
Next

Copy a range from excel to another workbook in a new worksheet

I'm very new to VBA macros and have taught myself some code but I'm struggling with my current piece of work and can't find the answer I am looking for.
I want to copy a range of cells (B3:N21) from one workbook to another "master" workbook - which seems simple enough - but I would like it to copy into a blank/new worksheet in the Master copy every time the Macro is run.
The range contains formulas, I would only need the values copied to the Master workbook.
Any help with this would be greatly appreciated.
Thanks
Worksheets("Sheet1").Range("C1:C5").Copy
Worksheets("Sheet2").activate
Worksheets("Sheet2").Range("D1:D5").PasteSpecial _
Operation:=xlPasteSpecialOperationAdd
End With
I think you only need paste special, this is an example
try this
Option Explicit
Sub main()
Dim masterWb As Workbook
Dim mySht As Worksheet
Set mySht = ThisWorkbook.ActiveSheet '<~~ assuming you're copying values from active worksheet of the workbook the macro resides in
' beware: if you start the macro while the active sheet is not the one you want, this will lead to unespected results
Set masterWb = Workbooks("Master") '<~~ Change "Master" with whatever name your master workbook must have
' beware: we're assuming "Master" workbook is already open, otherwise this line will throw an error
With masterWb.Worksheets.Add
.Range("B3:N21").Value = mySht.Range("B3:N21").Value
End With
End Sub
mind the comments
the code above can be reduce to a much less verbose (and self explanatory, too) one like follows
Sub main2()
Workbooks("Master").Worksheets.Add.Range("B3:N21").Value = ThisWorkbook.ActiveSheet.Range("B3:N21").Value
End Sub
where apply the same comments of the lengthy code, which is:
assuming you're copying values from active worksheet of the workbook the macro resides in
beware: if you start the macro while the active sheet is not the one you want, this will lead to unespected results
change "Master" with whatever name your master workbook must have
beware: we're assuming "Master" workbook is already open, otherwise an error would be thrown

Dealing with Run-time error '9': Subscript out of range when iterating over Worksheet

Not an Excel/VB expert but I keep getting
Run time error: 9: Subscript out of range
The error is occurring at the For Each line. Not sure why. I'm trying to copy worksheets from one workbook to another workbook.
The Workbook strFileName is being open successfully and the workbook does contain two other worksheets but code is failing on next line. I've seen similar posts regarding similar issue but have not had any luck. Any advice would be great. (I'm using Excel 2010) Thanks
Workbooks.Open (strFileName)
For Each sheet In Workbooks(strFileName).Worksheets
total = Workbooks(activeWKBook).Worksheets.Count
Workbooks(strFileName).Worksheets(sheet.Name).Copy _
after:=Workbooks(activeWKBook).Worksheets(total)
Next sheet
strFileName contains the full path of the workbook.
So you cannot use it in Workbooks(strFileName) since it only expects the workbook name.
This is how you should do it:
Dim wbName As String
wbName = Split(strFileName, "\")(Ubound(Split(strFileName, "\"))) ' Get the WB Name
For Each sheet In Workbooks(wbName).Worksheets
' Other cool stuff goes here
Next
But it is better to be explicit right away so you'll not have to worry about default path separator.
Remember that it is not always \. So I suggest you try below.
Dim myWB As Workbook
Set myWB = Workbooks.Open(strFileName)
Dim sheet As Worksheet
For Each sheet In myWB.Worksheets
With Thisworkbook ' Explicitly refer to the workbook that contains the code
sheet.Copy After:=.Sheets(.Sheets.Count)
End With
Next
Remember, you need to use ThisWorkbook in place of ActiveWorkbook.
Why? Because the moment you open the other workbook, the currently opened workbook becomes the ActiveWorkbook.
So to copy all sheets from the opened workbook to the workbook that contains the code, use ThisWorkbook instead.