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
238
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!
 

Some videos you may like

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)

par60056

Well-known Member
Joined
Jul 26, 2012
Messages
1,581
Office Version
  1. 2011
  2. 2010
Platform
  1. Windows
  2. MacOS
The problem is that none of your WITH statements has an END WITH
 

par60056

Well-known Member
Joined
Jul 26, 2012
Messages
1,581
Office Version
  1. 2011
  2. 2010
Platform
  1. Windows
  2. MacOS
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.
 

par60056

Well-known Member
Joined
Jul 26, 2012
Messages
1,581
Office Version
  1. 2011
  2. 2010
Platform
  1. Windows
  2. MacOS

ADVERTISEMENT

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
 

TkdKidSnake

Board Regular
Joined
Nov 27, 2012
Messages
238
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.
 

ukmikeb

Well-known Member
Joined
Jul 10, 2009
Messages
2,757

ADVERTISEMENT

Hi

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

hth
 

rallcorn

Well-known Member
Joined
Nov 11, 2008
Messages
1,027
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
 

par60056

Well-known Member
Joined
Jul 26, 2012
Messages
1,581
Office Version
  1. 2011
  2. 2010
Platform
  1. Windows
  2. MacOS
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.
 

rallcorn

Well-known Member
Joined
Nov 11, 2008
Messages
1,027
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.
 

Watch MrExcel Video

Forum statistics

Threads
1,123,323
Messages
5,600,954
Members
414,417
Latest member
Nobu

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
Top