Solved

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

Posted on 2014-04-24
11
156 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
Comment Utility
What exactly do you want to check?

Is it if B1 is blank?
0
 
LVL 45

Expert Comment

by:aikimark
Comment Utility
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 48

Expert Comment

by:Rgonzo1971
Comment Utility
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
 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
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
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 68

Expert Comment

by:Qlemo
Comment Utility
... 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
Comment Utility
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 68

Accepted Solution

by:
Qlemo earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks for the help. EXCELlent!!!
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Drop Down List with Unique/Distinct Values (Part II - ComboBox or ListBox and Data Validation List Bonus!) David Miller (dlmille) Intro This article focuses on delivering unique, sorted lists to list objects (e.g., ComboBox, ListBox) and Dat…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate how to create pivot charts out of a data set. I also added a drop-down menu which allows to choose from different categories in the data set and the chart will automatically update.

744 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now