Save active workbook with existing name, but into a specific folder

2Took

Board Regular
Joined
Jun 13, 2022
Messages
203
Office Version
  1. 365
Platform
  1. Windows
Can's find syntax...
Looking to save active workbook with already existing name, but into a specific folder
e.g. if active workbook name is ABC123, I'd want it to be saved with that name, but into a specific folder.

And if I wanted to add a phrase to existing name
e.g. Make name "ABC123 - today's date"
or
"ABC123 - contents of cell A1 on Sheet1 of the same book"
 
Did you comment out the MyName= ActiveWorkbook.Name line in your code? I think it is still there and is overriding the line with the fso.

VBA Code:
Sub SaveTo_DEMO()

With CreateObject("Scripting.FileSystemObject")
'strExt = .GetExtensionName(ActiveWorkbook.FullName) 'xx1
    strExt = "." & .GetExtensionName(ActiveWorkbook.FullName) 'xx2
    MyName = .GetBaseName(ActiveWorkbook.Name)
End With

Const csPath As String = "\\ABC\target folder\"
'----->!!!!! Comment out the line below this or delete it!!!<------
'----->MyName = ActiveWorkbook.Name                         <------

'ActiveWorkbook.SaveAs Filename:=csPath & MyName & " - " & Sheets("blah").Range("H32") & "." & strExt 'xx1
ActiveWorkbook.SaveAs Filename:=csPath & MyName & " - " & Sheets("blah").Range("H32") & strExt 'xx2

End Sub
Yes, I did commented that line out, before replying back to you. I don't just blindly copy & paste the code.
There can't be both of those lines.
But it must be considering that dot as part of the BaseName, and not part of the extension -- and so it keeps it.
How do we get it out of there?
 
Upvote 0

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
It doesn't add the dot on my machine, but you can use:
VBA Code:
MyName = Left(MyName, Len(MyName) - 1)
 
Upvote 0
It doesn't add the dot on my machine, but you can use:
VBA Code:
MyName = Left(MyName, Len(MyName) - 1)
a. I'd venture a guess, your machine is set not to display file extensions? Whereas, mine does. Could that be the reason?
b. How do you combine the two lines, one that you propose now with the current code I'm running:
VBA Code:
MyName = .GetBaseName(ActiveWorkbook.Name)
 
Upvote 0
I am showing extensions, but sometimes different versions of Excel behave slightly differently. No worries.

To fix the problem, use both statements. The first will give you the name with the "." and the second will strip the "." (more accurately, it will strip the last character from MyName no matter what it is).

If this doesn't work, we need to know where the "." is coming from. So only if this code fails, Add a Watch to the MyName variable so you can see how it changes as it steps through the code.

VBA Code:
MyName = .GetBaseName(ActiveWorkbook.Name)
MyName = Left(MyName, Len(MyName) - 1)
 
Upvote 0
I am showing extensions, but sometimes different versions of Excel behave slightly differently. No worries.

To fix the problem, use both statements. The first will give you the name with the "." and the second will strip the "." (more accurately, it will strip the last character from MyName no matter what it is).

If this doesn't work, we need to know where the "." is coming from. So only if this code fails, Add a Watch to the MyName variable so you can see how it changes as it steps through the code.

VBA Code:
MyName = .GetBaseName(ActiveWorkbook.Name)
MyName = Left(MyName, Len(MyName) - 1)
Would be nice to condition for the second line to run only if last character of the BaseName is a "."
What's the syntax for that?
 
Upvote 0
Good idea. Just use the Right function.


Thanks! Came up with this, will try it out during the coming week:

VBA Code:
MyName = .GetBaseName(ActiveWorkbook.Name)

Dim AnyString, MyStr
AnyString = MyName
MyStr = Right(AnyString, 1)
If MyStr = "." Then
MyName = Left(MyName, Len(MyName) - 1)
End If
'these lines go before "End With"
 
Upvote 0
Thanks! Came up with this, will try it out during the coming week:

VBA Code:
MyName = .GetBaseName(ActiveWorkbook.Name)

Dim AnyString, MyStr
AnyString = MyName
MyStr = Right(AnyString, 1)
If MyStr = "." Then
MyName = Left(MyName, Len(MyName) - 1)
End If
'these lines go before "End With"
That worked.

Now I'm trying to say if cell H32 is empty, then append cell H33 to the file name, but get this error on both "MyRng = ActiveWorkbook.Sheets..." lines...

What's the fix?

2022-08-01_08h55_59.png



VBA Code:
Dim MyRng 'xx3
If ActiveWorkbook.Sheets("blah").Range("H32") = "" Then
MyRng = ActiveWorkbook.Sheets("blah").Range("H33)
End If
MyRng = ActiveWorkbook.Sheet("blah").Range("H32)

ActiveWorkbook.SaveAs Filename:=csPath & MyName & " - " & MyRng & strExt 'xx3
 
Upvote 0
That worked.

Now I'm trying to say if cell H32 is empty, then append cell H33 to the file name, but get this error on both "MyRng = ActiveWorkbook.Sheets..." lines...

What's the fix?

View attachment 70573


VBA Code:
Dim MyRng 'xx3
If ActiveWorkbook.Sheets("blah").Range("H32") = "" Then
MyRng = ActiveWorkbook.Sheets("blah").Range("H33)
End If
MyRng = ActiveWorkbook.Sheet("blah").Range("H32)

ActiveWorkbook.SaveAs Filename:=csPath & MyName & " - " & MyRng & strExt 'xx3

Sorry for the delay.

Try adding .value after the ranges:
ActiveWorkbook.Sheets("blah").Range("H32").Value
ActiveWorkbook.Sheets("blah").Range("H33).Value

You are testing against a range instead of the value in that range.
 
Upvote 0

Forum statistics

Threads
1,214,926
Messages
6,122,306
Members
449,079
Latest member
juggernaut24

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