+ Reply to Thread
Results 1 to 5 of 5

Looping Through A Range Of Cells

  1. #1
    Registered User
    Join Date
    09-21-2006
    Posts
    28

    Looping Through A Range Of Cells

    Dear All,

    Firstly i would like all of you for all your help i am finding this forum very useful and looking through posts i have learn't alot about wrting macros.

    With that said i was wondering if someone could help me with the following.

    I am trying to write a loop which will loop through a range of cells check the contents for one of four possible answers and act accordingly.

    Please excuse the laymens terms code below but as i am struggling to write this loop i can only write whats logical in my brain.

    Please Login or Register  to view this content.
    Hopefully you can see what i am trying to do i'm just not sure how to write this.

    Any help much appreciated.

  2. #2
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606
    What's not clear to me is that you appear to be looping through a range of cells but you are updating the same cell each time (MasterCosts.Range("C5")). This means its final value will be determined by the last value in the range (E11). This doesn't seem right?
    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    09-21-2006
    Posts
    28
    Sorry Stephen i need C5 to increase each time i.e next time through the loop it will be C6 next time C7

    I have solved the problem now and maybe i was a premature in posting

    My apologies.

    my solution is as follows

    Please Login or Register  to view this content.
    It's a bit sloppy i know and i have had to set the value of Z outside the loop as it needs to start on 6. After this loop i have finished with Z.

    One last question i notice you declared rng as a variable prior to the the loop and i have just used i and not declared it, is this a bit naughty.

    If you have any suggestions to improve this please let me know.

    Else please set this one as solved.

  4. #4
    Forum Guru
    Join Date
    08-26-2007
    Location
    London
    Posts
    4,606
    I like the idea of it being a bit naughty.

    It's good practice to declare your variables, and there are various conventions for naming variables so as to indicate their type. So, if you can get in the habit of doing it, do so because it can make your code easier to read (especially for others or if you come back to it after a long gap) and it can facilitate error-trapping.

    But if you find it difficult, you're unlikely to be taken from your bed in the middle of the night by the VBA police.

    I think you have to set it as solved.

  5. #5
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    You could possibly use VLOOKUP formulas
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

+ 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