Running a macro for every sheet with a name listed in a range

coan29001

New Member
Joined
Jan 7, 2022
Messages
1
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hi, thanks to some help I got this code running. This code copiy/pastes new columns (not yet existing) from sheet "List" to sheet "Students".
VBA Code:
Dim wb As Workbook: Set wb = ThisWorkbook

With wb
Dim LastTemplateCol As Long: LastTemplateCol = .Worksheets("List").Cells(1, Columns.Count).End(xlToLeft).Column


      For i = 2 To LastTemplateCol

          Dim TempTask As String: TempTask = .Worksheets("List").Cells(1, i).Value
          Dim LastStudentCol As Long: LastStudentCol = .Worksheets("Students").Cells(1, Columns.Count).End(xlToLeft).Column
    
                For t = 2 To LastStudentCol
    
                Dim StudTask As String: StudTask = .Worksheets("Students").Cells(1, t).Value
                Dim Exists As Boolean: Exists = False
    
                  If TempTask = StudTask Then
                    Exists = True
                    GoTo taskloop:
                  Else
                    GoTo studloop:
                  End If
    
            studloop:
                Next t

        If Exists = False Then

          .Worksheets("List").Cells(1, i).Columns.EntireColumn.Copy
          .Worksheets("Students").Cells(1, LastStudentCol + 1).PasteSpecial
        End If

      taskloop:
      Next i

End With

This all works fine for sheet "Students". But I want to run this code for every sheet with a name that is listed in sheet "Sheetnames", range F2:F39.

How do I go about this? I'm not sure where to go... Many thanks in advance for helping me as a newbie in vba.
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Welcome to the Board!

Here is a little block of code to show you how to look through all those sheets:
VBA Code:
Sub WorksheetLoop()

    Dim cell As Range
    Dim ws As Worksheet
    
'   Loop through all sheets
    For Each cell In Sheets("Sheetnames").Range("F2:F39")
        If cell.Value <> "" Then
'           Set woorksheet object
            Set ws = Sheets(cell.Value)
'           Work with worksheet
            With ws
'               YOUR CODE HERE
            End With
        End If
    Next cell

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,552
Members
449,088
Latest member
davidcom

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