Dynamic Macro with some content in an Excel cell

mad4max

New Member
Joined
Mar 30, 2014
Messages
18
Hi people,

I need your help to improve my Excel sheet.

I use some small macros to save the file with a specific name in a specific folder, determined by variables.

In colum S you’ll find the Directory where macro should save the Excel file and the PDF file.

Also, you’ll find the file name which is composed of the content of several cells.

The Macro SaveOFFER() look into the sheet OFFRE, in column S where are info.

The problem is that I would like to move the content of the column R and S from the sheet OFFRE into the sheet PARAM so I’m more flexible if I add lines or columns.

I can’t find the way to say in the macro that it has to look into another sheet to print or save the file…

Here is the macro:

Sub SaveOFFER()

'-----------------------------------------------------------------------------------------------------------+
' Sauvegarde l'offre dans le répertoire facturation avec le bon nom de fichier
'-----------------------------------------------------------------------------------------------------------+

Range("C3").Select
ChDir Range("S6")
ActiveWorkbook.SaveAs Filename:=Range("S6") & Range("S7")


Explanation of what I need:

Range("C3").Select
Here it should go to Param!C3 instead of just C3

ChDir Range("S6")
Here it should go to Offre!S6 instead of just S6

ActiveWorkbook.SaveAs Filename:=Range("S6") & Range("S7")
Here it should stay in the sheet OFFRE, but use the value stored in Param!S6 and Param!S7 instead of just S6 and S7.

I hope that it’s clear. If not, ask me and I can send a copy of the Excel file I use...

Any better solution, is welcome.

Another solution could be that if I move anywhere the content of column R + S, the macro would also change it’s value. I don’t know if it’s possible in VBA to have dynamic references.

In Excel, when you move a the content of any cell that is in a formula, the formula is automatically adapted to the new location. What about VBA?

Thank you very much for your valuable help!

Best regards!
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Perhaps...
VBA Code:
Sub madmax()
' Not sure why you need to activate/select Param!C3 here...
With Sheets("Param")
    .Activate
    .Range("C3").Select
End With

With Sheets("Offre")
    ChDir (.Range("S6").Value)
    ActiveWorkbook.SaveAs Filename:=.Range("S6").Value & .Range("S7").Value
End With

End Sub
 
Upvote 0
Perhaps...
VBA Code:
Sub madmax()
' Not sure why you need to activate/select Param!C3 here...
With Sheets("Param")
    .Activate
    .Range("C3").Select
End With

With Sheets("Offre")
    ChDir (.Range("S6").Value)
    ActiveWorkbook.SaveAs Filename:=.Range("S6").Value & .Range("S7").Value
End With

End Sub
Hello,

There was a little mistake, maybe because I didn't myself explained well, due to my poor English... :(

I need to activate/select Offer!C3 as I want to file to always open on C3 in sheet Offer, which contains the Offer number. So, creating a new offer, can't forget to change the offer number to avoid duplicates.

I used your suggestion, changing some things and it work perfectly:

Sub SaveOFFER()
'-----------------------------------------------------------------------------------------------------------+
' Sauvegarde l'offre dans le répertoire facturation avec le bon nom de fichier
'-----------------------------------------------------------------------------------------------------------+
'
' Select the cell that where the file should open every time.
'
With Sheets("Offre")
.Activate
.Range("C3").Select
End With

'
' Select the sheet where the info like directory and filename is stored.
' and use the content of the specific cell (E10 + E11) as variable
'
With Sheets("Param")
ChDir (.Range("E10").Value)
ActiveWorkbook.SaveAs Filename:=.Range("E10").Value & .Range("E11").Value
End With

End Sub

The file is saved in right place, correctly...

BUT, I have a problem trying to save the file as .PDF file!

I tried to adapt your solution to my VBA code to create the .PDF file, but I get an error: "Compile Error. Sub or Function not defined"... :(

Here is the macro:

Sub SaveToPDF()
'
' SaveToPDF Macro
'
'
' Select the cell that where the file should open every time.
'
With Sheets("Offre")
.Activate
.Range("C3").Select
End With

'
' Select the sheet where the info like directory and filename is stored.
' and use the content of the specific cell (E12 + E13) in the sheet PARAM as variable

With Sheets("Param")
ChDir (.Range("E12").Value)
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF,Range("E12").Value & .Range("E13").Value _
, Quality:=xlQualityStandard, IncludeDocProperties:=True, IgnorePrintAreas _
:=False, OpenAfterPublish:=False

End With

I don't understand where is the mistake...
Also, is there maybe another way to save the file in a .PDF file as the one I used?

Thank you for your help!

Best regards!
 
Upvote 0
In the 'ActiveSheet.ExportAsFixedFormat' line, you haven't specified the "Filename:=" parameter like you did for "Type:=", "Quality:=", etc. You also left off the period before Range("E12").

Try:
VBA Code:
ActiveSheet.ExportAsFixedFormat Type:=xlTypePDF, Filename:=.Range("E12").Value & .Range("E13").Value & ".pdf" _
If your file name in E13 already has the ".pdf" at the end, then you can remove the & ".pdf" bit (leave the underscore at the end of the line, though.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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