Looking for an easier way to write this For Each loop. Perhaps with a case statement?

mikenelena

Board Regular
Joined
Mar 5, 2018
Messages
139
Office Version
  1. 365
Platform
  1. Windows
We have two groups of employees, and they need different handling for payroll. To loop through 1 type of employee, I simply excluded the others in my For Each statement, because there are only a few. For that second group though, it is inefficient and unwieldy to exclude an entire list. (Every time an employee comes or goes, I'd have to change the code.) Below is what I'm looking to accomplish, but I'm sure there is a better way. My gut says maybe a case statement would do it, but I'm not sure how to go about it. Can anyone help me with this?

Thanks so much!

Code:
Sub LoopGetAdminPayrollData()


Dim ws As Worksheet


Application.ScreenUpdating = False
For Each ws In Worksheets


    If ws.Name <> "Main" And ws.Name <> "Guide" And ws.Name <> "Sum" And ws.Name <> "Abb" And ws.Name <> "Bar" And ws.Name <> "Bur" _
    And ws.Name <> "Car" And ws.Name <> "Duc" And ws.Name <> "Jef" And ws.Name <> "Fit" And ws.Name <> "Gil" And ws.Name <> "Hec" _
    And ws.Name <> "JP" And ws.Name <> "Jud" And ws.Name <> "Nap" And ws.Name <> "Pow" And ws.Name <> "BR" And ws.Name <> "JR" _
    And ws.Name <> "Ree" And ws.Name <> "TJ" And ws.Name <> "Ben" And ws.Name <> "Tet" And ws.Name <> "Var" And ws.Name <> "Bill" _
    And ws.Name <> "Yoh" And ws.Name <> "Dan" Then


Worksheets(ActiveSheet.Index + 1).Select
Call GetAdminData


    End If


Next ws
    
End Sub
 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
I would recommend creating a two-column table that lists each employee name and payroll code/type, and then have your code look up the value and then determine what to do.
 
Upvote 0

Forum statistics

Threads
1,215,373
Messages
6,124,549
Members
449,170
Latest member
Gkiller

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