vba code save multiple

Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » VBA Code to Save multiple files depending on multiple data valid

VBA Code to Save multiple files depending on multiple data valid

resolvedResolved · Urgent Priority · Version 2010

Bylan has attended:
Excel Dashboards for Business Intelligence course

VBA Code to Save multiple files depending on multiple data valid

Hi, I am working on some code which will save many versions of the same file in to different folders depending on two data validation drop down lists which are linked together.

I have managed to code a macro to save all the files from the second drop down, but I need to manually alter the original (first) drop down list for the Macro to work.

Also I need to save the files in to a specific folder structure depending on the drop down lists.

Hope this makes sense.

RE: VBA Code to Save multiple files depending on multiple data v

Hi Bylan,

Thank you for the forum question.

You want to save a file in a number of specific folders depending on selections in two linked drop down lists (datavalidation).


To me it sounds like you need a decision code.

If range("cell with the first drop down list")="Test" and range("cell with the second drop down list")="Test2" then

activeworkbook.saveas FileName:="c:\folder\file.xlsm"

elseif range("cell with the first drop down list")="Test3" and range("cell with the second drop down list")="Test4" then

activeworkbook.saveas FileName:="c:\folder2\file.xlsm"
end if


I hope this will guide you in the right direction.


Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

RE: VBA Code to Save multiple files depending on multiple data v

Thanks Jens,

That's very helpful, but the drop down has 211 different options so writing the if statement for each will take a while.

Is there a loop that I could go through so it selects from the list at source?

Many Thanks for your help.

RE: VBA Code to Save multiple files depending on multiple data v

Hi Bylan,

With 211 different options I would organise the options (the ranges where you have the sources for the datavalidation lists) and assign range names to group my different options with different range names.

Then you could use the for each loop to run through each range name. I do not know how many different folders you need to save the different versions in, but if it is few I would do like this.

For each i in range("name of the first range")

if i.value="The cell reference of the cell with the first drop down list" and i.value="The cell reference of the cell with the second drop down list" then

activeworkbook.saveas FileName:="c:\folder1\file.xlsm"

end if
next i

For each i in range("name of the second range")

if i.value="The cell reference of the cell with the first drop down list" and i.value="The cell reference of the cell with the second drop down list" then

activeworkbook.saveas FileName:="c:\folder2\file.xlsm"

end if
next i

For each i in range("name of the third range")

if i.value="The cell reference of the cell with the first drop down list" and i.value="The cell reference of the cell with the second drop down list" then

activeworkbook.saveas FileName:="c:\folder3\file.xlsm"

end if
next i

You can also use an array to store the different options in the computer's memory, but then I will suggest that you have our Excel Advanced VBA course where we are working with arrays.

I hope this can give you some ideas to do what you want.

Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

RE: VBA Code to Save multiple files depending on multiple data v

Jens,

Thank you very much for your replies. I have one more question regarding specifying the filename.

I have created the following macro which has an inner and outer loop to select all my outputs. I need to save each one specific to certain folders and therefore I need the Filename to have a dynamic file path..

So if for example we are in Region "NORTH" the file needs to be saved in the folder which is the NORTH.

I am struggling to work this out. My code is below. I need to know how to make the filename dynamic.

Dim i, j As Integer
Dim regionname As String
Dim CCGname As String
Dim Areateam As String
Dim quater As String
Dim Year As String

i = 6
j = 6
quater = Sheets("COVER PAGE").Range("$J$3")
Year = Sheets("COVER PAGE").Range("$I$3")
While Worksheets("OrgSelection").Range("B" & i) <> ""
Worksheets("COVER PAGE").Range("D2") = Worksheets("OrgSelection").Range("B" & i)
regionname = Worksheets("COVER PAGE").Range("D2")
While Worksheets("OrgSelection").Range("D" & j) <> ""
Worksheets("COVER PAGE").Range("D3") = Worksheets("OrgSelection").Range("D" & j)

CCGname = Worksheets("COVER PAGE").Range("D3")

Application.Calculate
Application.CalculateUntilAsyncQueriesDone

Areaname = Application.WorksheetFunction.VLookup(Worksheets("COVER PAGE").Range("D3"), Worksheets("lookup").Range("B1:F212"), 5, False)

ActiveWorkbook.SaveAs Filename:="\\DCSPSQL02\kis\London Region\Delivery Dashboard\Delivery Dashboard Q1-Q4 2014-15 & "\regionname\ & "CCGname" & " DD", FileFormat:=52, CreateBackup:=False



' Next CCG
j = j + 1
Wend
' Next Region
i = i + 1
j = 6
Wend


End Sub


RE: VBA Code to Save multiple files depending on multiple data v

Hi Bylan,


To create dynamic file names I normally use the Date or Now function. If you need to save the file many times a day you will need the Now function if not more than ones a day the date function will be fine.

The code below will save the file with a new name each time you save it. You can add more to the Format function if you would like the whole date you saved the file Format(Date,"dd-mm-yyyy").


Dim strFilename As String
strFilename = Range("a1") & Format(Date, "DD")


ActiveWorkbook.SaveAs "C:\Users\jens\Documents\" & strFilename, FileFormat:=52



Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

RE: VBA Code to Save multiple files depending on multiple data v

Hi Jens,

As apposed to the file name being dynamic I need the folders in the file path to be dynamic, so sometimes the file would be saved in a London folder for example and sometimes in a midlands folder.

Regards

RE: VBA Code to Save multiple files depending on multiple data v

Hi Bylan,

You will need to do something like this to build the dynamic path. In my example I have the region in A1 and I also want the region to be a part of the file name.



Dim strFilename As String
Dim strRegion As String

strRegion = Range("a1")

strFilename = Range("a1") & Format(Date, "DD")


ActiveWorkbook.SaveAs "C:\Users\jens\Documents\" & strRegion & "\" & strFilename, FileFormat:=52



Kind regards

Jens Bonde
Microsoft Office Specialist Trainer

Tel: 0207 987 3777
Best STL - https://www.stl-training.co.uk
98%+ recommend us

London's leader with UK wide delivery in Microsoft Office training and management training to global brands, FTSE 100, SME's and the public sector

 

Training courses

 

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.


 

Excel tip:

Hiding a worksheet in Excel

Want to circulate an Excel workbook file but keep certain worksheets hidden from your colleagues' view?

You can do so by bringing up the sheet you wish to hide on your screen; then going to Format - Sheet - Hide.

It will not be immediately obvious that a sheet is hidden from view unless perhaps the sheet are still labelled Sheet 1, Sheet 2 etc.

To display the sheet again, you can go to Format - Sheet - Unhide on any of the other sheets in the workbook. A dialogue box will appear, allowing you to select the hidden sheet/s. Click OK to make the sheet/s reappear again.

View all Excel hints and tips


Server loaded in 0.08 secs.