Excel VBA Not Running Second ElseIf statement?

SomethngWicked

Board Regular
Joined
Feb 18, 2015
Messages
80
Hi All -

I have a CF macro that I'm applying to 20+ worksheets within a workbook. However, there are two worksheets that don't contain the same formatted data and I need to run a modified version of the CF statement to these two worksheets. Initially, I just wrote two elseif statements and this appears to work on the first worksheet, but does not seem to be applying to the second. Can someone please tell me where the train is coming off the tracks? I've reviewed some elseif literature online and I can't seem to find the issue. Below is some of my code. Thanks!

Code:
Sub RunCF()
    Dim xsheet As Worksheet
    For Each xsheet In ThisWorkbook.Worksheets
    If xsheet.Name <> ("Mast") And xsheet.Name <> ("SR1") And xsheet.Name <> ("SR2") Then
        xsheet.Select

    Range("A2:R100").Select
   'CF STATEMENT IS HERE'
    End With
   
    ElseIf Sheets("SR1").Select Then
    
    Range("A2:J100").Select
    'CF STATEMENT IS HERE'
    End With
    
    ElseIf Sheets("SR2").Select Then
    Range("A2:J100").Select
    'CF STATEMENT IS HERE'
    End With
   
    End If
 Next xsheet
     
End Sub
 

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
That code doesn't compile, so it won't run at all.
 
Upvote 0
You may want to use a different code structure:

Code:
Sub RunCF()
  Dim wks           As Worksheet

  For Each wks In ThisWorkbook.Worksheets
    Select Case LCase(wks.Name)
      Case "sr1"
        With wks.Range("A2:J100")
          ' CF statements here
        End With

      Case "sr2"
        With wks.Range("A2:J100")
          ' CF statements here
        End With

      Case "mast"
        ' whatever

      Case Else
        With wks.Range("A2:R100")
          ' CF statements here
        End With
    End Select
  Next wks
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,954
Messages
6,175,601
Members
452,658
Latest member
GStorm

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