Save As Issue in Marco

dw1256

New Member
Joined
Jun 27, 2022
Messages
12
Office Version
  1. 365
Platform
  1. Windows
I have been reading a lot of posts regarding "save as" in macros and I can't get it to work correctly. Essentially, the last thing I want my macro to do is to open the save as window and default to the current filename and path.

The first problem is I can't get the current file name to show when the save as windows opens, it is blank. The code below- does work to populate the file name field as "test":

Application.Dialogs(Application.GetSaveAsFilename(InitialFileName:="test")).Show

However, I want "test" to be the current file name, let's call it "MySaveAsMacro." So I tried this:

Dim fileName as String
fileName = ActiveWorkbook.Name
Application.Dialogs(Application.GetSaveAsFilename(InitialFileName:=fileName)).Show

When I step through the code and hover over "fileName" in the third line, it is assigned "MySaveAsMacro." However, when the save as windows opens, the File name: field is blank. Entering a random string in "" works fine, but not when using a variable.

Any ideas? I guess will tackle the path later.

Thanks.
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
Try replacing:
VBA Code:
Dim fileName as String
fileName = ActiveWorkbook.Name

with:
VBA Code:
Dim fileName    As String
Dim ary         As Variant
fileName = ActiveWorkbook.Name
ary = Split(fileName, ".")
fileName = ary(0)
 
Upvote 0
Solution
Try replacing:
VBA Code:
Dim fileName as String
fileName = ActiveWorkbook.Name

with:
VBA Code:
Dim fileName    As String
Dim ary         As Variant
fileName = ActiveWorkbook.Name
ary = Split(fileName, ".")
fileName = ary(0)
Thank you!
 
Upvote 0
Try replacing:
VBA Code:
Dim fileName as String
fileName = ActiveWorkbook.Name

with:
VBA Code:
Dim fileName    As String
Dim ary         As Variant
fileName = ActiveWorkbook.Name
ary = Split(fileName, ".")
fileName = ary(0)
I have a follow up here. Sometimes the filename has "." contained within, therefore this string might only return part of the filename.

For example, "my.test.xlsx" will return "my"

Any idea how to tweak that?

Thanks.
 
Upvote 0
Replace:
VBA Code:
Dim fileName as String
fileName = ActiveWorkbook.Name

with:
VBA Code:
    Dim FileName        As String
    FileName = Left$(ActiveWorkbook.Name, InStrRev(ActiveWorkbook.Name, ".") - 1)
 
Upvote 0

Forum statistics

Threads
1,215,563
Messages
6,125,560
Members
449,237
Latest member
Chase S

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