Forum Discussion
Jn12345
Oct 03, 2024Brass Contributor
VBA LOOP to copy a hidden sheet n number of times and naming them based on rows
Hello All, Back here again with something that seems like it should be simple but I cannot get my head around it. I have a workbook where there is a table (unfortunately not a real table, and...
HansVogelaar
Oct 03, 2024MVP
Thanks. Try this version of the macro:
Sub Rows_to_New_Sheet()
Dim A As Range
Dim W_C As Worksheet
Dim W_S As Worksheet
Dim W_T As Worksheet
Dim LastRow As Long
Application.ScreenUpdating = False
' Template sheet
Set W_T = Worksheets("Sheet1")
W_T.Visible = xlSheetVisible
' Active sheet
Set W_C = ActiveSheet
LastRow = W_C.Cells.SpecialCells(xlCellTypeLastCell).Row
' Loop
For Each A In W_C.Range("D23:D" & LastRow)
If A.Value <> "" Then
' Essential: reset W_S to nothing
Set W_S = Nothing
On Error Resume Next
' Check whether sheet for Sleeve ID already exists
Set W_S = Worksheets(A.Value)
On Error GoTo 0
If W_S Is Nothing Then
' If not, copy the template sheet.
W_T.Copy Before:=Sheets("Photos")
Set W_S = Sheets("Photos").Previous
W_S.Name = A.Value
End If
End If
Next A
' Optional: hide the template sheet
W_T.Visible = xlSheetHidden
Application.ScreenUpdating = True
End Sub
Jn12345
Oct 03, 2024Brass Contributor
HansVogelaar Thanks! I was able to learn a lot from this. Unfortunately i dont have the time to take a full course but this definitely shows me a ton! thanks for the 'notes in the VBA!