Forum Discussion
VBA LOOP to copy a hidden sheet n number of times and naming them based on rows
The VBA project in your workbook is locked for viewing.
- Jn12345Oct 03, 2024Brass Contributor
Hey Hans,
Thanks for the quick reply. Please see attached. The button I will use will obviously be smaller but for now its just a huge button that converts rows to sheets. One thing i notice is it you click it right away it does an alright job of adding the sheets but puts them in the wrong spot. (would be nice to have them appear before the photos page but thats me being a little picky) I do notice that it spells/ formats the name of the sheet wrong. slv-001 instead of SLV-001 and then i obviously dont know how to make the sheets incorporate the info on sheet1. Ideally i think it would work something like. for how many rows doing an xl down count counts, unhide sheet1 then copy it count-1 times and then rename. I might be way off the mark but i am new to this stuff so bare with me! thanks!!
- HansVogelaarOct 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
- Jn12345Oct 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!