Need to use a checking tab statement for several tabs and then receiving Else without If error

TkdKidSnake

Board Regular
Joined
Nov 27, 2012
Messages
245
Office Version
  1. 365
Platform
  1. Windows
Hi all,

I am trying to write a statement that checks if a tab is present and if it is perform certain tasks and if not move onto the next. Below is the code I have come up with however I keep getting the error message "Compile Error - Else Without If" also there is probably a better way of doing this.

Your help would be greatly appreciated.

See the code below:


Sub SortingBlocks()

Application.ScreenUpdating = False
Windows("002 - Downloaded Poan Report - RAW Data.xls").Activate

Dim sh As Worksheet, flg As Boolean
For Each sh In Worksheet

If sh.Name Like "GAL*" Then flg = True: Exit For
Next
If flg = True Then
Sheets("GAL").Select
Sheets("GAL").Move Before:=Sheets(1)
With ActiveWorkbook.Sheets("GAL").<wbr>Tab
.Color = 16711680
.TintAndShade = 0
'Call PageFormat
'Call Page1Ofxx
'Call RemoveUnwantedRows
Else

If sh.Name Like "SVC" Then flg = True: Exit For
Next
If flg = True Then
Sheets("SVC").Select
Sheets("SVC").Move Before:=Sheets(1)
With ActiveWorkbook.Sheets("SVC").<wbr>Tab
.Color = 16711680
.TintAndShade = 0
'Call PageFormat
'Call Page1Ofxx
'Call RemoveUnwantedRows
Else

If sh.Name Like "SLE*" Then flg = True: Exit For
Next
If flg = True Then
Sheets("SLE").Select
Sheets("SLE").Move Before:=Sheets(1)
With ActiveWorkbook.Sheets("SLE").<wbr>Tab
.Color = 16711680
.TintAndShade = 0
'Call PageFormat
'Call Page1Ofxx
'Call RemoveUnwantedRows
Else

If sh.Name Like "OLY*" Then flg = True: Exit For
Next
If flg = True Then
Sheets("OLY").Select
Sheets("OLY").Move Before:=Sheets(1)
With ActiveWorkbook.Sheets("OLY").<wbr>Tab
.Color = 16711680
.TintAndShade = 0
'Call PageFormat
'Call Page1Ofxx
'Call RemoveUnwantedRows
Else

If sh.Name Like "NEW*" Then flg = True: Exit For
Next
If flg = True Then
Sheets("NEW").Select
Sheets("NEW").Move Before:=Sheets(1)
With ActiveWorkbook.Sheets("NEW").<wbr>Tab
.Color = 16711680
.TintAndShade = 0
'Call PageFormat
'Call Page1Ofxx
'Call RemoveUnwantedRows
Else

If sh.Name Like "KBR*" Then flg = True: Exit For
Next
If flg = True Then
Sheets("KBR").Select
Sheets("KBR").Move Before:=Sheets(1)
With ActiveWorkbook.Sheets("KBR").<wbr>Tab
.Color = 16711680
.TintAndShade = 0
'Call PageFormat
'Call Page1Ofxx
'Call RemoveUnwantedRows
Else

If sh.Name Like "EUR*" Then flg = True: Exit For
Next
If flg = True Then
Sheets("EUR").Select
Sheets("EUR").Move Before:=Sheets(1)
With ActiveWorkbook.Sheets("EUR").<wbr>Tab
.Color = 16711680
.TintAndShade = 0
'Call PageFormat
'Call Page1Ofxx
'Call RemoveUnwantedRows
Else

If sh.Name Like "CDE" Then flg = True: Exit For
Next
If flg = True Then
Sheets("CDE").Select
Sheets("CDE").Move Before:=Sheets(1)
With ActiveWorkbook.Sheets("CDE").<wbr>Tab
.Color = 16711680
.TintAndShade = 0
'Call PageFormat
'Call Page1Ofxx
'Call RemoveUnwantedRows
Else
End If

End Sub



Thanks in advance!
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
The problem is that none of your WITH statements has an END WITH
 
Upvote 0
there are other problems too.

check is sh.name LIKE something but then you don't reference the sh.name you use a hard coded value of the first 3 characters looked for. This may be valid but if it is them use the = operator not LIKE.
 
Upvote 0
Try this. It could probably be simpler but I thought there might be something different depending on the sheet type.

Code:
Sub SortingBlocks()


Application.ScreenUpdating = False
Windows("002 - Downloaded Poan Report - RAW Data.xls").Activate


Dim sh As Worksheet
For Each sh In sheets


If sh.Name Like "GAL*" Then 
  Sh.Select
  Sh.Move Before:=Sheets(1)
  With sh.Tab
    .Color = 16711680
    .TintAndShade = 0
  end with
  'Call PageFormat
  'Call Page1Ofxx
  'Call RemoveUnwantedRows
ElseIf sh.Name Like "SVC" Then
  Sh.Select
  Sh.Move Before:=Sheets(1)
  With sh.Tab
    .Color = 16711680
    .TintAndShade = 0
  End With
  'Call PageFormat
  'Call Page1Ofxx
  'Call RemoveUnwantedRows
ElseIf sh.Name Like "SLE*" Then 
  Sh.Select
  Sh.Move Before:=Sheets(1)
  With sh.Tab
    .Color = 16711680
    .TintAndShade = 0
  End With
  'Call PageFormat
  'Call Page1Ofxx
  'Call RemoveUnwantedRows
ElseIf sh.Name Like "OLY*" Then
  Sh.Select
  Sh.Move Before:=Sheets(1)
  With sh.Tab
    .Color = 16711680
    .TintAndShade = 0
  End With
  'Call PageFormat
  'Call Page1Ofxx
  'Call RemoveUnwantedRows
ElseIf sh.Name Like "NEW*" Then
  Sh.Select
  Sh.Move Before:=Sheets(1)
  With Sh.Tab
    .Color = 16711680
    .TintAndShade = 0
  End With
  'Call PageFormat
  'Call Page1Ofxx
  'Call RemoveUnwantedRows
ElseIf sh.Name Like "KBR*" Then
  Sh.Select
  Sh.Move Before:=Sheets(1)
  With Sh.Tab
    .Color = 16711680
    .TintAndShade = 0
  End With
  'Call PageFormat
  'Call Page1Ofxx
  'Call RemoveUnwantedRows
ElseIf sh.Name Like "EUR*" Then
  Sh.Select
  Sh.Move Before:=Sheets(1)
  With Sh.Tab
    .Color = 16711680
    .TintAndShade = 0
  End With
  'Call PageFormat
  'Call Page1Ofxx
  'Call RemoveUnwantedRows
ElseIf sh.Name Like "CDE" Then
  Sh.Select
  Sh.Move Before:=Sheets(1)
  With Sh.Tab
    .Color = 16711680
    .TintAndShade = 0
  End With
  'Call PageFormat
  'Call Page1Ofxx
  'Call RemoveUnwantedRows
End If
next

End Sub
 
Upvote 0
Try this. It could probably be simpler but I thought there might be something different depending on the sheet type.

Code:
Sub SortingBlocks()


Application.ScreenUpdating = False
Windows("002 - Downloaded Poan Report - RAW Data.xls").Activate


Dim sh As Worksheet
For Each sh In sheets


If sh.Name Like "GAL*" Then 
  Sh.Select
  Sh.Move Before:=Sheets(1)
  With sh.Tab
    .Color = 16711680
    .TintAndShade = 0
  end with
  'Call PageFormat
  'Call Page1Ofxx
  'Call RemoveUnwantedRows
ElseIf sh.Name Like "SVC" Then
  Sh.Select
  Sh.Move Before:=Sheets(1)
  With sh.Tab
    .Color = 16711680
    .TintAndShade = 0
  End With
  'Call PageFormat
  'Call Page1Ofxx
  'Call RemoveUnwantedRows
ElseIf sh.Name Like "SLE*" Then 
  Sh.Select
  Sh.Move Before:=Sheets(1)
  With sh.Tab
    .Color = 16711680
    .TintAndShade = 0
  End With
  'Call PageFormat
  'Call Page1Ofxx
  'Call RemoveUnwantedRows
ElseIf sh.Name Like "OLY*" Then
  Sh.Select
  Sh.Move Before:=Sheets(1)
  With sh.Tab
    .Color = 16711680
    .TintAndShade = 0
  End With
  'Call PageFormat
  'Call Page1Ofxx
  'Call RemoveUnwantedRows
ElseIf sh.Name Like "NEW*" Then
  Sh.Select
  Sh.Move Before:=Sheets(1)
  With Sh.Tab
    .Color = 16711680
    .TintAndShade = 0
  End With
  'Call PageFormat
  'Call Page1Ofxx
  'Call RemoveUnwantedRows
ElseIf sh.Name Like "KBR*" Then
  Sh.Select
  Sh.Move Before:=Sheets(1)
  With Sh.Tab
    .Color = 16711680
    .TintAndShade = 0
  End With
  'Call PageFormat
  'Call Page1Ofxx
  'Call RemoveUnwantedRows
ElseIf sh.Name Like "EUR*" Then
  Sh.Select
  Sh.Move Before:=Sheets(1)
  With Sh.Tab
    .Color = 16711680
    .TintAndShade = 0
  End With
  'Call PageFormat
  'Call Page1Ofxx
  'Call RemoveUnwantedRows
ElseIf sh.Name Like "CDE" Then
  Sh.Select
  Sh.Move Before:=Sheets(1)
  With Sh.Tab
    .Color = 16711680
    .TintAndShade = 0
  End With
  'Call PageFormat
  'Call Page1Ofxx
  'Call RemoveUnwantedRows
End If
next

End Sub


Thank you PAR60056 I'll give it a go.
 
Upvote 0
Hi

Fyi - You don't need to select the sheet before sh.Move, sh.Move makes the candidate sheet the Active sheet.

hth
 
Upvote 0
Would this be easier?

Code:
Sub SortingBlocks()
 
Application.ScreenUpdating = False
 
Windows("002 - Downloaded Poan Report - RAW Data.xls").Activate
 
Dim s As Long, i As Long
 
s = Sheets.Count
 
For i = 1 To s
       If Sheets(i).Name Like "GAL*" Or Sheets(i).Name Like "SVC*" _
       Or Sheets(i).Name Like "SLE*" Or Sheets(i).Name Like "OLY*" _
       Or Sheets(i).Name Like "NEW*" Or Sheets(i).Name Like "KBR*" _
       Or Sheets(i).Name Like "EUR*" Or Sheets(i).Name Like "CDE*" Then
        Sheets(i).Move Before:=Sheets(1)
         
        'Do the rest of the things you want to do to the sheet
       End If
 
'Move to next sheet in loop
Next I
 
MsgBox "Done!"
 
End Sub
 
Upvote 0
rallcorn,
I debated rewriting it like that but I thought there might be something different that would be done to each sheet type (SVC, CDE, KBR...)

I tried to keep the code as close to the original as would work. Often people don't give the full explanation of what they need to accomplish. For example, I'm not sure why you would always move the sheet to the 1st position. I assume there are several sheets that match the names searched for. Not sure how many sheets there are in a file and they are being moved this way.
 
Upvote 0
par60056,

I debated rewriting it like that but I thought there might be something different that would be done to each sheet type (SVC, CDE, KBR...)
Understood - but I believe the code from original post had the same things being done for each sheet "type" . . . and there was so many things wrong with the original code!

Often people don't give the full explanation of what they need to accomplish. For example, I'm not sure why you would always move the sheet to the 1st position. I assume there are several sheets that match the names searched for. Not sure how many sheets there are in a file and they are being moved this way.
Agree wholeheartedly!

It wasn't my intention to criticize your solution, I just chimed in to offer an alternative approach.
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,380
Members
449,080
Latest member
Armadillos

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