Copying a column to a new workbook sheet in VBA - vba

I am trying to copy an existing column from a sheet to a column in a new workbook.
The code that I tried:
Set NewBook = Workbooks.Add
Sheets("Sheet1").Columns(1).Copy Destination:=NewBook.Sheets("Sheet1").Columns(2)
This is creating a sheet but no values are copied!
This code works if I copy to another sheet in same workbook.
How should I create a new sheet in the NewBook and target columns in that sheet?

You must remember the old book, otherwise Sheets("Sheet1").Columns(1) will refer to the first column of the Sheet1 of the newly added one since it is the ActiveWorkbook in this moment (so you actually copy something, but that something is an empty column):
Sub qwerty()
Set w1 = ActiveWorkbook
Set NewBook = Workbooks.Add
w1.Sheets("Sheet1").Columns(1).Copy Destination:=NewBook.Sheets("Sheet1").Columns(2)
End Sub
EDIT#1:
This will rename the sheet in the new workbook and then perform the copy:
Sub qwerty2()
Set w1 = ActiveWorkbook
Set NewBook = Workbooks.Add
ActiveSheet.Name = "Report"
w1.Sheets("Sheet1").Columns(1).Copy Destination:=Columns(2)
End Sub

Related

Excel Copy active sheet and specified sheets to new workbook

im trying to copy the active sheet and 2 specified sheets to a new workbook and then have the macro to continue to run on the new workbook to change a few things before i save it
i want to copy the sheets by the sheets codenames
1st Sheet 3 to copy this will be the "active sheet"
2nd Sheets codename is "DropDown_Sheet_1_VB"
3rd Sheets codename is "Control_Sheet_VB"
Example Code.
Sub Create_Work_Order()
Workbooks.Add
ActiveSheet.Copy 'copy to new workbook
DropDown_Sheet_1_VB.Copy 'also copy to new workbook
Control_Sheet_VB.Copy 'also copy to new workbook
Dim NewWB As Workbook
NewWB.ActiveSheet.Range("A1").Copy 'do stuff on new workbook
NewWB.Save
End Sub
I don't know what is the stuff at the end other than copying A1 range but I put it there too.
You simply need to assign the workbook object (wrk in my sample, NewWB in your sample - variable name doesn't matter) at the beginning and tell the Copy method where to copy. You will see that I specified first parameter - which is :Before - as wrk.Worksheets(1), so copied sheets are copied just before the first worksheet in the new workbook.
Sub Create_Work_Order()
Dim currentWrk As Workbook
Dim wrk As Workbook
Set currentWrk = ActiveWorkbook
Set wrk = Workbooks.Add
currentWrk.ActiveSheet.Copy wrk.Worksheets(1)
DropDown_Sheet_1_VB.Copy wrk.Worksheets(1)
Control_Sheet_VB.Copy wrk.Worksheets(1)
'wrk.ActiveSheet.Range("A1").Copy 'do stuff on new workbook
wrk.Save
' EDIT: I added following to change the functions to refer to the new file
wrk.ChangeLink currentWrk.FullName, wrk.FullName, xlExcelLinks
wrk.Save
End Sub

Create a Copy of Sheets from existing (unknown named) workbook into a new (unknown named) workbook

I have an existing workbook that will be used by multiple users (who will name the workbook uniquely - I can set one Workbook Codename if needed though, though don't know how to do this?).
I need to create a macro that opens a new workbook (which presumably I won't know the name of? as it could be 'Book1', 'Book2', 'Book3' etc?), then returns to the original workbook where the macro is stored, and copies several (can do one at a time if needed) sheets (that I DO know the names of these sheets) and pastes them as new sheets into the new workbook that I created at the start. The macro does not need to Save the file (in fact it's preferable that it doesn't as I want the user to save the new workbook wherever is most convenient for the user).
I have attempted to show what the macro would do, showing the obvious problem that I do not know the names of the workbooks I am creating/copying from/pasting into.
Any help, much appreciated!
Sub CopySheetintoNewWorkbook()
'Macro opens new / blank workbook (name unknown?)'
Workbooks.Add
'Macro goes back to original workbook where macro is saved (of which the name is unknown to the macro - i.e., users can and will change it)'
Windows("UnknownWorkbookName-1").Activate
'Macro goes to a sheet which can be named and will be known, so this is no problem'
Sheets("KnownSheet").Select
'Macro creates a copy of the sheet and pastes it as a new sheet within the new, unknown named workbook'
Application.CutCopyMode = False
Sheets("KnownSheet").Copy Before:=Workbooks("UnknownWorkbookName-2").Sheets(1)
End Sub
We want to copy Sheet1 and Sheet2.
This relies on a tiny trick:
Sub qwerty()
Dim wb1 As Workbook, wbNEW As Workbook
Set wb1 = ActiveWorkbook
Sheets("Sheet1").Copy
Set wbNEW = ActiveWorkbook
wb1.Sheets("Sheet2").Copy after:=wbNEW.Sheets(1)
End Sub
When the first .Copy is performed, a new workbook is created and it becomes the ActiveWorkbook ........the rest is easy.
EDIT#1:
If we have a group of sheets to be copied, then we can create an array of sheet names and loop through the array, copying one sheet at a time:
Sub qwerty()
Dim wb1 As Workbook, wbNEW As Workbook
Dim ary() As String, s As String, i As Long
s = "Larry,Moe,Curly"
ary = Split(s, ",")
Set wb1 = ActiveWorkbook
i = 1
For Each a In ary
If i = 1 Then
Sheets(a).Copy
Set wbNEW = ActiveWorkbook
Else
wb1.Sheets(a).Copy after:=wbNEW.Sheets(1)
End If
i = 2
Next a
wbNEW.Activate
End Sub

Copy range to new worksheet fails

VBA newbie having trouble copying a sheet into another with offset.
Copying the entire sheet works but I can't get copy with offset to function the same way.
What am I missing?
source.Copy ThisWorkbook.Sheets(Sheets.Count) 'works
source.Range("A12").Copy Destination:=ThisWorkbook.Sheets(Sheets.Count) 'fails
You were trying to paste a range on a sheet object, but you need to specify the initial range (top left cell) where you want to paste in that sheet! ;)
source.Range("A12").Copy Destination:=ThisWorkbook.Sheets(Sheets.Count).Range("A1")
You could also use "range-transfer" which is far more efficient than copy :
ThisWorkbook.Sheets(Sheets.Count).Range("A1").Value = source.Range("A12").Value
And here is how to create a new sheet and paste from the old one :
Sub test_frostbite()
Dim wB As Workbook, _
WsNEW As Worksheet, _
Source As Worksheet
Set wB = ActiveWorkbook
Set Source = wB.Sheets("SheetName")
Set WsNEW = wB.Sheets.Add
Source.UsedRange.Copy Destination:=WsNEW.Range("A1")
End Sub

Can I copy a sheet to a new workbook if it's hidden

So maybe I'm just not getting something here, but I have a hidden worksheet that list several columns based on other hidden sheets. I'm trying to minimize user intervention... :-)
I want to copy Hidden sheet 1 into a brand new workbook as an available sheet with the aforementioned values. Code that works when hiddensheet is visible:
Dim wbNew As Workbook
Application.DisplayAlerts = False
Worksheets("HiddenSheet").Copy
Set wbNew = ActiveWorkbook
With wbNew
With .Worksheets(1).UsedRange
.Value = .Value
End With
.SaveAs ThisWorkbook.Path & "\"
.Close True
End With
So I'd like to still copy the sheet to a new workbook....just want to do it with the sheet hidden.
Any thoughts?
Absolutely place this line:
Worksheets("HiddenSheet").Visible = xlSheetVisible
before this line:
Worksheets("HiddenSheet").copy
is is impossible to copy a hidden sheet to a new workbook
the new workbook would only have 1 sheet (hidden), but every wb needs at least one visible sheet
if you create a visible sheet and copy your hidden sheed later into it, it works
Sub Makro1()
Dim wbNew As Workbook
Set wbNew = Workbooks.Add
ThisWorkbook.Sheets("Hidden Sheet").Copy Before:=wbNew.Sheets(1)
End Sub
try it :)

Save a named sheet from one workbook to a new workbook in the same folder

I would like to be able to
take a sheet called "data" in a given workbook called
"original_data.xlsm",
copy it conditionally (using Autofilter or
something else), say only those rows where column C was "dog"
Create a new workbook, in the same folder as the original book, called dog.xlsm and save the copied stuff into
a new sheet called "dog data".
Then repeat with a different filter.So for example copy and
autofilter if column C was "cat" and create a workbook "cat.xlsm", in the same folder as the original book, with a sheet called "cat_data" containing some filtered data.
I've been making incorrect attempts for three days now and would appreciate some help. Here is what I have done so far.
Workbooks.Add
Set wb = ActiveWorkbook
GetBook = ActiveWorkbook.Name
wb.Sheets("data").SaveAs Workbooks(GetBook).Path & "\dog"
Workbooks("dog.xlsx").Worksheets("Sheet1").UsedRange.AutoFilter Field:=3, Criteria1:="=dog"
But it's not working. :(
Looks like you're trying to set wb to "original_data.xlsm", but your first line is making the new workbook the active workbook.
Workbooks.Add
Set wb = ActiveWorkbook
See if this helps.
Sub sheetCopy()
Dim wbS As Workbook, wbT As Workbook
Dim wsS As Worksheet, wsT As Worksheet
Set wbS = ThisWorkbook 'workbook that holds this code
Set wsS = wbS.Worksheets("Data")
wsS.Copy
Set wbT = ActiveWorkbook 'assign reference asap
Set wsT = wbT.Worksheets("Data")
wsT.Name = "Dog Data" 'rename sheet
wbT.SaveAs wbS.Path & "\dog.xlsx" 'save new workbook
wsT.UsedRange.AutoFilter Field:=3, Criteria1:="=dog"
End Sub