VBA - Move worksheet to new workbook and save to the same file location

Jon_H

New Member
Joined
Jun 23, 2017
Messages
8
Hi everyone;

I'm very new to VBA so please excuse what is probably a very basic question, and my less than perfect coding!! I've written a VBA which at the end I'd like it to move the active worksheet to a new workbook and save that new workbook to the same file path as the original workbook saving it as the worksheet name. Everything is fine up until the last four lines of the code - would appreciate some help!

Here's my code -

Code:
Sub Test_()

'
' Create_77_sheets_for_X Macro
'


    Sheets("X Single Template").Select
    Sheets("X Single Template").Copy After:=Sheets(4)
    Sheets("Main Data").Select
    Range("A5").Offset(1, 0).Select
    Selection.Copy
    Sheets("X Single Template (2)").Select
    Range("J30").Select
    ActiveSheet.Paste
    ActiveSheet.Name = ActiveSheet.Range("J30").Value
    ActiveSheet.Select
    Cells.Select
    Range("H1").Activate
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    ActiveSheet.Move
        Dim thisWb As Workbook
    
    Set thisWb = ActiveWorkbook
    ActiveWorkbook.SaveAs Filename:=thisWb.Path & "\new workbook.xls"
    ActiveWorkbook.Close savechanges:=False
    
End Sub
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.
Try this.
Code:
ActiveSheet.Move

With ActiveWorkbook
    .SaveAs Filename:=ThisWorkbook.Path & .Sheets(1).Name & ".xls"
    .Close savechanges:=False
End With
 
Upvote 0
Thanks for your time Norie

It certainly got further than mine before; but it didn't save it to the file folder of where the original Workbook is saved. When I re-ran it to find out where it was being saved it came up with the following (screen shot linked below) -

sNtNYq
 
Upvote 0
Can't see the screenshot I think there might be a missing \ in the code I posted.
Code:
ActiveSheet.Move

With ActiveWorkbook
    .SaveAs Filename:=ThisWorkbook.Path & "\" &  .Sheets(1).Name & ".xls"
    .Close savechanges:=False
End With
 
Upvote 0
It still did the same but I've changed your code and have now added in the file location as it will always be the same and it worked just fine.

Thanks very much for your help.
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,602
Members
449,089
Latest member
Motoracer88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top