+ Reply to Thread
Results 1 to 15 of 15

For Loop with Evaluate, How do I Correctly Reference Cells

  1. #1
    Registered User
    Join Date
    02-06-2019
    Location
    United States
    MS-Off Ver
    2016
    Posts
    7

    For Loop with Evaluate, How do I Correctly Reference Cells

    Long time Excel user but I'm new to VBA as I never have an occasion to need it until now. My application is a Finance Budget, I was able to get Solver running in VBA for one of my equations. However, what seemed to be the simpler equation is giving me fits.

    My basic spreadsheet setup is 27 columns wide, for each annual budget, 2019, 2020, etc. I'm simply trying to calculate a Withdrawal (row 98) = Year End Balance (row 145) - Minimum Required Balance (row 146). The VBA is to automate this calculation and then do it again for each annual budget, once, in each successive column (this cannot be done as a formula within Excel as it causes a circular reference). The repeating columns are G98:AG98 (For column = 7 To 33). My problem seems to be in the code to the right of the Evaluate, I'm simply trying to do G98 = G145-G146, and then repeat for each column, but clearly, even after reading a couple dozen examples I cannot the Cells(row, column) address correct for the calculation. I show a couple different non-working and working lines in the code with comments.

    Any insight where I've gone wrong is greatly appreciated.

    My code is:

    Please Login or Register  to view this content.

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,624

    Re: For Loop with Evaluate, How do I Correctly Reference Cells

    Please Login or Register  to view this content.
    Last edited by protonLeah; 02-09-2019 at 04:08 PM.
    Ben Van Johnson

  3. #3
    Registered User
    Join Date
    02-06-2019
    Location
    United States
    MS-Off Ver
    2016
    Posts
    7

    Re: For Loop with Evaluate, How do I Correctly Reference Cells

    Hello Ben,

    THANK YOU, absolutely amazing, that did the trick, I should have posted days ago instead of beating my head against the wall for so long. Now I can move on to get the rest of the spreadsheet working.

    Best Regards, FormatSort

  4. #4
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,108

    Re: For Loop with Evaluate, How do I Correctly Reference Cells

    Another option
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    02-06-2019
    Location
    United States
    MS-Off Ver
    2016
    Posts
    7

    Re: For Loop with Evaluate, How do I Correctly Reference Cells

    Hello Fluff13,

    Thank you for the reply, I'm glad to learn alternative options.

    Best Regards

  6. #6
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,108

    Re: For Loop with Evaluate, How do I Correctly Reference Cells

    You're welcome & thanks for the feedback

  7. #7
    Registered User
    Join Date
    02-06-2019
    Location
    United States
    MS-Off Ver
    2016
    Posts
    7

    Re: For Loop with Evaluate, How do I Correctly Reference Cells

    Ben,

    One more question please, is there a way in VBA to handle when I insert or delete rows or columns so VBA auto updates to the correct reference? I tested by inserting one line above line 98, so the formula starts at line 99, but VBA did not update. Is there a trick in how I reference things to get this behavior?

    Best Regards

  8. #8
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,108

    Re: For Loop with Evaluate, How do I Correctly Reference Cells

    How do you determine which row you need this in?
    Is there a specific word or phrase on that row, which does not appear anywhere else?

  9. #9
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,624

    Re: For Loop with Evaluate, How do I Correctly Reference Cells

    (How to) Upload a Workbook directly to the Forum
    (please do not post pictures or links to worksheets)
    • Click Advanced next to "Post Quick Reply" button at the bottom right of the editor box.
    • Scroll down until you see "Manage Attachments",
    • Click the "Choose" button at the upper left (upload from your computer).
    • Select your file, click "open", click "upload"
    • Once the upload is completed the file name will appear below the input boxes in this window.
    • Close the Attachment Manager window.
    • Click "Submit Reply"
    Note: Please do not attach password protected workbooks/worksheets
    Ensure to disable any Workbook Open/Autorun macros before attaching!

  10. #10
    Registered User
    Join Date
    02-06-2019
    Location
    United States
    MS-Off Ver
    2016
    Posts
    7

    Re: For Loop with Evaluate, How do I Correctly Reference Cells

    Quote Originally Posted by Fluff13 View Post
    How do you determine which row you need this in?
    Is there a specific word or phrase on that row, which does not appear anywhere else?
    Yes, Every Row in Column B contains a Description of each line. Every Column, in Row 3, contains the Year.

    Is there a way to reference these in the code so when I add or delete a row or column it will automatically update?
    Please Login or Register  to view this content.

  11. #11
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,108

    Re: For Loop with Evaluate, How do I Correctly Reference Cells

    What are the values?
    It would be best to supply as workbook as mentioned by protonLeah

  12. #12
    Registered User
    Join Date
    02-06-2019
    Location
    United States
    MS-Off Ver
    2016
    Posts
    7

    Re: For Loop with Evaluate, How do I Correctly Reference Cells

    Quote Originally Posted by Fluff13 View Post
    What are the values?
    It would be best to supply as workbook as mentioned by protonLeah
    I'm unable to provide a copy of the Workbook due to the proprietary data it contains. I strive to make my posts self explanatory due to this, sorry if I missed the mark.

    Let me give some further details:

    Minimum Required Balance (row 146) { Example: $10,000 }
    Code: Cells(146, columnIndex).Value

    Year End Balance (row 145) - { Example: starting value $3,000 } >>> Result after Macro runs: $10,000
    Code: Cells(145, columnIndex).Value -

    Withdrawal (row 98) = { Example: starting value zero } >>> Result after Macro runs: $7,000
    Code: Cells(98, columnIndex).Value =

    I was not looking for specific details on how to maintain Relative References for this example only, but I'm trying to learn what is possible in VBA so when I add or delete rows or columns the Relative References are maintained.

  13. #13
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,108

    Re: For Loop with Evaluate, How do I Correctly Reference Cells

    VBA is totally separate from Excel, so it's not like a formula that will adjust as rows/columns are added/deleted.
    You need a method of defining your range within the code.
    So if "Withdrawal" is only found on row 98 you can use Range.Find to locate it & set the row.
    If "Withdrawal" can be found in a number of different rows, then you will need to find another way of locating the correct row.

  14. #14
    Registered User
    Join Date
    02-06-2019
    Location
    United States
    MS-Off Ver
    2016
    Posts
    7

    Re: For Loop with Evaluate, How do I Correctly Reference Cells

    Excellent, thank you for the information.

  15. #15
    Forum Guru
    Join Date
    09-10-2017
    Location
    Chippenham, England
    MS-Off Ver
    365
    Posts
    15,108

    Re: For Loop with Evaluate, How do I Correctly Reference Cells

    You're welcome

+ 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. Replies: 11
    Last Post: 12-17-2018, 02:17 PM
  2. [SOLVED] Find value for reference, shift surrounding cells, and loop process until end of document
    By trevtrev in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 05-18-2017, 12:18 AM
  3. [SOLVED] Evaluate Match 3 sheets continue when error (probably Loop?)
    By psjpsjpsjpsj in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-10-2016, 07:02 PM
  4. [SOLVED] Cells reference not worknig in a loop
    By luv2glyd in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-04-2015, 01:28 PM
  5. For loop within loop not working correctly
    By HalPlz in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-25-2015, 12:06 AM
  6. [SOLVED] Loop - Reference Cells Correctly?
    By Janc in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-13-2015, 09:36 AM
  7. Changing my code to loop through a list of cells (lots of reference cells)
    By joshnathan in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-06-2014, 12:31 PM

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