VB code nearly there

c hassett

New Member
Joined
Jul 17, 2007
Messages
27
Hi all,
I'm taking my first steps into VB code and had some success, until now.
I found some code on here for saving a workbook to multiple locations with the addition to the file name of the date

Code:
ActiveWorkbook.SaveCopyAs Filename:="O:\MRC\Development" & _
bdFileName & " BU " & Format(Now, " yyyy-mm-dd") & _
".xls"

And I recorded a Macro where I lock all the cells, protect the workbook and all the sheets and save that to another drive as a read only file with password to modify.

What I'd like to do is add to the recorded Macros generated file name Now " yyyy-mm-dd". Haven't worked out how, it gets to the part about password:="" and stops.

Ultimatly I want the the two as one so that it generates the BU with the Now first, then adds all the protection to the workbook and saves the read only version to the other drive.

I'm confident it can do it, I'm just not sure how to tell it to.
Thanks in advance,
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
As long as each routine works, you should be able to use them in turn.

To save as read-only you just want to set the argument in the SaveAs statement:

Code:
Sub Macro1()

    ActiveWorkbook.SaveAs Filename:= "C:\Alex.xls", WriteResPassword:="mypassword"
End Sub

The macro recorder sets the password to "" but this really means no password. The password argument refers to a password to open the file. the WriteResPassword argument refers to a password to open the file with write access (this is where you get the option to open read only).

For filename just build the string you need:
Code:
myString = "C:\Alex" & & Format(Now, "yyyy-mm-dd") & "_BU.xls"
ActiveWorkbook.SaveAs Filename:=myString

Don't get confused about the differences between Save, SaveAs, and SaveCopyAs...each acts slightly differently, but all need filenames and file locations. Also think about what Excel is doing with its location when you save in different places and try not to make inaccurate assumptions about where things are being saved or what is the active workbook.

Regards.
 
Upvote 0

Forum statistics

Threads
1,222,239
Messages
6,164,779
Members
451,914
Latest member
mdfariborz

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