Results 1 to 3 of 3

Thread: VBA: For Each loop not going through cells in range
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    New Member
    Join Date
    Mar 2019
    Posts
    17
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default VBA: For Each loop not going through cells in range

    This is my code:
    'Select range
    Set rng1 = Range("K5:N5")
    Set rng2 = Range("H6:N56")
    Set rng3 = Range("H57:K57")

    Set totalrng = Union(rng1, rng2, rng3)
    totalrng.Select

    'Call SumPassengers sub
    For Each cell In totalrng
    Call SumPassengers
    Next cell

    The SumPassengers sub is a large number of if statements and sumifs (that works fine). When I run this code, it puts the sum total in for the first cell in the range (K5) but none of the others in the selected range. How can I fix this?

  2. #2
    Board Regular
    Join Date
    Jun 2005
    Location
    London
    Posts
    7,755
    Post Thanks / Like
    Mentioned
    7 Post(s)
    Tagged
    1 Thread(s)

    Default Re: VBA: For Each loop not going through cells in range

    I think you need to post all of your code but what happens when you step through the code using F8?

    Btw, you don't need to select the range.
    Neil

  3. #3
    Board Regular Tim_Excel_'s Avatar
    Join Date
    Jul 2016
    Posts
    512
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    4 Thread(s)

    Default Re: VBA: For Each loop not going through cells in range

    I do not see any problems with the code you posted (please use code tags), so I am assuming the problem lies in the sub you are calling for each cell.
    Like Neil mentioned, use F8 to step through the code to see how the code executes.
    Always make a backup of your data before trying out any code

    Before you ask why my code is not working: have you checked if the references are correct? Are you referring to the correct Workbook/-sheet?

    Use [CODE] tags when pasting code in your posts

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •