Sheets("New").Select creating new workbook unintentionally - excel

For some reason, Sheets.copy is making another workbook. I don't know how to fix it. I just want it to copy the first Sheets("New") and paste everything that I have selected over to a new Sheets("New(2)").
Sheets.Copy
Sheets("New").Select
Sheets("New").Copy After:=Sheet1
'****Copy Assessment Date to the Nourish Report Printable****
Sheets("New").Select
Range("C74").Select
Application.CutCopyMode = False
Selection.Copy
Sheets("New (2)").Select
Range("V96").Select
ActiveSheet.Paste

I think the issue is with the Sheets object. In my experience it is better to use the Worksheets object. I would recommend simplifying your code by removing all of the copy and paste / select and just making the cell range take the exact value you want. The Macro Recorder does not do a very a good job of producing efficient code but it certainly does help with syntax and getting you started. Consider the code below to accomplish what you want.
Sub CopySheet()
Dim wb As Workbook, ws As Worksheet, nws As Worksheet
'set up
Set wb = ThisWorkbook
Set ws = wb.Worksheets("New")
'Create a copy of the worksheet
ws.Copy wb.Worksheets(1) 'places the worksheet at the front
Set nws = wb.Worksheets(1) 'References the new front worksheet
'set a name for the new sheet for easy reference
nws.Name = "newSheet" 'set to whatever name you want
'Print out the actual value you want rather than copy and paste.
nws.Range("V96").Value = ws.Range("C74").Value
nws.Activate 'select the new worksheet
End Sub
And here it is without all of the comments
Sub CopySheet()
Dim wb As Workbook, ws As Worksheet, nws As Worksheet
Set wb = ThisWorkbook
Set ws = wb.Worksheets("New")
ws.Copy wb.Worksheets(1)
Set nws = wb.Worksheets(1)
nws.Name = "newSheet"
nws.Range("V96").Value = ws.Range("C74").Value
End Sub
Let me know if this works.

Related

How to create and use a new sheet as the "new" `ActiveSheet` VBA

Each time this macro runs I want it to add a new sheet in the workbook and make the ActiveSheet this new sheet. Maybe I need to create a parent workbook and worksheet? Im not really sure. Here is my poor attempt at code.
If ActiveSheet.index = Worksheets.Count Then
Sheets.Add
Else
ActiveSheet.index = Worksheets.Count
End If
Thanks!
Just run Sheets.Add.
This will create a new sheet and this new sheet will be the active one.
Worksheets.Add() returns a reference to the just-added sheet, so there's no need to rely on ActiveSheet here...
Like this:
Dim ws as worksheet
Dim wb as workbook
set ws = activesheet
Set wb = activesheet.parent
if ws.Index = wb.worksheets.count then
'add a new sheet and set `ws` as a reference to it
Set ws = wb.worksheets.add(After:=wb.worksheets(wb.worksheets.count))
end if
'now work with `ws`...
Here you have it:
Sub test()
If ActiveSheet.Index = Worksheets.Count Then
Sheets.Add After:=Worksheets(Worksheets.Count)
Worksheets(Worksheets.Count).Activate
Else
ActiveSheet.Index = Worksheets.Count
End If
End Sub
Just tested, it will add a new sheet and then activate it. Thought it's not o good practice to use activate. I would suggest adding sheets with names and then reference them in code

How to copy two sheets to a new workbook?

I have a workbook with many sheets. I'm trying to copy two sheets together to a new workbook.
I get
Run-time error 13 for type mismatch.
Sub CopyBillStatandCosts()
Dim MyBook As Workbook
Dim NewBook As Workbook
Set MyBook = ThisWorkbook
Workbooks.Add ' Open a new workbook
Set NewBook = ActiveWorkbook
Set MyBook = ActiveWorkbook
Sheets(11).Copy Before:=Workbooks(NewBook).Sheets(1)
Sheets(9).Copy Before:=Workbooks(NewBook).Sheets(1)
Workbooks(NewBook).Sheet1.Delete
End Sub
Update: I figured out the code. But how do I refer to the sheets by their code names, which is best practice? They are sheet9 and sheet 11.
Sub copyBillStatandCosts()
ThisWorkbook.Worksheets(Array("BillStat", "C")).Copy
End Sub
Your second
Set MyBook = ActiveWorkbook
was probably meant to be
MyBook.Activate
although an overall simpler way to do this would be
Sub CopyBillStatandCosts()
Sheets(Array("BillStat", "Costs")).Copy
End Sub
The Copy with no parameter makes the copy in a new workbook.
Copy Worksheets by Code Name in One Go
Sub copyBillStatandCosts()
ThisWorkbook.Worksheets(Array(Sheet9.Name, Sheet11.Name)).Copy
' To continue to work with the new workbook, do the following:
Dim NewBook As Workbook: Set NewBook = ActiveWorkbook
' e.g.:
' NewBook.SaveAs "C:\Test\Test.xlsx", xlOpenXMLWorkbook
' To continue to work with each new worksheet, do the following:
Dim bws As Worksheet: Set bws = NewBook.Worksheets(1)
Dim cws As Worksheet: Set cws = NewBook.Worksheets(2)
' e.g.:
MsgBox NewBook.Name & vbLf & bws.Name & vbLf & cws.Name
End Sub
Why use code names? Now you can rename the two worksheets in the tabs, and the code will still copy the right ones to a new workbook.
Why in one go? If there are references of the worksheets from one to each other they will still work in the new workbook i.e. will not refer to the worksheets in the source workbook.
If you wanna use programmatic names, then just use Name property:
ThisWorkbook.Sheets(Array(sheet9.Name, sheet11.Name)).Copy

Copy tabs of similar partial name to another workbook

Would appreciate if there's any help anywhere. Let's say, I have the following tabs: Data Set 001, Data Set 002, Data Set 003, so long the tab names contain Data Set, it should copy over to another workbook (let's say Main File). Any help with regards to this is welcomed. Thanks all in advance!
Best Regards,
Josh
I used this in Workbook containing worksheet, "ASSESSMENT FORMxx":
Sub CopyWorksheetsToNewWorkbook()
'This macro is to be in the ActiveWorkbook
Application.DisplayAlerts = False
Application.ScreenUpdating = False
Dim wb As Workbook
Dim ws As Worksheet
Set wb = Workbooks.Add
wb.SaveAs Filename:="Book10" & ".xlsx"
Workbooks.Open ("Book10.xlsx")
For Each ws In Workbooks("ActiveWorkbookName.xlsm").Sheets
If ws.Name Like ("ASSESSMENT FORM*") Then ws.Copy Before:=Workbooks("Book10.xlsx").Worksheets("Sheet1")
Next ws
Workbooks("Book10.xlsx").Worksheets("Sheet1").Move Before:=Workbooks("Book10.xlsx").Sheets(1)
Application.ScreenUpdating = True
Application.DisplayAlerts = True
End Sub
Note that both workbook with worksheet "ASSESSMENT FORMxx" and Book10.xlsx must be open.
Did you have a go at any code?
Sub Whatever()
Dim wb As Workbook
Dim ws As Worksheet
Set wb = Workbooks("AnyOpenWorkbookName.xlsx")
For Each ws In ThisWorkbook.Sheets
If ws.Name Like "Data Set" & "*" Then
Call ws.Copy(after:=wb.Sheets(wb.Sheets.Count))
End If
Next ws
End Sub
You need to loop through all Worksheets of your 1st Workbook, test if its name contains Data set with Worksheet.Name and InStr.
If the InStr function returns something else than 0 (meaning your Worksheet name contains Data Set), you can copy the current Worksheet to the 2nd Workbook.
Adapt this sample to your needs:
'Loop through all worksheets
If InStr(wsCurrent.Name,"Data Set") <> 0 Then
' Copy wsCurrent to new WorkBook
End If
' End of loop

Choose from open workbooks to paste in copied data

I am writing an VBA code that should copy a specific range from one workbook into another workbook (that is open). I want to choose this workbook from a popup window that is showing all the open excel workbooks.
What I have to start with is below which is copying the range I want, do not want to specify the workbook (changing every year) or the worksheet (changing every month) more than this. From the Application.Dialogs(xlDialogActivate).Show I get a list of active workbooks/aplications (thanks #jkpieterse) and I now want to set this as WB2 so that I can copy past it into my chosen range.
Sub GTS_Timesheet()
Dim WB As Workbook, WS As Worksheet, RG As Range, WB2 As Workbook
Set WB = ActiveWorkbook
Set WS = WB.ActiveSheet
Set RG = WS.Range("F10", Range("U" & Cells(Rows.Count, "F").End(xlUp).Row))
''Set WB2 =
RG.Copy
WB2 = Application.Dialogs(xlDialogActivate).Show
WB2.Worksheets("Paste FRW Data").Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial Paste:=xlPasteValues
End Sub
Thanks, and please tell if I need to explain more what I want.
Once selected the workbook becomes active:
Application.Dialogs(xlDialogActivate).Show
Set WB2 = ActiveWorkbook

Want to format one workbook but i am wrinting the Macro in another workbook

I have one workbook where I have some userform and code. Now based on this macro I am fetching the data into another workbook. Now From first excel I want to format the 2nd workbook after getting required data.
Below is my code:
Set abc = objWorkbook1.Sheets(report_shtnm)
abc.Activate
With Sheet2.Range(Cells(4, 1), Cells(rw_reps_sht, cl_reps_sht))
.Borders.Weight = xlThin
.WrapText = True
End With
The problem is its formatting the excel which has macro. Please help.
The best way is to declare your objects correctly and then simply work with them... See this example
Option Explicit
Sub Sample()
Dim wb1 As Workbook, wb2 As Workbook
Dim ws1 As Worksheet, ws2 As Worksheet
'~~> This workbook which has the macro
Set wb1 = ThisWorkbook
Set ws1 = wb1.Sheets("Sheet")
'~~> The other workbook
Set wb2 = Workbooks.Open("C:\Sample.xlsx")
Set ws2 = wb2.Sheets("Sheet1")
'~~> Work with sheet1 in the workbook which has the macro
With ws1
'
'~~> Your code here
'
End With
'~~> Work with sheet1 in the second workbook
With ws2
'
'~~> Your code here
'
End With
End Sub
If you notice that using this method doesn't even require you to use .Select or .Activate ;) You might also want to read this regarding .Select or .Activate

Resources