Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-04-24
11
Medium Priority
?
165 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +3
11 Comments
 
LVL 34

Expert Comment

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

Is it if B1 is blank?
0
 
LVL 46

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 52

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 71

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 71

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 71

Accepted Solution

by:
Qlemo earned 2000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

721 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