Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2014-04-24
11
Medium Priority
?
167 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 35

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 53

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

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…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.

877 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