Vb to Save a new workbook in the same location

bark01

Board Regular
Joined
Sep 6, 2006
Messages
135
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have a script that takes a sheet from the current workbook puts it into a new workbook then converts all the data into values and then saves it as a specific name.

It currently saves the new workbook to the 'my documents' folder but I want to save it in the same location as the workbook which contained the original macro.

how do I do this?

Code:
Sub ZCRM()
Dim Fname As String
Fname = Sheets("ZCRM_PROD_SELECT").Range("A1").Value
Sheets("ZCRM_PROD_SELECT").Copy
With ActiveWorkbook
Range("A1:k3000").Select

Selection.Copy
Range("a1").Select

Selection.PasteSpecial Paste:=xlPasteValues, _
    Operation:=xlNone, SkipBlanks:=False, Transpose:=False

Range("A1").Select

Application.CutCopyMode = False

    .SaveAs Filename:="ZCRM.xls"
    .Close
End With
End Sub

many thanks
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Code:
Sub ZCRM()
    Dim Fname As String
    Fname = Sheets("ZCRM_PROD_SELECT").Range("A1").Value
    Sheets("ZCRM_PROD_SELECT").Copy
    With ActiveWorkbook
    Range("A1:k3000").Select
    
    Selection.Copy
    Range("a1").Select
    
    Selection.PasteSpecial Paste:=xlPasteValues, _
        Operation:=xlNone, SkipBlanks:=False, Transpose:=False
    
    Range("A1").Select
    
    Application.CutCopyMode = False
    
        .SaveAs Filename:=[B]ThisWorkbook.Path & "\ZCRM.xls"[/B]
        .Close
    End With
End Sub
 
Upvote 0
Thanks for the rapid response, I've got a similar problem with another piece of code. I need the .csv created by this saved in the same folder so guess I need to add [ThisWorkbook.Path] somewhere, I've tried a few combinations but can't get it to work.

Code:
Sub mysaver()
Application.Calculation = xlManual
Dim counter As Integer
counter = 9
' counter is for the number of sheets in the workbook
Do While counter <= Worksheets.Count
' Worksheets.Count represents the total number of sheets in the workbook
On Error GoTo ErrorHandler
' go to the nominated sheet
Worksheets(counter).Activate
' and save it. Simple...
ActiveSheet.SaveAs Filename:=ActiveSheet.Name, FileFormat:=xlCSV
counter = counter + 1
Loop
MsgBox "All Sheets Saved.", , "Success"
Exit Sub

ErrorHandler:
MsgBox "Error during save - Caution!", vbCritical, "Save Errors"
Exit Sub
Application.Calculation = xlAutomatic
End Sub

I'm guessing I need to add something into here
Code:
ActiveSheet.SaveAs Filename:=ActiveSheet.Name, FileFormat:=xlCSV
 
Upvote 0
Code:
ActiveSheet.SaveAs Filename:=ThisWorkbook.Path & "\" & ActiveSheet.Name, FileFormat:=xlCSV
 
Upvote 0

Forum statistics

Threads
1,224,517
Messages
6,179,240
Members
452,898
Latest member
Capolavoro009

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