vba to save excel file with given name to specific location
I recently asked about saving an excel file with a specific name to a set location:
Getting correct default save name and save directory with spaces in VBA
I want to use this same procedure but in a somewhat different way. I tried to edit the code to make it work, but I keep on have a black filename screen when I execute the code.
The file I want to save with the routine is a template which is being refreshed with new data every 4 weeks. It is a read-only file which functions as a source template and after updating the data, I has to be saved on a different location to keep the original source file save from errors/unwanted modification.
When refreshing, the template opens a file which contains the refresh script named "refresh_segment_template.xlsm".
the code in the template is:
Option Explicit
Dim aantalrijen As Long
Const SheetSchaduwblad As String = "schaduwblad"
-------
Sub vernieuwalles()
Dim myTemplate As String: myTemplate = ActiveWorkbook.Name
Dim myTool As String: myTool = "refresh_segment_template.xlsm"
Application.ScreenUpdating = False
Workbooks.Open GetPath & myTool
Application.Run myTool & "!vernieuwalles", myTemplate
Call Windows(myTool).Close(False)
Application.ScreenUpdating = True
End Sub
Private Function GetPath() As String
Dim myPosition As Integer
Dim myPath As String: myPath = ActiveWorkbook.Path
myPosition = InStr(StrReverse(myPath), "") - 1
myPosition = Len(myPath) - myPosition
GetPath = Mid(myPath, 1, myPosition - 1) & "XLAM"
End Function
the code in the refresh_segment_template is:
Option Explicit
Dim aantalrijen As Long
Const SheetSchaduwblad As String = "schaduwblad"
------------------
Sub vernieuwalles(mytemplate As String)
Windows(mytemplate).Activate
On Error GoTo Err_
Application.StatusBar = "Bezig met vernieuwen"
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
' Call SheetOpschonen
Call datawissen
Call dataplaatsen
Call kolomtitels
Call toevoegen
Call maaktabel
Call refreshpivots
Exit_:
Application.StatusBar = ""
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Exit Sub
Err_:
Call MsgBox(Err.Number & vbCrLf & Err.Description)
Resume Exit_
Application.Calculation = xlCalculationAutomatic
End Sub
-------------
Sub refreshpivots()
Dim workbook_Name As Variant
Dim location As String
Dim filename As String
filename = "M:CommercieMarktdataIRiSegment Ontwikkeling" & ActiveWorkbook.Name
ActiveWorkbook.RefreshAll
workbook_Name = Application.GetSaveAsFilename(fileFilter:="Excel binary sheet (*.xlsb), *.xlsb", InitialFileName:="M:CommercieMarktdataIRiSegment Ontwikkeling")
If workbook_Name <> False Then
ActiveWorkbook.SaveAs filename:=filename, WriteResPassword:="TM", FileFormat:=50
End If
End Sub
I am now wondering how I can make the last script to use the filename of the template and save it on the give location in the script (i.e. M:CommercieMarktdataIRiSegment Ontwikkeling).
When I execute the code above, I get a 'save as' screen, but with no filename given, only the set directory is correct.
The refresh_segment_template is a .xlsm file. The template is a .xlsb file.
excel vba filenames excel-2016 savefiledialog
add a comment |
I recently asked about saving an excel file with a specific name to a set location:
Getting correct default save name and save directory with spaces in VBA
I want to use this same procedure but in a somewhat different way. I tried to edit the code to make it work, but I keep on have a black filename screen when I execute the code.
The file I want to save with the routine is a template which is being refreshed with new data every 4 weeks. It is a read-only file which functions as a source template and after updating the data, I has to be saved on a different location to keep the original source file save from errors/unwanted modification.
When refreshing, the template opens a file which contains the refresh script named "refresh_segment_template.xlsm".
the code in the template is:
Option Explicit
Dim aantalrijen As Long
Const SheetSchaduwblad As String = "schaduwblad"
-------
Sub vernieuwalles()
Dim myTemplate As String: myTemplate = ActiveWorkbook.Name
Dim myTool As String: myTool = "refresh_segment_template.xlsm"
Application.ScreenUpdating = False
Workbooks.Open GetPath & myTool
Application.Run myTool & "!vernieuwalles", myTemplate
Call Windows(myTool).Close(False)
Application.ScreenUpdating = True
End Sub
Private Function GetPath() As String
Dim myPosition As Integer
Dim myPath As String: myPath = ActiveWorkbook.Path
myPosition = InStr(StrReverse(myPath), "") - 1
myPosition = Len(myPath) - myPosition
GetPath = Mid(myPath, 1, myPosition - 1) & "XLAM"
End Function
the code in the refresh_segment_template is:
Option Explicit
Dim aantalrijen As Long
Const SheetSchaduwblad As String = "schaduwblad"
------------------
Sub vernieuwalles(mytemplate As String)
Windows(mytemplate).Activate
On Error GoTo Err_
Application.StatusBar = "Bezig met vernieuwen"
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
' Call SheetOpschonen
Call datawissen
Call dataplaatsen
Call kolomtitels
Call toevoegen
Call maaktabel
Call refreshpivots
Exit_:
Application.StatusBar = ""
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Exit Sub
Err_:
Call MsgBox(Err.Number & vbCrLf & Err.Description)
Resume Exit_
Application.Calculation = xlCalculationAutomatic
End Sub
-------------
Sub refreshpivots()
Dim workbook_Name As Variant
Dim location As String
Dim filename As String
filename = "M:CommercieMarktdataIRiSegment Ontwikkeling" & ActiveWorkbook.Name
ActiveWorkbook.RefreshAll
workbook_Name = Application.GetSaveAsFilename(fileFilter:="Excel binary sheet (*.xlsb), *.xlsb", InitialFileName:="M:CommercieMarktdataIRiSegment Ontwikkeling")
If workbook_Name <> False Then
ActiveWorkbook.SaveAs filename:=filename, WriteResPassword:="TM", FileFormat:=50
End If
End Sub
I am now wondering how I can make the last script to use the filename of the template and save it on the give location in the script (i.e. M:CommercieMarktdataIRiSegment Ontwikkeling).
When I execute the code above, I get a 'save as' screen, but with no filename given, only the set directory is correct.
The refresh_segment_template is a .xlsm file. The template is a .xlsb file.
excel vba filenames excel-2016 savefiledialog
Backslashes are generally used as escape characters (which mean they give another meaning to the succeeding character). Can you replace every single backslash by a double one? (M:\Commercie\Markdata\...
)
– Dominique
Nov 26 '18 at 13:55
getting the location works fine, however getting the right filename is not working. In the case of the mentioned question, I have troubles because of the file type and the desired name (xlsm vs xlsb). Because this template (.xlsb) uses a separate update script (xlsm) in I am wondering if this update script interferes with the save as script and causes a file extension conflict.
– DutchArjo
Nov 26 '18 at 14:35
1
@Dominique, backslashes are escape characters in C and some other languages, not in VB.
– Paul Ogilvie
Nov 26 '18 at 15:04
add a comment |
I recently asked about saving an excel file with a specific name to a set location:
Getting correct default save name and save directory with spaces in VBA
I want to use this same procedure but in a somewhat different way. I tried to edit the code to make it work, but I keep on have a black filename screen when I execute the code.
The file I want to save with the routine is a template which is being refreshed with new data every 4 weeks. It is a read-only file which functions as a source template and after updating the data, I has to be saved on a different location to keep the original source file save from errors/unwanted modification.
When refreshing, the template opens a file which contains the refresh script named "refresh_segment_template.xlsm".
the code in the template is:
Option Explicit
Dim aantalrijen As Long
Const SheetSchaduwblad As String = "schaduwblad"
-------
Sub vernieuwalles()
Dim myTemplate As String: myTemplate = ActiveWorkbook.Name
Dim myTool As String: myTool = "refresh_segment_template.xlsm"
Application.ScreenUpdating = False
Workbooks.Open GetPath & myTool
Application.Run myTool & "!vernieuwalles", myTemplate
Call Windows(myTool).Close(False)
Application.ScreenUpdating = True
End Sub
Private Function GetPath() As String
Dim myPosition As Integer
Dim myPath As String: myPath = ActiveWorkbook.Path
myPosition = InStr(StrReverse(myPath), "") - 1
myPosition = Len(myPath) - myPosition
GetPath = Mid(myPath, 1, myPosition - 1) & "XLAM"
End Function
the code in the refresh_segment_template is:
Option Explicit
Dim aantalrijen As Long
Const SheetSchaduwblad As String = "schaduwblad"
------------------
Sub vernieuwalles(mytemplate As String)
Windows(mytemplate).Activate
On Error GoTo Err_
Application.StatusBar = "Bezig met vernieuwen"
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
' Call SheetOpschonen
Call datawissen
Call dataplaatsen
Call kolomtitels
Call toevoegen
Call maaktabel
Call refreshpivots
Exit_:
Application.StatusBar = ""
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Exit Sub
Err_:
Call MsgBox(Err.Number & vbCrLf & Err.Description)
Resume Exit_
Application.Calculation = xlCalculationAutomatic
End Sub
-------------
Sub refreshpivots()
Dim workbook_Name As Variant
Dim location As String
Dim filename As String
filename = "M:CommercieMarktdataIRiSegment Ontwikkeling" & ActiveWorkbook.Name
ActiveWorkbook.RefreshAll
workbook_Name = Application.GetSaveAsFilename(fileFilter:="Excel binary sheet (*.xlsb), *.xlsb", InitialFileName:="M:CommercieMarktdataIRiSegment Ontwikkeling")
If workbook_Name <> False Then
ActiveWorkbook.SaveAs filename:=filename, WriteResPassword:="TM", FileFormat:=50
End If
End Sub
I am now wondering how I can make the last script to use the filename of the template and save it on the give location in the script (i.e. M:CommercieMarktdataIRiSegment Ontwikkeling).
When I execute the code above, I get a 'save as' screen, but with no filename given, only the set directory is correct.
The refresh_segment_template is a .xlsm file. The template is a .xlsb file.
excel vba filenames excel-2016 savefiledialog
I recently asked about saving an excel file with a specific name to a set location:
Getting correct default save name and save directory with spaces in VBA
I want to use this same procedure but in a somewhat different way. I tried to edit the code to make it work, but I keep on have a black filename screen when I execute the code.
The file I want to save with the routine is a template which is being refreshed with new data every 4 weeks. It is a read-only file which functions as a source template and after updating the data, I has to be saved on a different location to keep the original source file save from errors/unwanted modification.
When refreshing, the template opens a file which contains the refresh script named "refresh_segment_template.xlsm".
the code in the template is:
Option Explicit
Dim aantalrijen As Long
Const SheetSchaduwblad As String = "schaduwblad"
-------
Sub vernieuwalles()
Dim myTemplate As String: myTemplate = ActiveWorkbook.Name
Dim myTool As String: myTool = "refresh_segment_template.xlsm"
Application.ScreenUpdating = False
Workbooks.Open GetPath & myTool
Application.Run myTool & "!vernieuwalles", myTemplate
Call Windows(myTool).Close(False)
Application.ScreenUpdating = True
End Sub
Private Function GetPath() As String
Dim myPosition As Integer
Dim myPath As String: myPath = ActiveWorkbook.Path
myPosition = InStr(StrReverse(myPath), "") - 1
myPosition = Len(myPath) - myPosition
GetPath = Mid(myPath, 1, myPosition - 1) & "XLAM"
End Function
the code in the refresh_segment_template is:
Option Explicit
Dim aantalrijen As Long
Const SheetSchaduwblad As String = "schaduwblad"
------------------
Sub vernieuwalles(mytemplate As String)
Windows(mytemplate).Activate
On Error GoTo Err_
Application.StatusBar = "Bezig met vernieuwen"
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
' Call SheetOpschonen
Call datawissen
Call dataplaatsen
Call kolomtitels
Call toevoegen
Call maaktabel
Call refreshpivots
Exit_:
Application.StatusBar = ""
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Exit Sub
Err_:
Call MsgBox(Err.Number & vbCrLf & Err.Description)
Resume Exit_
Application.Calculation = xlCalculationAutomatic
End Sub
-------------
Sub refreshpivots()
Dim workbook_Name As Variant
Dim location As String
Dim filename As String
filename = "M:CommercieMarktdataIRiSegment Ontwikkeling" & ActiveWorkbook.Name
ActiveWorkbook.RefreshAll
workbook_Name = Application.GetSaveAsFilename(fileFilter:="Excel binary sheet (*.xlsb), *.xlsb", InitialFileName:="M:CommercieMarktdataIRiSegment Ontwikkeling")
If workbook_Name <> False Then
ActiveWorkbook.SaveAs filename:=filename, WriteResPassword:="TM", FileFormat:=50
End If
End Sub
I am now wondering how I can make the last script to use the filename of the template and save it on the give location in the script (i.e. M:CommercieMarktdataIRiSegment Ontwikkeling).
When I execute the code above, I get a 'save as' screen, but with no filename given, only the set directory is correct.
The refresh_segment_template is a .xlsm file. The template is a .xlsb file.
excel vba filenames excel-2016 savefiledialog
excel vba filenames excel-2016 savefiledialog
asked Nov 26 '18 at 13:39
DutchArjoDutchArjo
1071316
1071316
Backslashes are generally used as escape characters (which mean they give another meaning to the succeeding character). Can you replace every single backslash by a double one? (M:\Commercie\Markdata\...
)
– Dominique
Nov 26 '18 at 13:55
getting the location works fine, however getting the right filename is not working. In the case of the mentioned question, I have troubles because of the file type and the desired name (xlsm vs xlsb). Because this template (.xlsb) uses a separate update script (xlsm) in I am wondering if this update script interferes with the save as script and causes a file extension conflict.
– DutchArjo
Nov 26 '18 at 14:35
1
@Dominique, backslashes are escape characters in C and some other languages, not in VB.
– Paul Ogilvie
Nov 26 '18 at 15:04
add a comment |
Backslashes are generally used as escape characters (which mean they give another meaning to the succeeding character). Can you replace every single backslash by a double one? (M:\Commercie\Markdata\...
)
– Dominique
Nov 26 '18 at 13:55
getting the location works fine, however getting the right filename is not working. In the case of the mentioned question, I have troubles because of the file type and the desired name (xlsm vs xlsb). Because this template (.xlsb) uses a separate update script (xlsm) in I am wondering if this update script interferes with the save as script and causes a file extension conflict.
– DutchArjo
Nov 26 '18 at 14:35
1
@Dominique, backslashes are escape characters in C and some other languages, not in VB.
– Paul Ogilvie
Nov 26 '18 at 15:04
Backslashes are generally used as escape characters (which mean they give another meaning to the succeeding character). Can you replace every single backslash by a double one? (
M:\Commercie\Markdata\...
)– Dominique
Nov 26 '18 at 13:55
Backslashes are generally used as escape characters (which mean they give another meaning to the succeeding character). Can you replace every single backslash by a double one? (
M:\Commercie\Markdata\...
)– Dominique
Nov 26 '18 at 13:55
getting the location works fine, however getting the right filename is not working. In the case of the mentioned question, I have troubles because of the file type and the desired name (xlsm vs xlsb). Because this template (.xlsb) uses a separate update script (xlsm) in I am wondering if this update script interferes with the save as script and causes a file extension conflict.
– DutchArjo
Nov 26 '18 at 14:35
getting the location works fine, however getting the right filename is not working. In the case of the mentioned question, I have troubles because of the file type and the desired name (xlsm vs xlsb). Because this template (.xlsb) uses a separate update script (xlsm) in I am wondering if this update script interferes with the save as script and causes a file extension conflict.
– DutchArjo
Nov 26 '18 at 14:35
1
1
@Dominique, backslashes are escape characters in C and some other languages, not in VB.
– Paul Ogilvie
Nov 26 '18 at 15:04
@Dominique, backslashes are escape characters in C and some other languages, not in VB.
– Paul Ogilvie
Nov 26 '18 at 15:04
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53482356%2fvba-to-save-excel-file-with-given-name-to-specific-location%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53482356%2fvba-to-save-excel-file-with-given-name-to-specific-location%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Backslashes are generally used as escape characters (which mean they give another meaning to the succeeding character). Can you replace every single backslash by a double one? (
M:\Commercie\Markdata\...
)– Dominique
Nov 26 '18 at 13:55
getting the location works fine, however getting the right filename is not working. In the case of the mentioned question, I have troubles because of the file type and the desired name (xlsm vs xlsb). Because this template (.xlsb) uses a separate update script (xlsm) in I am wondering if this update script interferes with the save as script and causes a file extension conflict.
– DutchArjo
Nov 26 '18 at 14:35
1
@Dominique, backslashes are escape characters in C and some other languages, not in VB.
– Paul Ogilvie
Nov 26 '18 at 15:04