+ Reply to Thread
Results 1 to 25 of 25

Silly Question about variables

  1. #1
    Registered User
    Join Date
    03-21-2015
    Location
    GrassyKnoll
    MS-Off Ver
    2010
    Posts
    82

    Question Silly Question about variables

    What is the consequence of declaring a lot of variables?
    Just asking because if a condition is met then I would like to assign or set them.
    But even if I dont assign them I still see the declarations in the local window.
    Am I doing this right?
    Or
    Should I be calling a different procedure if the condition is met then declare and assign or set the variables in that procedure?

  2. #2
    Registered User
    Join Date
    03-21-2015
    Location
    GrassyKnoll
    MS-Off Ver
    2010
    Posts
    82

    Re: Silly Question about variables

    Or should I be creating functions then calling the function?

    Or is all this a matter of taste?

  3. #3
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Silly Question about variables

    Aside from the fact that unused variables are confusing when you or someone else revisits your code a year later, the only consequence is memory usage.
    Entia non sunt multiplicanda sine necessitate

  4. #4
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Silly Question about variables

    Hi xlBunny,

    Are you using VBA and declaring variables or are you using the Names Manager and declaring them?
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  5. #5
    Registered User
    Join Date
    03-21-2015
    Location
    GrassyKnoll
    MS-Off Ver
    2010
    Posts
    82

    Re: Silly Question about variables

    consequence is memory usage.
    I was hoping that wasnt the case. I guess that I do need to break this down to small procedures & create some function to call on.
    Aside from the fact that unused variables are confusing when you or someone else revisits your code
    Yup it confusing to me now that why I was asking. I was willing to brush it off if there was no cost to performance. Thank you for your help.

  6. #6
    Registered User
    Join Date
    03-21-2015
    Location
    GrassyKnoll
    MS-Off Ver
    2010
    Posts
    82

    Re: Silly Question about variables

    Hi Marvin,

    Are you using VBA and declaring variables
    yes I am.

    I dont know why but I dont like name ranges and dont use them.

  7. #7
    Registered User
    Join Date
    03-21-2015
    Location
    GrassyKnoll
    MS-Off Ver
    2010
    Posts
    82

    Re: Silly Question about variables

    Because I don’t like something doesn’t mean I shouldn’t have it in my tool box and use it when necessary. It could be that I had a hard time learning it or struggled with the concepts as a new learner & just kind of stayed away because I guess we tend to work with what we know and feel comfortable with.

    I know more now than I did then and maybe it is time to revisit some of these concepts. I understand a bit better how functions work now that I learned to pass variables from on Sub to the next, and well that is what a function does too, right well sometimes at least if that’s what it is supposed to do.

  8. #8
    Registered User
    Join Date
    03-21-2015
    Location
    GrassyKnoll
    MS-Off Ver
    2010
    Posts
    82

    Re: Silly Question about variables

    Should I be using named ranges in my VBA @Marvin? Am I missing out on a huge edge that I could have if I used them? I will start doing it if its something I should be doing. I will just have to get over whatever hang-ups I have. I just want to write clean efficient code

    Thank you both for your guidance Im going to start writing some of these procedures as functions.

  9. #9
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Silly Question about variables

    Named ranges are a great way to communicate worksheet structure to sub procedures; not so much for user-defined functions, since UDFs should be passed everything they need to calculate a result without sniffing around the worksheet.

    I was willing to brush it off if there was no cost to performance.
    You could declare scalar (ordinary, non-array, non-object) variables until your typing fingers got tired without affecting storage significantly. Unused variables don't affect run-time performance at all.
    Last edited by shg; 03-29-2015 at 02:16 PM.

  10. #10
    Registered User
    Join Date
    03-21-2015
    Location
    GrassyKnoll
    MS-Off Ver
    2010
    Posts
    82

    Re: Silly Question about variables

    Ok

    I’m going to make named ranges my friend and get to know them better.

    What is the deciding factor as to whether something should be a function or a Sub? How do you determine which way to go on that one?

  11. #11
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Silly Question about variables

    In my mind, a UDF computes a small number of results (frequently just one) based on a small number of arguments. A macro typically needs to calculate a bunch of results from a bunch of data that all needs to be looked at in aggregate.

    SIN(x) is a perfect example of an appropriate UDF: it receives a value of x and returns its sine. It doesn't need to know where the variable came from. A sub that puts the sines of a list of numbers one cell to the right seems silly.

    A UDF that computes inventory cost in FIFO fashion by looking at all previous sales and purchases could be made to work, but every line would need to duplicate all of the work of the FIFO formulas above, and then do one more calculation; a sub could generate results row by row without redundancy, but needs to know how data is arranged on the sheet (that's where named ranges come in).

    IMO, here and elsewhere, Subs are overutilized when UDFs would be simpler and more reusable.

    If you write a few hundred of each, the picture will emerge in your mind.

  12. #12
    Registered User
    Join Date
    03-21-2015
    Location
    GrassyKnoll
    MS-Off Ver
    2010
    Posts
    82

    Re: Silly Question about variables

    Thanks a lot I think I got it now. All of my one trick ponies should be functions and if necessary I could call on them or assimilate them in a Sub to do something. I think I got it.
    That makes a lot of senses when I think about when I do see people using functions.
    Please Login or Register  to view this content.
    Thank you so much. Now I know why Im doing it I can feel more confident about make decisions your the Best!

  13. #13
    Registered User
    Join Date
    03-21-2015
    Location
    GrassyKnoll
    MS-Off Ver
    2010
    Posts
    82

    Re: Silly Question about variables

    The Fog is lifting.

  14. #14
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Silly Question about variables

    You're welcome.

  15. #15
    Registered User
    Join Date
    03-21-2015
    Location
    GrassyKnoll
    MS-Off Ver
    2010
    Posts
    82

    Re: Silly Question about variables

    Should I mark this thread as Solved? I have more questions but I don’t want to take advantage.
    Maybe I should because I think my question is a bit more evolved now and more specific about functions.

  16. #16
    Registered User
    Join Date
    03-21-2015
    Location
    GrassyKnoll
    MS-Off Ver
    2010
    Posts
    82

    Re: Silly Question about variables

    Well I guess it does have to do with variables. I just made a little UDF: DateFormated
    Please Login or Register  to view this content.
    It works I would have thought I was missing the declaration if that’s the right word. Where we say
    X As String
    Any how my function works but why isnt VBA telling me Im missing the declaration.

  17. #17
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Silly Question about variables

    Hi Bunny,

    I've been thinking about this topic for a few weeks as I work with a Golf Scores workbook. If I used ranges in formulas and then move stuff around, like inserting or deleting rows or columns, Excel readjusts my formulas so they still work. If I write VBA code and look at row 7 for something that has moved, Excel doesn't adjust to fit my code. If I name a range and use in my VBA code Range("MyData") the code will work because it can find the range after inserting or deleting rows/columns.

    I also use a lot of Dynamic Named Ranges so more rows can automatically increase the range of values.

    At first I thought you wanted to use Global Variables in your VBA code that might be another option for you, that I also use.

    After all these different ways of making my work able to deal with many different sets of data, I sometimes forget to change all the "hiding places" and my code fails, especially after not seeing it for a year or two as shg suggests.

  18. #18
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Silly Question about variables

    Quote Originally Posted by xlBunny View Post
    Any how my function works but why isnt VBA telling me Im missing the declaration.
    Declaration of what?

  19. #19
    Registered User
    Join Date
    03-21-2015
    Location
    GrassyKnoll
    MS-Off Ver
    2010
    Posts
    82

    Re: Silly Question about variables

    Well how come VBA isnt saying "Variable not defined" for DateFormated

    Please Login or Register  to view this content.
    Maybe I should go brush up on functions so I know the dos & don’ts save you guys sometime in trying to explain everything to me. When I was starting out I didn’t like functions because stepping through the code they would take me outside the main sub routine & it was hard to keep my baring. So I avoided them. It’s like when you first learn to drive there’s so much going on. In time you get enough practice you can drive home and not even remember the journey.

    Thank you @Marvin Global Variables is something I have been thinking about too.
    Last edited by xlBunny; 03-29-2015 at 04:31 PM.

  20. #20
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,167

    Re: Silly Question about variables

    VBA will tell you a variable isn't defined ONLY if you use Option Explicit at the top of a module.
    http://www.cpearson.com/excel/declaringvariables.aspx

  21. #21
    Registered User
    Join Date
    03-21-2015
    Location
    GrassyKnoll
    MS-Off Ver
    2010
    Posts
    82

    Re: Silly Question about variables

    Ok I see when I step its a Variant/String

  22. #22
    Registered User
    Join Date
    03-21-2015
    Location
    GrassyKnoll
    MS-Off Ver
    2010
    Posts
    82

    Re: Silly Question about variables

    So this is how I should have done it?

    Please Login or Register  to view this content.
    The yellow font will burn your eyes caution it like sun glare. Wholly cow that bright.

    I guess if you don’t declare the type for the Function its default is Variant. Glad I didn’t open a new thread for that, and this whole conversation is under one thread for somebody to maybe learn from.
    Last edited by xlBunny; 03-29-2015 at 04:51 PM.

  23. #23
    Registered User
    Join Date
    03-21-2015
    Location
    GrassyKnoll
    MS-Off Ver
    2010
    Posts
    82

    Re: Silly Question about variables

    Thanks so much guys your great Im off to go have more fun xling.

    SHAZAM!!! Functions Calling Functions my code just got a lot cleanner.
    Last edited by xlBunny; 03-29-2015 at 05:14 PM.

  24. #24
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,355

    Re: Silly Question about variables

    In this instance, there is no variable to be declared. You are using the Date variable, in your UDF. Date is a built in function which returns the System Date. And your function is formatting it as "mm-dd-yyyy"

    I think your original question about variables has probably been covered and you are now moving on to questions about User Defined Functions. Probably warrants a new thread, or two.

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  25. #25
    Registered User
    Join Date
    03-21-2015
    Location
    GrassyKnoll
    MS-Off Ver
    2010
    Posts
    82

    Re: Silly Question about variables

    Hi just wanted to update this thread with a link to a follow-up question that I just asked. It seems a lot of people have questions on this because the views are pretty high. At least I think so because this thread isnt even a moth old and its has just over 400 views.

    Thanks again to shg, Marvin, and TMS
    I am xling thanks to you.
    Last edited by xlBunny; 04-23-2015 at 11:27 AM.

+ 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. [SOLVED] Silly little question regarding cel formatting
    By Kym-B in forum Excel General
    Replies: 1
    Last Post: 12-05-2013, 08:46 AM
  2. [SOLVED] Silly Question !!
    By DanielRay in forum The Water Cooler
    Replies: 4
    Last Post: 12-08-2012, 04:49 PM
  3. Silly Question !!
    By DanielRay in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-07-2012, 11:41 AM
  4. Hello! Liz from Detroit, MI - SILLY question!
    By libby1972 in forum Hello..Introduce yourself
    Replies: 1
    Last Post: 11-27-2012, 07:41 PM
  5. silly look up question
    By floricita in forum Excel General
    Replies: 1
    Last Post: 10-11-2010, 07:03 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