+ Reply to Thread
Results 1 to 18 of 18

storing a variable as a range to be used later

  1. #1
    Forum Contributor
    Join Date
    05-09-2009
    Location
    Chicago, US
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    188

    storing a variable as a range to be used later

    Hi. I am trying to store a variable (I will highlight it in red below) to be used later in the same formula. The formula I have now works, but it puts the variable in cell G20, and I do not need this variable anywhere on the sheet. However, I do need it to perform some calculations later in the code. Here is the code:

    Please Login or Register  to view this content.

    As you can see, in order to get G21, I am using the value from G20. Ideally, I want to be able to calculate G21 without G20 being visible. So instead of my code saving this number in G20, is there a way for the code to remember or store the value (of G20) to be used in the following formula (bottom of top code):

    Please Login or Register  to view this content.

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: storing a variable as a range to be used later

    Hi,

    Yes, just declare another variable. e.g.

    Please Login or Register  to view this content.
    HTH
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Forum Contributor
    Join Date
    05-09-2009
    Location
    Chicago, US
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    188

    Re: storing a variable as a range to be used later

    That worked like a charm! Thank you Sir!!

    Does it matter if I declare it as a Double, Range or Integer?

    I went with Double like you said, but I'm just wondering why Double vs. Range or vs. Integer

  4. #4
    Forum Contributor
    Join Date
    05-09-2009
    Location
    Chicago, US
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    188

    Re: storing a variable as a range to be used later

    Sub y()

    Dim c As Range, lrow As Long, whatever As Double

    lrow = Sheet1.Cells(Rows.Count, 1).End(xlUp).Row

    Application.ScreenUpdating = False

    For Each c In Sheet1.Range("A2:A29" & lrow)
    If c.Value = "Mike" And c.Offset(0, 1).Value = "Fax" Then
    whatever = whatever + c.Offset(0, 3).Value
    End If
    Next c

    Range("G21").Formula = "=" & whatever & " / SUMPRODUCT(--(A2:A29=""Mike""),--(B2:B29=""Fax""))"
    Range("G22").Formula = "=" & whatever & " - Range("M19") * Range("M25")

    Application.ScreenUpdating = True

    End Sub
    I have a problem with the red highlighted area above. I am trying to Subtract a value from cell M19 from "whatever" double THEN multiply the result by another value stored in cell M25
    Last edited by losmi8; 11-23-2009 at 09:48 PM.

  5. #5
    Forum Contributor
    Join Date
    05-09-2009
    Location
    Chicago, US
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    188

    Re: storing a variable as a range to be used later

    Both ranges "M19" and "M25" are already stored in the sheet. They aren't calculated from anything. I manually enter them if that is of any help.

  6. #6
    Forum Contributor
    Join Date
    05-09-2009
    Location
    Chicago, US
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    188

    Re: storing a variable as a range to be used later

    Anyone? I can't proceed without getting this part...Because I'll need that value to calculate others. Ah, programing can get frustrating sometimes

  7. #7
    Forum Contributor
    Join Date
    05-09-2009
    Location
    Chicago, US
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    188

    Re: storing a variable as a range to be used later

    If the above explanation of what I need is not good enough...I need to perform some basic math functions including already stored cell values and a variable that is a Double.

    double + value in cell + (value in cell - value in a different cell)


    in the above example, "whatever" is my Double.

  8. #8
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: storing a variable as a range to be used later

    Hi

    Please Login or Register  to view this content.
    Is this what you are trying to do?


    rylo

  9. #9
    Forum Contributor
    Join Date
    05-09-2009
    Location
    Chicago, US
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    188

    Re: storing a variable as a range to be used later

    Yep, perfect! I was using "Range("M19")" instead of just M19


    Another question for you if you don't mind (or anyone else if you get to it first)

    I want to do:
    Please Login or Register  to view this content.
    IF...the value of G25 is a positive number. However, if G25 is a negative number, I'd want to do the following:
    Please Login or Register  to view this content.

    Hope that makes sense?

  10. #10
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: storing a variable as a range to be used later

    Hi

    Normal IF statement approach

    Please Login or Register  to view this content.
    rylo

  11. #11
    Forum Contributor
    Join Date
    05-09-2009
    Location
    Chicago, US
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    188

    Re: storing a variable as a range to be used later

    Oh cool. That is simple enough! So I should have two lines of code, one for IF it is greater than 0 and one if it is less than 0?
    Please Login or Register  to view this content.

  12. #12
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: storing a variable as a range to be used later

    Hi

    No, only the one line of code as the if statment is in the formula.

    rylo

  13. #13
    Forum Contributor
    Join Date
    05-09-2009
    Location
    Chicago, US
    MS-Off Ver
    Excel 2003, Excel 2007
    Posts
    188

    Re: storing a variable as a range to be used later

    Okay, thank you. I figured it out!

    Thank you for being so helpful rylo! Appreciate it!
    Last edited by losmi8; 11-24-2009 at 01:58 AM.

  14. #14
    Forum Expert
    Join Date
    01-15-2007
    Location
    Brisbane, Australia
    MS-Off Ver
    2007
    Posts
    6,591

    Re: storing a variable as a range to be used later

    Hi

    sure

    Please Login or Register  to view this content.
    rylo

  15. #15
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: storing a variable as a range to be used later

    Quote Originally Posted by losmi8 View Post
    That worked like a charm! Thank you Sir!!

    Does it matter if I declare it as a Double, Range or Integer?

    I went with Double like you said, but I'm just wondering why Double vs. Range or vs. Integer
    Hi,

    I can see from the further correspondence overnight that you're well on the way, but to answer your question, it all depends on what's in the range you're recording as a variable. If it's only ever an integer value then an integer variable is all you need. If however it's number involving decimals then you'll need the double variable.

    Always try to use the variable type which takes the least memory. Double takes up more space than Integer (or Single, Long etc.).

    HTH

  16. #16
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: storing a variable as a range to be used later

    In 32 bit VBA Integers are converted "under the hood" to Long, as such some have argued that use of Integers is in fact slower than use of Longs given the additional operations that take place behind the scene, ie: Integer -> Long -> Integer

    On that basis I never use Integer type, only Long or Byte if it will suffice... there is no value to using Integer in VBA other than as a sanity check - ie to deliberately generate an Overflow error.


    losmi8, given this is an extension of your other thread I hope you will be denoting the other thread as Solved in the immediate future and adding a link from that to this for sake of continuity.

  17. #17
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: storing a variable as a range to be used later

    Thanks Donkey.

    I didn't know that. Interesting.

    Rgds

  18. #18
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: storing a variable as a range to be used later

    I only know that little nugget c/o romperstomper... but it's a good one I agree.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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