Else If Question

Guzzlr

Well-known Member
Joined
Apr 20, 2009
Messages
982
Office Version
  1. 2021
Platform
  1. Windows
Hello All

Code:
For Each sht In ActiveWorkbook.Worksheets
    If sht.Visible And (sht.Name = "Alignment") Then
    sht.Delete
    Sheets.Add.Name = "Alignment"
    sht.Activate
    Cells.Select
        With Selection
            .Clear
            .RowHeight = 20.01
            .ColumnWidth = 8.43
            Columns("B:C").ColumnWidth = 19.01
            .NumberFormat = "0.00"
            .Font.Name = "Calibri"
            .Font.Size = 12
            .Interior.Pattern = xlNone
            .HorizontalAlignment = xlCenter
            .VerticalAlignment = xlCenter
            Range("B2") = "MES Document"
            Range("B2").Interior.ColorIndex = 4
'            Range("D6") = "Not Used"
'            Range("D6").Interior.Color = RGB(255, 157, 91)
            Range("A1").Select
        End With
    End If
Next sht

This code will add and format a new "Alignment" sheet correctly. But what if there is no "Alignment" sheet already in the workbook? This is where I'm having problems using an ElseIf statement, it seems it wants to keep adding multiple worksheets...
I got irritated and deleted the Else part of it.
Sorry
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Try this
Code:
For Each Sht In ActiveWorkbook.Worksheets
    If Sht.Visible And (Sht.Name = "Alignment") Then
      Sht.Delete
      Exit For
    End If
Next Sht

Sheets.Add.Name = "Alignment"
Cells.Select
With Selection
    .Clear
    .RowHeight = 20.01
    .ColumnWidth = 8.43
    Columns("B:C").ColumnWidth = 19.01
    .NumberFormat = "0.00"
    .Font.Name = "Calibri"
    .Font.size = 12
    .Interior.Pattern = xlNone
    .HorizontalAlignment = xlCenter
    .VerticalAlignment = xlCenter
    Range("B2") = "MES Document"
    Range("B2").Interior.ColorIndex = 4
'            Range("D6") = "Not Used"
'            Range("D6").Interior.Color = RGB(255, 157, 91)
    Range("A1").Select
End With
 
Upvote 0
Don't it do it that way! What that will do is try to add a new sheet named "Alignment" the number of times as the number of sheets you have NOT named "Alignment".

That is exactly what it was doing, and why I was getting really irritated!
 
Upvote 0
Try this
Code:
For Each Sht In ActiveWorkbook.Worksheets
    If Sht.Visible And (Sht.Name = "Alignment") Then
      Sht.Delete
      Exit For
    End If
Next Sht

Sheets.Add.Name = "Alignment"
Cells.Select
With Selection
    .Clear
    .RowHeight = 20.01
    .ColumnWidth = 8.43
    Columns("B:C").ColumnWidth = 19.01
    .NumberFormat = "0.00"
    .Font.Name = "Calibri"
    .Font.size = 12
    .Interior.Pattern = xlNone
    .HorizontalAlignment = xlCenter
    .VerticalAlignment = xlCenter
    Range("B2") = "MES Document"
    Range("B2").Interior.ColorIndex = 4
'            Range("D6") = "Not Used"
'            Range("D6").Interior.Color = RGB(255, 157, 91)
    Range("A1").Select
End With

Ohh...I didn't think to do it this way...I'll try it later and report back.
 
Upvote 0
Try this
Code:
For Each Sht In ActiveWorkbook.Worksheets
    If Sht.Visible And (Sht.Name = "Alignment") Then
      Sht.Delete
      Exit For
    End If
Next Sht

Sheets.Add.Name = "Alignment"
Cells.Select
With Selection
    .Clear
    .RowHeight = 20.01
    .ColumnWidth = 8.43
    Columns("B:C").ColumnWidth = 19.01
    .NumberFormat = "0.00"
    .Font.Name = "Calibri"
    .Font.size = 12
    .Interior.Pattern = xlNone
    .HorizontalAlignment = xlCenter
    .VerticalAlignment = xlCenter
    Range("B2") = "MES Document"
    Range("B2").Interior.ColorIndex = 4
'            Range("D6") = "Not Used"
'            Range("D6").Interior.Color = RGB(255, 157, 91)
    Range("A1").Select
End With

This is working...Thank you!
 
Upvote 0
Glad to help & thanks for the feedback

Though as Joe4 pointed out, you can get rid of the loop & replace it with a function.
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,248
Members
452,623
Latest member
cliftonhandyman

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