VBA linked to a check box to save excel file

Jmoz092

Board Regular
Joined
Sep 8, 2017
Messages
184
Office Version
  1. 365
  2. 2011
Platform
  1. Windows
  2. MacOS
Hi , I'm having trouble creating a check box to "save as" for one of my excel projects. I want the VBA to generate the name of the file based on some cells within a sheet of the workbook. Also,some of the save path on my network can be determined by data within some cells on the same sheet. I've attempted to write a code to do this, but am not having any luck. Any help/education would be greatly appreciated.

<code>
Sub saveastest()
Dim thisfile As Range
ChDir “z:\pathfolder1\pathfolder2\pathfolder3\” & Range(“AC1”)\” & Range(“AC2”)\.Value, FileFormat:=51"
thisfile = Range("AC3").Value
ActiveWorkbook.SaveAs Filename:=thisfile, FileFormat:=51
End Sub
</code>

 
Re: Help with VBA linked to a check box to save excel file

What is the value of FilePth & thisfile?

You can see that in the Immediate Window (CTRL+G) if you add this before the SaveAs code.
Code:
Debug.Print FilePth & thisfile

PS Are you making sure FilePth ends with a '\'? If you don't then VBA will concatenate the last folder in the path with the file name and try and use that as the filename to save as.
 
Upvote 0

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Re: Help with VBA linked to a check box to save excel file

Also, it changes the file name to:
"\filepath1\filepath2\filepath3(no space or separator)AC1(no space or separator)AC2(no space or separator)AC3"
The separators have gone. I just assumed that it was the board stripping them out, but maybe not.
Code:
Sub saveastest()


    Dim thisfile As String
    Dim FilePth As String
    
    FilePth = "z:\filepath1\filepath2\filepath3[COLOR=#ff0000]\[/COLOR]_" & Range("AC1").Value & "[COLOR=#ff0000]\[/COLOR]_" & Range("AC2").Value & "[COLOR=#ff0000]\[/COLOR]_"
    thisfile = Range("AC3").Value
    ActiveWorkbook.SaveAs FilePth & thisfile, FileFormat:=51


End Sub
Remove the underscores
 
Upvote 0
Re: Help with VBA linked to a check box to save excel file

The separators have gone. I just assumed that it was the board stripping them out, but maybe not.
Code:
Sub saveastest()


    Dim thisfile As String
    Dim FilePth As String
    
    FilePth = "z:\filepath1\filepath2\filepath3[COLOR=#ff0000]\[/COLOR]_" & Range("AC1").Value & "[COLOR=#ff0000]\[/COLOR]_" & Range("AC2").Value & "[COLOR=#ff0000]\[/COLOR]_"
    thisfile = Range("AC3").Value
    ActiveWorkbook.SaveAs FilePth & thisfile, FileFormat:=51


End Sub
Remove the underscores

Maybe I worded that poorly. I meant that when I click the check box with this macro inserted into it, it changes the file name to:

"z:filepath1\filepath2\filepath3\datafromcellAC1datafromcellAC2datafromcellAC3"

Whatever the words associated with those files paths is transposed into the new file name. IOW, the new file name is a concatanation of the entire file path and the specified cells within the macro code.
 
Upvote 0
Re: Help with VBA linked to a check box to save excel file

What exactly do you see in the Immediate Window when you try this like I suggested earlier?
Code:
Debug.Print FilePth & thisfile
 
Upvote 0
Re: Help with VBA linked to a check box to save excel file

Are the values in A1 & AC sub folders?
If so you are missing the path separators.
My code in post#12 shows 3 \ in red followed by an _
You need to remove the _ & ensure that the separator \ remains
 
Upvote 0
Re: Help with VBA linked to a check box to save excel file

What exactly do you see in the Immediate Window when you try this like I suggested earlier?
Code:
Debug.Print FilePth & thisfile


I was just trying that...not seeing anything happen
 
Upvote 0
Re: Help with VBA linked to a check box to save excel file

Did you add that line of code into your code before the SaveAs and open the Immediate Window with CTRL+G to see what's output?
 
Upvote 0
Re: Help with VBA linked to a check box to save excel file

Did you add that line of code into your code before the SaveAs and open the Immediate Window with CTRL+G to see what's output?

Code:
Debug.Print FilePth & thisfile
z:\filepath1\filepath2\filepath3\September 2017\September 20,2017\lastName, firstName September-20-2017
 
Upvote 0
Re: Help with VBA linked to a check box to save excel file

Is that a valid path and is the filename what you expected?
 
Upvote 0
Re: Help with VBA linked to a check box to save excel file

Are the values in A1 & AC sub folders?
If so you are missing the path separators.
My code in post#12 shows 3 \ in red followed by an _
You need to remove the _ & ensure that the separator \ remains

The values in cells AC1 and AC2 are the names of subfolders in our network. They were named in a accordance with data that is filled into areas of our sheet...just last name, first name, day, month and year. They data is entered via a date picker in cell e4. The sheet name is changed and the last name and first name are taken from that. Then I have the AC cells pick the other cells apart and concatanate it in different ways so as to match the exact layout as the names of the networks subfolders. I did this because the form is used daily and dated differently and then stored on the network folders named in a matching manner by month (month yyyy), and day (month dd,yyyy). Then the name of the file is to become: "LastName, FirstName mm-dd-yy".

Yes, the \ are in the code that I'm using. Not sure why they're not showing in my posts, sorry.
 
Upvote 0

Forum statistics

Threads
1,223,910
Messages
6,175,316
Members
452,634
Latest member
cpostell

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