Solved

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

Posted on 2014-04-24
11
160 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 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 51

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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
LVL 70

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 70

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 70

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.

751 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