+ Reply to Thread
Results 1 to 31 of 31

setting variable equal to a value in a cell

  1. #1
    Registered User
    Join Date
    02-23-2015
    Location
    US
    MS-Off Ver
    2010
    Posts
    20

    setting variable equal to a value in a cell

    Hello-

    New to VBA after having just finished an introductory text. Thanks for your help in advance!

    I am trying to do something that I thought would be pretty simple- set a variable equal to a cell in a spreadsheet.

    the line of code that is giving me trouble is:

    payrate = Worksheets("sheet1").Cells(payrow, paycolumn)

    The values for payrow and paycolumn are determined elsewhere in the program. For example, if payrow is determined to be 1 and paycolumn is determined to be 10, I want payrate to be equal to the value in the cell at 1,10 (row 1 column 10) in sheet 1.

    When the debug function is running, I hover my mouse cursor over payrow and paycolumn and the values in those variables are correct, but hovering over payrate shows a value of 0. The error I am getting is "run time error 1004, application defined or object defined error"

    What am I doing wrong? Thanks for your time.

  2. #2
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: setting variable equal to a value in a cell

    Can you post all your code?

  3. #3
    Registered User
    Join Date
    02-23-2015
    Location
    US
    MS-Off Ver
    2010
    Posts
    20

    Re: setting variable equal to a value in a cell

    Quote Originally Posted by StephenR View Post
    Can you post all your code?
    Here's the pertinent code:


    yearsofserviceadjustment = SpinButton3.Value
    If yearsofserviceadjustment > 12 Then yearsofserviceadjustment = 12
    paycolumn = yearsofserviceadjustment + 27


    If OptionCaptain And Option747 Then payrow = 1
    If OptionCaptain And Option767 Then payrow = 2
    If OptionCaptain And Option757 Then payrow = 3
    If OptionCaptain And Option319 Then payrow = 4

    If OptionFirstOfficer And Option747 Then payrow = 7
    If OptionFirstOfficer And Option767 Then payrow = 8
    If OptionFirstOfficer And Option757 Then payrow = 9
    If OptionFirstOfficer And Option319 Then payrow = 10


    payrate = Worksheets("sheet1").Cells(payrow, paycolumn) 'page 118

    payrate is DIM'ed as a double
    payrow is DIM'ed as a integer
    paycolumn is DIM'ed as a string
    Last edited by ualdriver; 02-23-2015 at 06:38 AM. Reason: get rid of extra stuff not needed

  4. #4
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: setting variable equal to a value in a cell

    paycolumn should be declared as Long, not String (not sure if that is causing your error though).

  5. #5
    Registered User
    Join Date
    02-23-2015
    Location
    US
    MS-Off Ver
    2010
    Posts
    20

    Re: setting variable equal to a value in a cell

    Quote Originally Posted by StephenR View Post
    paycolumn should be declared as Long, not String (not sure if that is causing your error though).
    Unfortunately still getting error no matter how I DIM the variables.

    payrate = Worksheets("sheet1").Cells(payrow, paycolumn)

    Is that line above a "legitimate" line of VBA code? For the life of me, I couldn't find an example on-line or in my reference book where the desired row and column of a cell were determined by variables. All the examples I found set a variable like "payrate" above to a specific cell.
    Last edited by ualdriver; 02-23-2015 at 06:57 AM.

  6. #6
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: setting variable equal to a value in a cell

    Nonetheless, you should declare variables correctly as it can cause other problems.

    What is in the relevant cell?

  7. #7
    Registered User
    Join Date
    02-23-2015
    Location
    US
    MS-Off Ver
    2010
    Posts
    20

    Re: setting variable equal to a value in a cell

    Quote Originally Posted by StephenR View Post
    Nonetheless, you should declare variables correctly as it can cause other problems.

    What is in the relevant cell?
    Will do.

    The relevant cell could be any one in a range from AB1 to AM4 or AB7 to AM10 depending on the user's input. In the example I was playing with in debug, payrow is showing equal to 1 (correct), paycolumn is showing 39 (correct), so I want to say payrate to the value in row 1, column 39, which should be cell AM1. The value in cell AM1 is 254.74. payrate shows a value of 0 when I hover over it in debug, and I get the error message as in post 1.

  8. #8
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: setting variable equal to a value in a cell

    There's nothing wrong with that line as long as the variables are correctly declared, and the value in the cell is a number.

    I'm stumped. Can you post a workbook?

  9. #9
    Registered User
    Join Date
    02-23-2015
    Location
    US
    MS-Off Ver
    2010
    Posts
    20

    Re: setting variable equal to a value in a cell

    Sure.....don't laugh as I am a hack!

    to get the same numbers as me select:
    Captain
    747/777/787/767-400 option
    30 years of service
    200000 gross pay
    900 hours work
    10 days vacation
    17000 401K
    Yes 50 or older
    Attached Files Attached Files

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

    Re: setting variable equal to a value in a cell

    The value of payrow is 0 because none of the expressions in these If statements evaluate to True.
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

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

    Re: setting variable equal to a value in a cell

    Actually, I must have made a mistake or the 747/777/787/767-400 option became unchecked somehow.

    Anyway, you do need to declare paycolumn as Long, if you declare it as string you'll get an error.

    The reason for the error is that within Cells if the column argument is a string VBA assumes it's a column letter.

  12. #12
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: setting variable equal to a value in a cell

    If I declare paycolumn correctly, I don't get an error, although I can't tell if the code does anything.

  13. #13
    Registered User
    Join Date
    02-23-2015
    Location
    US
    MS-Off Ver
    2010
    Posts
    20

    Re: setting variable equal to a value in a cell

    Quote Originally Posted by Norie View Post
    The value of payrow is 0 because none of the expressions in these If statements evaluate to True.
    Please Login or Register  to view this content.


    1) In debug mode, when I hover over OptionCaptain and Option747, they both show TRUE so therefore payrow should be set to 1
    2) When I hover over the payrow variable, it shows 1, which it is supposed to be.

    I guess I don't understand why payrow wouldn't = 1 if both OptionCaptain and Option747 are both TRUE?

    EDIT: Just saw your response disregard.

  14. #14
    Registered User
    Join Date
    02-23-2015
    Location
    US
    MS-Off Ver
    2010
    Posts
    20

    Re: setting variable equal to a value in a cell

    Quote Originally Posted by StephenR View Post
    If I declare paycolumn correctly, I don't get an error, although I can't tell if the code does anything.
    Yeah, when I DIM paycolumn as long, the button on my User Form doesn't do anything and I don't know why. When I DIM paycolumn as string, the button works but I get the error as described in post 1.

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

    Re: setting variable equal to a value in a cell

    When you declare paycolumn as Long does the value of payrate get set correctly?

  16. #16
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: setting variable equal to a value in a cell

    Norie - does for me.

    The problem must lie elsewhere.

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

    Re: setting variable equal to a value in a cell

    I think the problem might be with one/some of the other variables, for example daysofvacationforfeit is never set, so the vacationvalue calculation might be off.

  18. #18
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: setting variable equal to a value in a cell

    It's working through a whole load of calculations but without knowing what should happen, it's hard to know where the problem lies.

  19. #19
    Registered User
    Join Date
    02-23-2015
    Location
    US
    MS-Off Ver
    2010
    Posts
    20

    Re: setting variable equal to a value in a cell

    Quote Originally Posted by Norie View Post
    I think the problem might be with one/some of the other variables, for example daysofvacationforfeit is never set, so the vacationvalue calculation might be off.
    Quote Originally Posted by StephenR View Post
    It's working through a whole load of calculations but without knowing what should happen, it's hard to know where the problem lies.
    Yeah, sorry guys. It's a work in progress and probably poorly written at that as it is my first attempt at writing something using VBA. I don't want to waste your time. What's supposed to happen is that a person enters the data on the Excel spreadsheet above the click here button. Then the userform should pop up when the click here button is pressed. Then the data is entered/selected into the userform, then after hitting the button on the userform the program is supposed to crunch numbers based on the entered data on the userform and the data on the spreadsheet then go back to the worksheet 1 and enter data into F14-f16. After I got it working, I was going to make it more "pretty" with graphs and colors and stuff.

    But the program hasn't gotten that far yet because I can't get that payrate variable to set properly, hence the initial post.

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

    Re: setting variable equal to a value in a cell

    Are you sure payrate isn't being set properly?

    When I step through the code it's definitely getting set to something.

  21. #21
    Registered User
    Join Date
    02-23-2015
    Location
    US
    MS-Off Ver
    2010
    Posts
    20

    Re: setting variable equal to a value in a cell

    Quote Originally Posted by Norie View Post
    Are you sure payrate isn't being set properly?

    When I step through the code it's definitely getting set to something.
    I am sure of nothing! payrate should be set to a number like 254.77 not 0, like I was getting.

    How does one "step through the code" so that one can see what the values of the variables are?

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

    Re: setting variable equal to a value in a cell

    First put a breakpoint in the Click_Here_Click sub by selecting line of code and hitting F9, then run the userform, enter the required information and click the Click Here button.

    You should now go into break mode with the code paused on the line of code you put the breakpoint on and you can then step through the code using F8.
    Last edited by Norie; 02-23-2015 at 05:13 PM.

  23. #23
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: setting variable equal to a value in a cell

    When I run it, this line is false
    Please Login or Register  to view this content.
    so much of the code is missed.

  24. #24
    Registered User
    Join Date
    02-23-2015
    Location
    US
    MS-Off Ver
    2010
    Posts
    20

    Re: setting variable equal to a value in a cell

    Quote Originally Posted by Norie View Post
    First put a breakpoint in the Click_Here_Click sub by selecting line of code and hitting F9, then run the userform, enter the required informationan an click th Click Here button.

    You should now go into break mode with the code paused on the line of code you put the breakpoint on and you can then step through the code using F8.
    Quote Originally Posted by StephenR View Post
    When I run it, this line is false
    Please Login or Register  to view this content.
    so much of the code is missed.
    Thanks guys. Had to go to sleep and now working all night, so have to put this down for a day or so. I will look at this information and post back if you guys haven't gotten bored yet!

  25. #25
    Forum Contributor
    Join Date
    03-11-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2016 (among others!)
    Posts
    331

    Re: setting variable equal to a value in a cell

    It's late, I'm tired and not 100% sure what has happened since the opening post here but.....shouldn't:

    payrate = Worksheets("sheet1").Cells(payrow, paycolumn) 'page 118


    be:

    payrate = Worksheets("sheet1").Cells(payrow, paycolumn).value
    if you want the value of the cell being returned as payrate? If I've misunderstood or am talking tosh feel free to mock me once I'm in bed!

  26. #26
    Registered User
    Join Date
    02-23-2015
    Location
    US
    MS-Off Ver
    2010
    Posts
    20

    Re: setting variable equal to a value in a cell

    Quote Originally Posted by malcmail View Post
    It's late, I'm tired and not 100% sure what has happened since the opening post here but.....shouldn't:

    payrate = Worksheets("sheet1").Cells(payrow, paycolumn) 'page 118


    be:

    payrate = Worksheets("sheet1").Cells(payrow, paycolumn).value
    if you want the value of the cell being returned as payrate? If I've misunderstood or am talking tosh feel free to mock me once I'm in bed!
    Thanks malcmail. Unfortunately, adding the ".value" to the end didn't work

  27. #27
    Registered User
    Join Date
    02-23-2015
    Location
    US
    MS-Off Ver
    2010
    Posts
    20

    Re: setting variable equal to a value in a cell

    Quote Originally Posted by Norie View Post
    First put a breakpoint in the Click_Here_Click sub by selecting line of code and hitting F9, then run the userform, enter the required information and click the Click Here button.

    You should now go into break mode with the code paused on the line of code you put the breakpoint on and you can then step through the code using F8.
    Quote Originally Posted by StephenR View Post
    When I run it, this line is false
    Please Login or Register  to view this content.
    so much of the code is missed.
    Quote Originally Posted by malcmail View Post
    It's late, I'm tired and not 100% sure what has happened since the opening post here but.....shouldn't:

    payrate = Worksheets("sheet1").Cells(payrow, paycolumn) 'page 118


    be:

    payrate = Worksheets("sheet1").Cells(payrow, paycolumn).value
    if you want the value of the cell being returned as payrate? If I've misunderstood or am talking tosh feel free to mock me once I'm in bed!
    Stephen and malcmail and Norie-

    After stepping away from my little program for a while and coming back to it, I figured out some errors and what you were all trying to tell me. When I properly DIM'ed the variables as you described, the code was running and fixed, but I just didn't know it because a lot of it was being bypassed in the error described above. Now that I know how to step through the code F8/F9, I can see what was causing my confusion. Now I'm working on the next set of errors Thanks very much for your time.

  28. #28
    Forum Contributor
    Join Date
    03-11-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2016 (among others!)
    Posts
    331

    Re: setting variable equal to a value in a cell

    Just tried it myself without value and it worked. Hoping you've found your answer now.

  29. #29
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: setting variable equal to a value in a cell

    Glad we got there.

    maclmail - arguably it's better practice, but because Value is the default property of the Range object in most cases we don't strictly need to specify it.

  30. #30
    Forum Contributor
    Join Date
    03-11-2013
    Location
    United Kingdom
    MS-Off Ver
    Excel 2016 (among others!)
    Posts
    331

    Re: setting variable equal to a value in a cell

    StephenR

    THanks for the info there. I've always done it that way simply as I remember the first time I ever tried anything it wouldn't work without it. Good to know for future.

  31. #31
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606

    Re: setting variable equal to a value in a cell

    Yes there are some cases when you explicitly have to include it, and I've never understood why.

+ 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] How do you set a variable to equal the value of a cell?
    By ahilty in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 07-09-2013, 02:33 PM
  2. Setting One Cell Equal to Another
    By ashleys.nl in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 09-07-2011, 12:43 PM
  3. vba code for setting the cell value to be equal to cell value in another workbook
    By yellowpower in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-15-2010, 04:10 AM
  4. set a variable equal to a value in a named cell
    By barrfly in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 11-10-2009, 04:50 AM
  5. [SOLVED] Setting a cell equal to another worksheet cell fails (sometimes)
    By Richard in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-10-2006, 12:15 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