Macro - with If statements

Nyanko

Active Member
Joined
Sep 1, 2005
Messages
437
Hi,

The below code is part of a larger routine, but I'm having trouble getting the order correct !

What I want to happen depends on the value of "Flag" For some values it's simply creating a value to be used later. But for some the process needed is more complex.

Code:
  Do While Flag <> "" 'look down rows if cells do not equal ""
    If Flag = "" Then Exit For 
    If Flag = "LTS" Then OtherFlag = "Long Term Sick"
    If Flag = "SICKR" Then OtherFlag = "Sick on Rest Day"
                  rowcount = rowcount + 1
                  Flag = Cells(rowcount, colcount)
    If Flag = "x" Then rowcount = rowcount + 1
                 Flag = Cells(rowcount, colcount)
  Loop

As you can see (hopefully !) if Flag equals "x" or "SICKR" there are several operations that need to happen for that instance only. However what is happening is that the code is being read as linear whereas I need it to skip to each If statement.

I really really hope that makes sense !!!
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Try

Code:
Do While Flag <> "" 'look down rows if cells do not equal ""
    If Flag = "" Then
        Exit For
    ElseIf Flag = "LTS" Then
        OtherFlag = "Long Term Sick"
    ElseIf Flag = "SICKR" Then
        OtherFlag = "Sick on Rest Day"
        RowCount = RowCount + 1
        Flag = Cells(RowCount, colcount)
    ElseIf Flag = "x" Then
        RowCount = RowCount + 1
        Flag = Cells(RowCount, colcount)
    End If
  Loop
 
Upvote 0
What For are you exiting here?
Code:
If Flag = "" Then Exit For
 
Upvote 0
What For are you exiting here?
Code:
If Flag = "" Then Exit For

It's part of a larger code that works fine ... it's just to show here that it's where the code needs to exit.

VoG II - that looks like a good solution, I'll give it a try.

Many thanks
 
Upvote 0
Try

Code:
Do While Flag <> "" 'look down rows if cells do not equal ""
    If Flag = "" Then
        Exit For
    ElseIf Flag = "LTS" Then
        OtherFlag = "Long Term Sick"
    ElseIf Flag = "SICKR" Then
        OtherFlag = "Sick on Rest Day"
        RowCount = RowCount + 1
        Flag = Cells(RowCount, colcount)
    ElseIf Flag = "x" Then
        RowCount = RowCount + 1
        Flag = Cells(RowCount, colcount)
    End If
  Loop

Hi, I try this and get the error :
Compile Error - Else without If
 
Upvote 0
whyy don't to try "Select case" .... that will make code more clean and easy to work with something like

Code:
Select Case Flag
  Case ""
      Exit ..... what ever actioin u want 
  Case "LTS"
     OtherFlag = "Long Term Sick"
  Case "SICKR"
     OtherFlag = "Sick on Rest Day"
     RowCount = RowCount + 1
     Flag = Cells(RowCount, colcount)
  Case "x"
     RowCount = RowCount + 1
     Flag = Cells(RowCount, colcount)
  Case Else
      MsgBox "Invalid Flag" 
      Exit ..... what ever actioin u want
End Select
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,858
Members
449,051
Latest member
excelquestion515

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top