Solved

define variables in an Excel macro

Posted on 2014-03-18
11
380 Views
Last Modified: 2014-03-23
I want to define 3 variables (global variables) at the beginning of a procedure to use in several macros that run as part of that procedure.  Here's what I have written, but it is incorrect (using the wrong syntax or something).  How should this be written so each variable works in the appropriate macro when that macro is called in Sub iXpenseIt2?

'Begin - define these 3 variables for your expense report
Dim name As String
name = "My Name"  'inserts your name into line 84
Dim logo As String
logo = "C:\Users\Jeff\Dropbox\Documents\Pictures\Agro-K\agro-k logo.JPG"  'inserts the path for the AGRO-K logo into line 209
Dim path As String
path = "C:\Users\Jeff\Dropbox\Documents\Agro-K\Expense Reports\14 reports\"   'inserts the path where you want to save this file into line 296
'End define variables for your expense report

Sub iXpenseIt2()
    Application.DisplayAlerts = False
    Macro6_filename_saveas
    Macro1_create_pivot_table
    Macro2_format_data
    Macro3_format_print_layout
    Macro4_delete_pivot_sheet
    Macro5_format_page_for_printing
    Macro6_filename_saveas
    Application.DisplayAlerts = True
End Sub

--------------------------------------------

following are the lines of code that do work if I type the variable information into the code directly.

Line 84:       ActiveCell.FormulaR1C1 = "My Name"
Line 209:   ActiveSheet.PageSetup.RightHeaderPicture.Filename = "C:\Users\Jeff\Dropbox\Documents\Pictures\Agro-K\agro-k logo.JPG"
Line 296:   ActiveWorkbook.SaveAs Filename:="C:\Users\Jeff\Dropbox\Documents\Agro-K\Expense Reports\14 reports\" & Format(Date, "YYMMDD") & "_JTM_expense_report", FileFormat:=xlNormal
0
Comment
Question by:mycomac
  • 4
  • 3
  • 2
  • +1
11 Comments
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 39937103
Are you wanting them to be global constants? Then you want to do it like this:
Public Const name As String = "Your name"
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 39937122
If you write only the DIMs into the module code, and then run a "macro" (read: procedure) to set those vars, it should work.
Dim name As String
Dim logo As String
Dim path As String

Sub iXpenseIt2()
    Application.DisplayAlerts = False
    name = "My Name"  'inserts your name into line 84
    logo = "C:\Users\Jeff\Dropbox\Documents\Pictures\Agro-K\agro-k logo.JPG"  'inserts the path for the AGRO-K logo into line 209
    path = "C:\Users\Jeff\Dropbox\Documents\Agro-K\Expense Reports\14 reports\"   'inserts the path where you want to save this file into line 296
    Macro6_filename_saveas
    Macro1_create_pivot_table
    Macro2_format_data
    Macro3_format_print_layout
    Macro4_delete_pivot_sheet
    Macro5_format_page_for_printing
    Macro6_filename_saveas
    Application.DisplayAlerts = True
End Sub

Open in new window

The macros need to be in the same scope (read: module file).
0
 
LVL 19

Expert Comment

by:regmigrant
ID: 39937134
Tommy's solution answers the question as written but requires you to modify the code and update the constants each time an item changes

If you want to modify them at runtime (ie: in the macro) you need to declare them as publilc without setting a value:-

- note these are before the 'sub' line in the main module

Public myName as String
Public myLogo as String
Public my Path as String

'i've added the 'my' prefix to avoid reserved words or confusion on things like name and path

After the sub you then have a form (or read a spreadsheet cell) to set them to the values you want
0
 
LVL 19

Expert Comment

by:regmigrant
ID: 39937136
crossed with qelmo
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 39937274
I think you misread the question. He doesn't want to change them at run time. He wants to set them at the top so the user doesn't have to go find them all every the user needs to change them.

That is one of the many good reasons to use constants.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Comment

by:mycomac
ID: 39937365
I apologize for my choice of words. I do believe i want to define constants so the user only modifies them once at the top.
0
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 500 total points
ID: 39937503
Yes. So this is the syntax you want
Public Const name As String = "Your name"

So it would look like this
'Begin - define these 3 variables for your expense report
Public Const name As String = "My Name"  'inserts your name into line 84
Public Const logo As String = "C:\Users\Jeff\Dropbox\Documents\Pictures\Agro-K\agro-k logo.JPG"  'inserts the path for the AGRO-K logo into line 209
Public Const path As String = "C:\Users\Jeff\Dropbox\Documents\Agro-K\Expense Reports\14 reports\"   'inserts the path where you want to save this file into line 296
'End define variables for your expense report

Open in new window

Of course, you shouldn't refer to line numbers in the comments. Just the purpose (which you have already done for most of them).
The line numbers will change as you add code (which is most likely one reason you wisely decided to use constants in the first place).
0
 
LVL 19

Expert Comment

by:regmigrant
ID: 39938125
so your users will be editing the vba? that seems...... optimistic but to each his own
0
 

Author Comment

by:mycomac
ID: 39938286
Do you have a suggestion how to do it differently that does not require collecting the constants every time?  Could we use a form to do that the first time the macro is run?
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 39938335
You can use cells in a sheet for the values and a button to trigger the macro/sub.
0
 
LVL 19

Expert Comment

by:regmigrant
ID: 39938336
The most straightforward way would be to use a worksheet_activate macro to allow data entry into defined cells and when confirmed change the cells to 'locked' and copy the values into the (publicly defined) variables (I assume you are using 'protect sheet'). To prevent update when the sheet is re-opened it should check for the 'lock', the one downside to this is if they make a mistake it will be necessary to unlock them again.

The other alternative is a form that opens on entry and perhaps allows an option to update.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now