Go Back   DeveloperBarn Forums > Operating Systems, Servers & Software > Microsoft Office

Sponsored Links

Discuss "Break & Continue in VB (Excel)" in the Microsoft Office forum.

Microsoft Office - Learn helpful tips and tricks to get the best out of Office suite, using Excel, Word, PowerPoint and more. Discuss how to automate repetitive tasks and more.


Reply « Previous Thread | Next Thread »  
 
LinkBack Thread Tools Display Modes
  #1  
Old July 23rd, 2008, 05:20 PM
Lauramc's Avatar
I like Data Cubes too...

 
Join Date: Mar 2008
Location: Far Far Away
Posts: 47
Thanks: 1
Thanked 4 Times in 2 Posts
Rep Power: 1
Lauramc is on a distinguished road

Awards Showcase
Microsoft SQL Server 
Total Awards: 1

Default Break & Continue in VB (Excel)

I have a process that I need to run in an Excel spreadsheet using a macro. I have a loop that iterates through the records and evaluates them; then performs an action based on that. Sometimes there are conditions where I need to break out of the loop, but I want to continue if the condition that caused a break warrants it.

Unfortunately, I see that there is no Break... Continue available (as it is in C#). Any ideas on how to circumvent that?

Thanks!

P.S. I can post code if needed
__________________
"The Enrichment Center is required to remind you that first you will be baked, then there will be cake." - GLaDOS
Reply With Quote
Sponsored Links
  #2  
Old July 23rd, 2008, 05:42 PM
jmurrayhead's Avatar
The Barnfather

 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 817
Thanks: 20
Thanked 74 Times in 71 Posts
Blog Entries: 5
Rep Power: 3
jmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura about

Awards Showcase
Microsoft .Net Microsoft SQL Server Microsoft Windows Classic ASP 
Total Awards: 4

Default

Posting one's code is always helpful...

Anyway...depending on the loop, there are ways to get out of it. For example, in a For...Next loop use: Exit For
__________________
jmurrayhead
If you agree with me... click the icon!
If my post solved your problem, click the button in the lower right-hand corner of the post.

Join our Folding team: DeveloperBarn Folding
Reply With Quote
  #3  
Old July 23rd, 2008, 07:44 PM
Lauramc's Avatar
I like Data Cubes too...

 
Join Date: Mar 2008
Location: Far Far Away
Posts: 47
Thanks: 1
Thanked 4 Times in 2 Posts
Rep Power: 1
Lauramc is on a distinguished road

Awards Showcase
Microsoft SQL Server 
Total Awards: 1

Default

Exiting is not a problem. I use Exit Do in this case. I thought about it further, and I am thinking that I may need a function call in the loop to make that decision as to whether to exit the loop. When I get that worked out I will post back, but in the meantime, I guess I am without a continue.... probably a good thing anyway.

As for posting code, the only reason I did not do so, is that the code makes no sense unless you know the problem I am trying to solve... that explanation is not short
Reply With Quote
  #4  
Old July 23rd, 2008, 08:19 PM
jmurrayhead's Avatar
The Barnfather

 
Join Date: Mar 2008
Location: Reston, VA, USA
Posts: 817
Thanks: 20
Thanked 74 Times in 71 Posts
Blog Entries: 5
Rep Power: 3
jmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura aboutjmurrayhead has a spectacular aura about

Awards Showcase
Microsoft .Net Microsoft SQL Server Microsoft Windows Classic ASP 
Total Awards: 4

Default

I'm not sure I totally understand...but maybe look into GoTo
Reply With Quote
  #5  
Old July 23rd, 2008, 08:45 PM
Lauramc's Avatar
I like Data Cubes too...

 
Join Date: Mar 2008
Location: Far Far Away
Posts: 47
Thanks: 1
Thanked 4 Times in 2 Posts
Rep Power: 1
Lauramc is on a distinguished road

Awards Showcase
Microsoft SQL Server 
Total Awards: 1

Default

I thought about GoTo, but I try not to use it if possible (as it makes the code less readable). I will create a function I think... then call that function in the loop. The purpose of the function would be to make the decision as to whether to continue the process or not based on the data in the current record (if that makes any sense). It is probably better to do it that way anyhow, but when I get it done, I will post here
Reply With Quote
  #6  
Old July 24th, 2008, 05:59 PM
Lauramc's Avatar
I like Data Cubes too...

 
Join Date: Mar 2008
Location: Far Far Away
Posts: 47
Thanks: 1
Thanked 4 Times in 2 Posts
Rep Power: 1
Lauramc is on a distinguished road

Awards Showcase
Microsoft SQL Server 
Total Awards: 1

Post

Update... I think I got this in a workable state finally. This process basically takes records in a label format (inserted into column A) i.e.:
Name
Address
City, State zip
and inserts a new line after each. A subsequent process will split the data into fields on the new line. There is probably an easier way to do this, of that I am certain
Code:
Sub linereplace()

Dim recno As Integer
recno = 1

Dim checktext As String
Dim EndofLine As String
Dim BoxCheck As String
Dim IsEndOfLine As Boolean
IsEndOfLine = False 'Initialize variable to False... we'll change it when needed.

Range("A65536").Select 'This is the presumed end of the worksheet
Selection.End(xlUp).Select

Dim LastRow As Integer
LastRow = ActiveCell.Row

Dim MaxRecord As Integer
MaxRecord = LastRow * 3

'First delete any data in columns other than A
Columns("B:IV").Select
Selection.Delete

'Remove blank lines sub:
RemoveBlankLines

Range("A1").Select 'Ensures that A1 is the start point

Do Until recno = MaxRecord
 
checktext = Range("A" & recno)
checktext = Strings.Trim(checktext)
checktext = Strings.Replace(checktext, "-", "")
 
Dim EvalArr As Variant
EvalArr = Split(checktext, " ", , vbTextCompare)

On Error Resume Next

Dim MaxArr As Variant
MaxArr = UBound(EvalArr)

'the last item in the line must be numeric
If IsNumeric(EvalArr(MaxArr)) Then
    IsEndOfLine = True
End If

If Len(EvalArr(MaxArr)) > 9 Or Len(EvalArr(MaxArr)) < 5 Then
    IsEndOfLine = False
End If

If IsEndOfLine = True Then
    Dim iCount As Integer
    iCount = 0

    For iCount = 0 To MaxArr
    Dim s As String
    s = EvalArr(iCount)

    If Strings.LCase(s) = "box" Or Strings.LCase(s) = "pmb" Then
        IsEndOfLine = False
        Exit For
    End If
    Next
End If

If IsEndOfLine = True Then
    Dim NewCell As String
    NewCell = Range("A" & (recno + 1))
    If Not NewCell = "" Then
        Rows(recno + 1 & ":" & recno + 1).Select
        Selection.Insert shift:=xlDown
    End If 'Evaluate New Cell Contents
End If 'Is this the end of the line?
recno = recno + 1
Loop
 
End Sub

Private Sub RemoveBlankLines()
'Select the last row then move up to the last row where data is found.
Range("A65536").Select
Selection.End(xlUp).Select

Do Until ActiveCell.Row = 1
Dim i As Integer

Selection.End(xlUp).Select
i = ActiveCell.Row

Dim RemoveRow As Integer
RemoveRow = i - 1
If RemoveRow < 1 Then Exit Do

Rows(RemoveRow & ":" & RemoveRow).Select
Selection.Delete shift:=xlUp

Loop 'End remove extra spaces loop

End Sub
It always surprises me how complex the simplest task can be! The only way to know that the end of the record was reached is when the last item on a line is a number AND it is not a PO Box or PMB. However, I can see there being other exclusions as well....
Reply With Quote
Reply

  DeveloperBarn Forums > Operating Systems, Servers & Software > Microsoft Office

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
From excel to database/web app Rebelle Database Design Help 7 July 10th, 2008 01:36 PM
Excel question Rebelle Microsoft Office 11 May 27th, 2008 07:22 PM
Retrieving Web Data using Excel mehere Microsoft Office 13 May 22nd, 2008 03:14 PM
opening excel file todd2006 ASP Development 5 April 30th, 2008 05:36 AM
export to excel function prompt help Rebelle JavaScript Programming 2 April 14th, 2008 10:40 AM


All times are GMT -4. The time now is 12:30 PM.



Content Relevant URLs by vBSEO 3.2.0