Solved

For loop works for >1 but not for one or blank.

Posted on 2014-04-24
11
159 Views
Last Modified: 2014-04-26
I am having issues with the following statement:
For Each cel In Range(Range("B1"), Range("B1").End(xlToRight))

There are times that there is one, and it must be going way right, because it keeps going and going.  Other times is blank.  How do I rewrite this so it works when it is one or greater, but if B1 is blank to allows for a msgbox?
0
Comment
Question by:RWayneH
  • 3
  • 3
  • 2
  • +3
11 Comments
 
LVL 33

Expert Comment

by:Norie
ID: 40021723
What exactly do you want to check?

Is it if B1 is blank?
0
 
LVL 45

Expert Comment

by:aikimark
ID: 40021804
Something like this, perhaps:
Dim rng as Range, cel as Range
Set rng = Range(Range("B1"), Range("B1").End(xlToRight))
If (worksheetfunction.Count(rng) + worksheetfunction.CountA(rng) ) = 0 Then
    msgbox "empty range"
Else
    For Each cel In rng

    Next
End If

Open in new window

0
 
LVL 50

Expert Comment

by:Rgonzo1971
ID: 40021981
Hi,

pls try

Dim myRange As Range, cell As Range
Set myRange = Range(Range("B1"), Range("B1").End(xlToRight))
If (WorksheetFunction.Count(myRange) + WorksheetFunction.CountA(myRange)) = 0 Then
    MsgBox "empty range"
ElseIf myRange.Resize(1, 1).Offset(0, 1).Value = "" Then
    'Your code
Else
    For Each cell In myRange
        'Your code
    Next
End If

Open in new window

Regards
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 69

Expert Comment

by:Qlemo
ID: 40022211
IMO checking for B1 being empty should be enough:
If IsEmpty([B1]) Then
  MsgBox ...
Else
  For Each cel In Range(Range("B1"), Range("B1").End(xlToRight))
  ' ...
  Next
End If

Open in new window

0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 40022313
You'd need to check C1 as well. If B1 is populated but C1 is not, using End(xlToRight) will extend the range to the last column of the worksheet.

FWIW, for the other solutions, only CountA is necessary since it counts numbers too. ;)
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 40022340
... or we use [A1].End(xlToRight) instead, like in
For Each cel In Range([B1], [A1].End(xlToRight))

Open in new window

0
 

Author Comment

by:RWayneH
ID: 40022717
Thinking through the if statements prior to the For loop I could check B1
I am not good at grouping the if statements together or doing the Case structure but I think this would work and also sure that this could be written a little better without having to use the Goto 150

If IsEmpty([B1]) Then
  MsgBox ...
' Send it past the For loop
' Goto 150
End If

If IsEmpty([C1]) Then
' Run procedure once
' Send it past the For loop
  Goto 150
End If

If NotIsEmpty([C1]) Then
 For Each cel In Range(Range("B1"), Range("B1").End(xlToRight))
  'Loop through procedure as many time as neccessay ...
  Next

150
0
 
LVL 69

Accepted Solution

by:
Qlemo earned 500 total points
ID: 40022801
Too much ado about nothing.  As I told, just check B1 for being empty, and then run the For Each:
If IsEmpty([B1]) Then
  MsgBox ...
Else
  For Each cel In Range([B1], [A1].End(xlToRight))
  ' ...
  Next
End If

Open in new window

0
 

Author Comment

by:RWayneH
ID: 40022889
Can you explain what the [A1] does? I know that A1 cannot be part of the range.  The range starts at B1.  Perhaps I am not understanding this.  If the parsing of A1 returns only one value, it was going all the way out to column IV.  Does the [A1] stop it from going that far out and keeps the for loop at running only once?
0
 
LVL 85

Expert Comment

by:Rory Archibald
ID: 40022912
If B1 is empty, the first test in Qlemo's code will catch it. If it isn't, [A1].end(xltoright) will stop at B1 if C1 is empty, or carry on until the next blank cell if not, which is what you want.
0
 

Author Closing Comment

by:RWayneH
ID: 40025197
Thanks for the help. EXCELlent!!!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question