Solved

define variables in an Excel macro

Posted on 2014-03-18
11
383 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 69

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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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
 

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 69

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

825 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