+ Reply to Thread
Results 1 to 10 of 10

Using Variables and calling subs

  1. #1
    Registered User
    Join Date
    01-31-2008
    MS-Off Ver
    2010
    Posts
    38

    Using Variables and calling subs

    I'm having some trouble getting to grips with using/calling variables and other sub routines. I thought I'd use the following bits of code as an example of what I currently have and with your help how you think it SHOULD read.

    Please Login or Register  to view this content.
    As you will no doubt guess each Private Sub is attached to a command button, when you click the button it checks to see if the selected cell is within a range and if it is changes it's colour. There are more than 2 buttons in total by the way, I've just using 2 as an example (hence my desire to write tighter code).

    The only code difference in the routines is the colour that the cell is changed to, so it would make sense to use variables but I'm falling down at this point as to how to declare them. Also you'll notice the range that the function checks is the same, again a variable for this seems logical called from outside the routine this time?

    The way I would expect the whole thing to work is have 1 sub routine written elsewhere that's called when the command button is clicked. The colours would be a variable at the start too. I'm thinking something along the lines of the following (naturally the code is just a concept):

    Please Login or Register  to view this content.
    If anyone can spare the time to show me how to improve this code I'd be grateful.

    Thanks in advance.

    Brelin

  2. #2
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    I think you want this

    Please Login or Register  to view this content.
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  3. #3
    Registered User
    Join Date
    01-31-2008
    MS-Off Ver
    2010
    Posts
    38
    Thanks for that reply Roy ...I was so close but so far away lol.

    I'm particularly interested in the opener:

    Option Explicit
    Dim intColour

    ...is this how you declare the variable 'globally' so to speak, so you only need reference the actual value in the individual code later? (e.g. intColour = 1)

    Also there is no declaration of the type of variable after 'Dim intColour' either, so does this mean that's it's not always needed?

    Thanks again

    Brelin

  4. #4
    Forum Contributor Tarball's Avatar
    Join Date
    04-21-2007
    Location
    Atlanta, GA
    Posts
    166
    The "Option Explicit" is a pragma that tells Excel that all variables in the routines behind the worksheet must be declared. This is actually optional but you should know that declaring your variables will improve performance. As you noted, you do not have to declare a type. I generally avoid doing this as Excel will declare the type internallly and will then have to do its own type conversions implicitly. Again you can improve performance by declaring variables and their type. Lastly, you can simply initialize variables after their declaration or internal to the declaration. Variables defined outside of a SUB or Function are global to the workbook, It is not generally considered good programming practice to use global variables but used wisely you shouldnt have any issues.
    Reach me at excel_help at bellsouth dot net

  5. #5
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Quote Originally Posted by Brelin View Post
    Thanks for that reply Roy ...I was so close but so far away lol.

    I'm particularly interested in the opener:

    Option Explicit
    Dim intColour

    ...is this how you declare the variable 'globally' so to speak, so you only need reference the actual value in the individual code later? (e.g. intColour = 1)

    Also there is no declaration of the type of variable after 'Dim intColour' either, so does this mean that's it's not always needed?

    Thanks again

    Brelin
    I would have declared intColour as a Integer or Long because the default would be a Variant. See this for more information

    http://www.excel-it.com/clear_code.htm

  6. #6
    Registered User
    Join Date
    01-31-2008
    MS-Off Ver
    2010
    Posts
    38
    Thanks for all the responces, I'm going to have a look at this at lunch today and apply it throught my spreadsheet.

    Thanks again

    Brelin

  7. #7
    Forum Contributor
    Join Date
    02-23-2006
    Location
    Near London, England
    MS-Off Ver
    Office 2003
    Posts
    770
    Is there any reason for declaring intColour Globally?

    I would have done something more like:

    Please Login or Register  to view this content.
    That way ChangeColour couldn't be called without specifying a colour. Coded the other way there is the possibility of called ChangeColour wihtout having yet given a valid colour value to intColour.
    Last edited by Phil_V; 08-20-2008 at 04:44 AM.

  8. #8
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    The code is called by buttons, each button appears to be setting the colour so it didn't look as if what you are suggesting could happen

  9. #9
    Forum Contributor
    Join Date
    02-23-2006
    Location
    Near London, England
    MS-Off Ver
    Office 2003
    Posts
    770
    Quote Originally Posted by royUK View Post
    The code is called by buttons, each button appears to be setting the colour so it didn't look as if what you are suggesting could happen
    Ok, I guess I'm just used to pain-in-the-backside-users going poking in the 'macro' menu

  10. #10
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    You can use Option Private to prevent macros showing in the menu.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Calling row with variables
    By eyecalibrate in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-19-2007, 11:19 AM
  2. Passing variables to Range function?
    By svaiskau in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-13-2007, 11:29 AM
  3. proper format for functions
    By DKY in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-13-2007, 08:39 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1