How to copy sheets to another *specific* workbook using vba? - excel

So I know the answer to the question How to copy sheets to another workbook using vba? And here is the code I used:
wb.Worksheets(1).Copy Before:=activeWB.Worksheets("Sheet1")
So from this workbook called "wb", I copy the worksheet within "wb" to a new workbook called"activeWB". The before function place this sheet in front of "Sheet1".
However, I want to place the sheet from "Wb" to "Sheet1", not before or after it. I tried many many methods, but couldn't get this to work. :(

Don't overthink this. Copy the sheet. Delete the existing Sheet1. Rename the copied sheet to Sheet1
wb.Worksheets(1).Copy Before:=activeWB.Worksheets(1)
Application.DisplayAlerts = False
activeWB.Worksheets("Sheet1").Delete
Application.DisplayAlerts = True
activeWB.Worksheets(1).Name = "Sheet1"

Have you tried?
wb.WorkSheets(1).UsedRange.Copy activeWB.WorkSheets("Sheet1").Range("A1")

Related

Copy/Paste worksheet to new workbook without that worksheet's VBA code

What I want to do is to create a normal Excel workbook (.xlsx) where I summarize the inputs/outputs of some simulations by copying some sheets from a Macro-enabled workbook (.xlsm) and pasting in a normal Excel workbook. The original sheets have macros, shapes, named ranges, dropdown lists, and some formatting. I want some properties of the sheets to come over to the new workbook (named ranges, row and column formatting, cell formatting) but not others (shapes, dropdown lists, macros). I also have to distribute the .xlsm workbook to other users, so I want a solution that doesn't require the user to grant permissions.
If I copy/paste like below, then I get all the properties of the worksheet to come over to the new workbook. I have figured out how to delete shapes and remove dropdown list formatting from the sheet in the new workbook, but I can't remove the worksheet's VBA code without modifying the VBA references.
If I paste special, then I can avoid bringing over the worksheet's VBA code, but I can't bring over the named ranges.
CopySheetToWB(sht as string, wb_New as workbook)
Dim sht_Name as string, rng As Range, shp as shape
Dim ws As Worksheet, wb As Workbook, ws_New As Worksheet
' set sheet in CURRENT wb
Set wb = ThisWorkbook
Set ws = wb.Worksheets(sht)
' copy/paste sheet to NEW wb
ws.Copy after:=wb_New.Sheets(wb_New.Sheets.Count)
' delete shapes from NEW ws
Set ws_New = wb_New.Worksheets(sht)
For Each shp In ws_New.Shapes
shp.Delete
Next shp
' remove dropdown lists from copied sheet
ws_New.Cells.Validation.Delete
End Sub
The answer to my problem turned out to be quite simple. My old code that wasn't working and my new code that is working. If you save the file type correctly, then the macros in the worksheets are not a problem as they are removed.
wb_New.SaveAs FileName:=str_fName, FileFormat:=xlWorkbookNormal 'old code
wb_New.SaveAs FileName:=str_fName, FileFormat:=51 'new code
Also make sure to include the file extension in str_fName. VBA doesn't automatically append the correct file extension based on the FileFormat you choose.

vba macro to copy data from one sheet and save it a new workbook

I'm not sure what is going wrong here.
The following code is supposed to copy all my data from one of the sheets in the current workbook, paste that into a new workbook and then save that workbook in the same directory as the current workbook.
Sub copy_to_new_workbook()
ThisWorkbook.Sheets("summary_data").Copy
' Create new Workbook
Set NewBook = Workbooks.Add
' Name it and paste data
NewBook.Activate
ActiveSheet.Paste
NewBook.SaveAs Filename:="export.xlsx"
End Sub
However what happens, is it creates a new excel file called "export.xlsx" and all that is copied is my vba code? Additionally a new workbook opens up called like book2 or book3, this workbook has my required data but it is not saved?
Additionally I would like to save the new workbook with a title such as: export_DD_MM_YYYY with todays date in the name field.
Thanks.
ThisWorkbook.Sheets("summary_data").Copy
creates a copy of the sheet (not the contents) - if you don't specify where that copy should be located then it will be created in a new workbook.
At that point you can just call
Set NewBook = Activeworkbook
and save it.
If you only want to copy the sheet contents then you could use:
ThisWorkbook.Sheets("summary_data").Cells.Copy
or
ThisWorkbook.Sheets("summary_data").UsedRange.Copy

Copy the sheet with index to a new Workbook

I use this code to copy sheets(i) to new workbook but it always error at ActiveWorkbook.Sheets(i).Copy . If I use specific sheet (like sheets("Handover")) code run. Please help me explain what 's wrong
Sub CopyToNew()
'Copy the sheets(i) to a new Workbook.
For i = 3 To ActiveWorkbook.Sheets.Count
ActiveWorkbook.Sheets(i).Copy
Next
End Sub
Assuming you have 3 or more worksheets, what's happening is that the first time you do ActiveWorkbook.Sheets(i).Copy, the newly created workbook becomes the ActiveWorkbook, with only one worksheet.
To avoid this anomaly, replace ActiveWorkbook with ThisWorkbook in your code.
Also, Check if there are hidden sheets in the workbook, the method Copy fails on hidden worksheets.

I need an Excel Macro that will automatically select "Yes" for a pop-up window regarding a name that already exists

I've built a Macro that will copy all of the sheets in one workbook and paste them into another workbook. So far it works just fine, but while the macro is in the midst of copying and pasting each sheet, excel flashes a pop up window that reads "The name '(insert any name here)' already exists. Click Yes to use that version of the name, or click No to rename the version of '(insert any name here)' you're moving or copying."
It does this for multiple named items that have the same name in the sheets that I'm copying and pasting to the new workbook, so this window pops up a lot (about 30 times!). Is there a Macro that will automatically select "Yes" so I don't have to keep selecting yes over and over again? I've listed the macro for copying and pasting all worksheets into another workbook of mine below for reference.
Sub Copy_Sheets()
Dim b1 As Workbook, b2 As Workbook
Dim sh As Worksheet
Workbooks.Open Filename:="C:\Users\Documents\Test.xlsx"
Set b1 = ActiveWorkbook
Workbooks.Open Filename:="C:\Users\Experiment.xlsx"
Set b2 = ActiveWorkbook
For Each sh In b2.Sheets
sh.Copy After:=b1.Sheets(b1.Sheets.Count)
Next sh
End Sub
You can insert:
Application.DisplayAlerts = False
before the copying, and
Application.DisplayAlerts = True
after.
This way you'll turn off the alerts before copying and will turn them on after.

Move or Copy Excel Sheet to Another Workbook (Destination workbook is named using variable)

I am trying to copy an excel worksheet into another excel workbook. This generally wouldn't be too difficult, except I am moving my worksheet into a workbook that is saved using variables.
Here is a bit of my code:
Sheets.Add After:=Sheets(Sheets.Count)
ActiveCell.FormulaR1C1 = "a"
Sheets("Policy Search").Move After:=Workbooks(fname).Sheets(SheetName)
"policy search" will always be named the same, and this is the workbook I am trying to move. However, I am trying to move it to a file that is dynamically saved using the variable "fname", and the sheet name "SheetName".
Every time I try and run this bit of code, I am getting a Subscript out of range error. Any ideas on how to fix this?
Thanks!
Try this:
Sub MoveSheet()
Sheets("Policy Search").Select
Sheets("Policy Search").Move After:=Workbooks(fname).Sheets(SheetName)
End Sub
It assumes "Policy Search" is the active sheet.

Resources