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.
Related
I'm trying to select a number of worksheets, open a new workbook and pastes the worksheets into the new workbook.
I've come across a few solutions online but they all seem to be very long and require you to input an address for the new workbook. I don't want to do this as I want the end user to be able to save the file wherever he or she wants to.
The below code allowed me to create a new worksheet but only with the activated worksheet
Sub CopyToNew2()
'Copy the active sheet to a new Workbook.
ActiveSheet.Copy
End Sub
So I tried changing it to the code below but it didn't work because it seems that I you can only activate one workbook at any one time
Sub Macro44()
Worksheets(Array("Sheet1", "Sheet2", "Sheet3", "Sheet4")).Activate
ActiveSheet.Copy
End Sub
Finally, I found this code that does exactly what I want, except it attempts to save the file.
Sub Macro43()
'
Worksheets(Array("Sheet1", "Sheet2", "Sheet3", "Sheet4")).Copy
With ActiveWorkbook
.SaveAs Filename:=Environ("TEMP") & "\New1.xlsx", FileFormat:=xlOpenXMLWorkbook
End With
End Sub
Is there any way I can change the first code so that the sheets I have predetermined copy and open in a new worksheet?
Thank you so much for your help
I have set up a macro that runs when a button is clicked. The macro checks a specified folder and pulls in the data from all of the workbooks contained in the folder into the active workbook.
The code worked perfectly however, there are new worksheets being added where the tabs have slightly different numbers at the beginning meaning the code no longer works.
Is there a way to make the macro collect all data regardless of the sheet name? (There will only ever be one sheet per workbook)
I have written the code which worked when the sheet name was always the same, however, when this changes the code no longer runs.
Do While Len(fl) > 0
' Open file
Set dWb = Workbooks.Open(fDir & fl)
' Copy data from
dWb.Sheets("DailyReport").Range("A2:AU100").Copy mSh.Cells(1, nxtCol)
' Close workbook
dWb.Close SaveChanges:=True
' Increment column counter
nxtCol = nxtCol + 7
' Go to next file
fl = Dir
Loop
Run-time error '9':
Subscript out of range
If there is only one sheet:
Set dWb = Workbooks.Open(fDir & fl)
dWb.Worksheets(1) 'this selects the first sheet in the workbook
I hope this helps
Try use activeSheet; hard or str variable:
dWb.Worksheets(ActiveSheet.Name)
Dim ash as String
ash = ActiveSheet.Name
dWb.Worksheets(ash)
I hope this is a simple question...
My understanding is that "ActiveWorkbook" returns the currently active workbook, even if the macro was run in a different workbook (this is why I almost always use "ThisWorkbook" instead).
And "ActiveSheet" returns the currently active worksheet, even if the macro was run in a different workbook (or different worksheet).
So how can I get the worksheet which currently has focus within a specific workbook, even if that workbook is not the currently active one?
You can do this by fully qualifying .ActiveSheet
Example:
Private Sub test()
Dim wb As Workbook
Set wb = Workbooks.Add
'Change the name of Sheet1 in the second workbook
'so it's not confused with Sheet1 in the first workbook.
wb.Sheets("Sheet1").Name = "Foo"
ThisWorkbook.Activate
MsgBox wb.ActiveSheet.Name
End Sub
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")
I am trying to create an Tracker. Need help with these ideas:
When the Excel book is opened, it must show only the "Tracker" Worksheet. All other sheets need to be hidden. Now am using the code {sheet.visible = xlveryhidden} But the code is too long, I have to include each sheet name. I need help with a code to show only the Tracker sheet but to hide all other sheets in the workbook.
I've included two comboboxes as year and month. Also included a Command button OK & Cancel. When clicked on OK, it has to verify the IF condition and show a specific sheet. I have no issues with that. it is showing a specific sheet. But I need a single line code to hide all other sheets
I have also included a "New Tracker" Command button. When Clicked on it, it must include a New sheet. Also too let the user to rename it. This worksheet must also be hidden once saved.
I aware the process is simple, but not sure how to complete it.
To hide all but the Tracker worksheet
Sub hidesheets()
Dim ws As Worksheet, wb As Workbook 'Create the variables we'll use for our worksheet and workbook objects.
Set wb = Excel.ActiveWorkbook 'Set the workbook variable to equal the active workbook. This can also be set to equal a named workbook if you want.
For Each ws In wb.Worksheets 'Loop through each worksheet in the workbook.
If Not ws.Name = "Tracker" Then 'If the worksheet's name isn't "Tracker" then...
ws.Visible = xlSheetVeryHidden 'Set it to very hidden.
Else 'If it is the "Tracker" worksheet, then ...
ws.Visible = xlSheetVisible 'Set it to visible.
End If 'End the IF/THEN statement.
Next ws 'Repeat for the next worksheet in the workbook.
End Sub
You'll have to modify this if you want it to work with a command button or do something with another worksheet. But it should get you started. I'd do it but I don't quite have the time right now.