+ Reply to Thread
Results 1 to 36 of 36

Basic Variable Question

  1. #1
    Registered User
    Join Date
    06-27-2013
    Location
    Arizona, US
    MS-Off Ver
    Excel 2010
    Posts
    24

    Basic Variable Question

    Can someone give me a basic example of using variables that can be used on a different sheet but still reference their values on the sheet where all my data is.

    Please Login or Register  to view this content.
    The above code posts the data on sheet1 in the cell I want however it is only posting the contense of column C which I can see why. I want to have it call it as a variable not the actual spot c. I dont need my whole code written by any means, I know you all have lives to live and wouldnt expect that from someone. Just looking for a point in the right direction. Any tips or answers would be greatly appreciated. Thanks

    Side note: I did post a similar thread however I felt it was a bit vague and didnt really get to the heart of what I was stuck on.
    Last edited by cosmicwinds; 06-28-2013 at 02:25 PM.

  2. #2
    Valued Forum Contributor
    Join Date
    03-29-2013
    Location
    United Kingdom
    MS-Off Ver
    Office/Excel 2013
    Posts
    1,749

    Re: Basic Variable Question

    I'm assuming that the C in Cells(1,C) is your variable ...
    If you want it to work across different sheets you need to declare it in a code module.
    Elegant Simplicity............. Not Always

  3. #3
    Registered User
    Join Date
    06-27-2013
    Location
    Arizona, US
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Basic Variable Question

    You are correct, C is my variable. Sorry I didn't post more of the code I had written. I have declared it in the code.
    Sub try()
    Dim c
    'Dim avgCallFails
    'Finding Best MobileReceivePower
    '*********************************************************************************************
    Please Login or Register  to view this content.
    Last edited by cosmicwinds; 06-28-2013 at 02:14 PM.

  4. #4
    Registered User
    Join Date
    06-27-2013
    Location
    Arizona, US
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Basic Variable Question

    I also tried to work back a little since this wasnt working and tried a simpiler but still in essence the same thing and couldnt get it to work.

    Please Login or Register  to view this content.

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Basic Variable Question

    I don't think that's all the code but perhaps something like this, assuming Cells(1, c) is on 'Series Formatted Data'.
    Please Login or Register  to view this content.
    PS When you post code can you enclose it in code tags.
    If posting code please use code tags, see here.

  6. #6
    Valued Forum Contributor
    Join Date
    03-29-2013
    Location
    United Kingdom
    MS-Off Ver
    Office/Excel 2013
    Posts
    1,749

    Re: Basic Variable Question

    Looks to me that the problem is with ""= Average(‘Series Formatted Data’!c:c)""... .. You are testing for varying conditions on the source sheet but then applying the same formula to the destination --- Result being that they will all calculate the same answer.

    Try Sheets("Sheet1").Cells(2, 11).Formula = "= average(Range(Cells(2, C), Cells(10000, C)))"

  7. #7
    Registered User
    Join Date
    06-27-2013
    Location
    Arizona, US
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Basic Variable Question

    Sorry I made the corrections to code tags.

  8. #8
    Registered User
    Join Date
    06-27-2013
    Location
    Arizona, US
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Basic Variable Question

    You are right the code will go on further looking for various other strings and output the averages and other formulas based on what it finds (at least thats the goal ). I test parts here and there to make sure they are working but this I cant really get moving since I was stuck in a rut with variable related problems.

    Ok If I am understanding this correct what you said was rng was equal to cell (1,c) on X sheet.
    we then loop until an empty slide occurs and within this loop we utilize a case statement to find the necessary strings. Damn thats smart >.< Case statements didnt even cross my mind

    Awesome I think this should be enough to get me moving in the correct direction. I am very greatful for your insight and help as well as everyone elses

  9. #9
    Registered User
    Join Date
    06-27-2013
    Location
    Arizona, US
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Basic Variable Question

    Alright Ill try that out as well in addition to the code supplied by Norie. Thanks for your time I appreciate it more than you believe

  10. #10
    Registered User
    Join Date
    06-27-2013
    Location
    Arizona, US
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Basic Variable Question

    Sorry to bother you but Im trying to get that code working and it keeps throwing an error and for the life of me I cant see where it is.

    It is telling me there is a syntax error but its identical to the first case statement pretty much and that one has no issues.

    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    06-27-2013
    Location
    Arizona, US
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Basic Variable Question

    Andy, so I dont actually have to specify what page it is to be implemented on, only the source location had to be specified?

  12. #12
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Basic Variable Question

    The quotes are wrong, they should be " not ”.

    I corrected some of them but missed some too.

  13. #13
    Registered User
    Join Date
    06-27-2013
    Location
    Arizona, US
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Basic Variable Question

    Oh! I had no idea that they were interpretted differently Learning so much since I came to this forum! Thanks again!

  14. #14
    Registered User
    Join Date
    06-27-2013
    Location
    Arizona, US
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Basic Variable Question

    Still having the same problem it turns out. While I do agree your method looks far cleaner than mine did. From that page it is still only pulling data from the column C, not c as a variable. Sorry to keep pestering you Norie

    Please Login or Register  to view this content.

  15. #15
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: Basic Variable Question

    Is c supposed to be shown as an absolute range? Why, if it is declared as a variable can it not be coded as just c?

    Sorry. I'm new.

  16. #16
    Registered User
    Join Date
    06-27-2013
    Location
    Arizona, US
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Basic Variable Question

    I am also fairly new can you explain what you mean? I am also new

  17. #17
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655
    I'm trying to view this on my phone with the excel forums app. Perhaps I misread. I thought you were trying to use the variable in your average formula because the c was lower case.
    Thanks,
    Solus


    Please remember the following:

    1. Use [code] code tags [/code]. It keeps posts clean, easy-to-read, and maintains VBA formatting.
    Highlight the code in your post and press the # button in the toolbar.
    2. Show appreciation to those who have helped you by clicking below their posts.
    3. If you are happy with a solution to your problem, mark the thread as [SOLVED] using the tools at the top.

    "Slow is smooth, smooth is fast."

  18. #18
    Registered User
    Join Date
    06-27-2013
    Location
    Arizona, US
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Basic Variable Question

    Yes I am trying to use c as my variable which will represent my column in a different sheet.

  19. #19
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Basic Variable Question

    Are you saying that in this piece of code c is a variable rather than column C?
    Please Login or Register  to view this content.
    If that's the case I think we need more information, for example what should c change to?

    Perhaps an example workbook might help?

    Click on GO ADVANCED and use the paperclip icon to open the upload window.

    View Pic

  20. #20
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655
    That's what I'm wondering. I don't see where c ever changes so it would just be 1:1 or all of column a (or c if its read as a range and not a variable).

  21. #21
    Registered User
    Join Date
    06-27-2013
    Location
    Arizona, US
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Basic Variable Question

    oops I had forgotten to include c=c+1
    However even with that added to the code it still has the same result.

  22. #22
    Registered User
    Join Date
    06-27-2013
    Location
    Arizona, US
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Basic Variable Question

    Does this work?
    Img1.jpgimg2.JPG

  23. #23
    Registered User
    Join Date
    06-27-2013
    Location
    Arizona, US
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Basic Variable Question

    c is supposed to represent my column number and if the code didnt see the string it would continue on to the next column until it found matches in various strings or it hit an empty column.

  24. #24
    Registered User
    Join Date
    06-27-2013
    Location
    Arizona, US
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Basic Variable Question

    Well after spending all day I believe I finally figured it out. Ill update later tonight if I didnt fall asleep from being so tired.... 4 hrs of sleep a day for a couple of weeks really starts to ware on a guy. To be honest I think it was far simpler than I was making it out to be. But ya... if I fall asleep Ill update this thread first thing in the morning. Thanks again guys

  25. #25
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Basic Variable Question

    Still not clear what you want but if c is meant to represent the column that is to be averaged I would write it something like this.
    Please Login or Register  to view this content.
    PS Images don't really help.

  26. #26
    Forum Contributor
    Join Date
    08-22-2012
    Location
    nj, us
    MS-Off Ver
    Excel 2010
    Posts
    163

    Re: Basic Variable Question

    c has to be a static, not dim

    so:
    Please Login or Register  to view this content.

  27. #27
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Basic Variable Question

    John456852

    I'm not even sure c is needed.

  28. #28
    Forum Contributor
    Join Date
    08-22-2012
    Location
    nj, us
    MS-Off Ver
    Excel 2010
    Posts
    163

    Re: Basic Variable Question

    ok norie. if needed, since c is used more than once, use static to keep value

  29. #29
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Basic Variable Question

    Why would you want to keep the value?

  30. #30
    Forum Contributor
    Join Date
    08-22-2012
    Location
    nj, us
    MS-Off Ver
    Excel 2010
    Posts
    163

    Re: Basic Variable Question

    because c=c+1

    and after every time the sub ends, c will be reset to 0

  31. #31
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Basic Variable Question

    That's because you want to move across the columns starting at column 1 (A).

    If you declare as Static then the next time you run the code you'll start at the last column that was checked, not column 1.

    So you would be skipping all the columns to the left.

  32. #32
    Forum Contributor
    Join Date
    08-22-2012
    Location
    nj, us
    MS-Off Ver
    Excel 2010
    Posts
    163

    Re: Basic Variable Question

    OH sorry. i am not very good and i am not sure i understood what the original code was for.

  33. #33
    Registered User
    Join Date
    06-27-2013
    Location
    Arizona, US
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Basic Variable Question

    Norie I needed C and I got it working. Think of it as a counter that will continue moving along checking for strings each time the counter incremented.

  34. #34
    Forum Contributor
    Join Date
    08-22-2012
    Location
    nj, us
    MS-Off Ver
    Excel 2010
    Posts
    163

    Re: Basic Variable Question

    so for the counter you could use static

  35. #35
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Basic Variable Question

    I don't see why you would want/need to use Static unless you wanted to start from the column you finished in on the previous run.

  36. #36
    Forum Contributor
    Join Date
    08-22-2012
    Location
    nj, us
    MS-Off Ver
    Excel 2010
    Posts
    163

    Re: Basic Variable Question

    I guess ur righty

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Tags for this Thread

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