Hello all, first time poster here so please bear with me.
I have a project where I am trying to write code to be used as a tamplate across many projects. It has 2 sheets (to start). The "Master" sheet, which is where all the information is initially inserted, and the "Hidden" sheet, which is the template the applicable information from the Master sheet needs to be moved too.
Here is what I am trying to do from a fairly high level.
1. User files in information into master sheet.
2. User presses command button
3. VBA searches column B starting at row 14 (running until an empty cell) looking for operation names. For each operation name (many will repeat), create 1 new worksheet named with that operation name (cannot have repeats).
4. Say a new worksheet is named "Operation 1" and on the Master sheet, there are 4 lines relating to "operation 1". From this point, I want the Rows associated with having the condition of "Operation 1" being the operation name, to be copied and pasted into the template ("Hidden") starting at B14 and working down as needed.
The following is what I have so far and I'm having issues with 2 things.
1. I can't get code to skip past repeating values in column B. The first 2 cells are "Op 005" and it makes the first template, but stops from that point with an error for naming the tab (because it is repeating)
2. I'm trying to figure out how to have it copy and paste the rows from the master sheet to the applicable NewSheet (based on operation name).
I am by no means fluent in VBA, I can work through it, but am struggling right now. Any help is truly appreciated! Also, I found most of the code I am using on this site (not sure if I am supposed to show references?? https://stackoverflow.com/questions...rom-a-list-in-a-master-tab-and-populate-a-cel)
Thanks!!
------------------------------------------------------------------------------------------------------------------------------------------------------------
I have a project where I am trying to write code to be used as a tamplate across many projects. It has 2 sheets (to start). The "Master" sheet, which is where all the information is initially inserted, and the "Hidden" sheet, which is the template the applicable information from the Master sheet needs to be moved too.
Here is what I am trying to do from a fairly high level.
1. User files in information into master sheet.
2. User presses command button
3. VBA searches column B starting at row 14 (running until an empty cell) looking for operation names. For each operation name (many will repeat), create 1 new worksheet named with that operation name (cannot have repeats).
4. Say a new worksheet is named "Operation 1" and on the Master sheet, there are 4 lines relating to "operation 1". From this point, I want the Rows associated with having the condition of "Operation 1" being the operation name, to be copied and pasted into the template ("Hidden") starting at B14 and working down as needed.
The following is what I have so far and I'm having issues with 2 things.
1. I can't get code to skip past repeating values in column B. The first 2 cells are "Op 005" and it makes the first template, but stops from that point with an error for naming the tab (because it is repeating)
2. I'm trying to figure out how to have it copy and paste the rows from the master sheet to the applicable NewSheet (based on operation name).
I am by no means fluent in VBA, I can work through it, but am struggling right now. Any help is truly appreciated! Also, I found most of the code I am using on this site (not sure if I am supposed to show references?? https://stackoverflow.com/questions...rom-a-list-in-a-master-tab-and-populate-a-cel)
Thanks!!
------------------------------------------------------------------------------------------------------------------------------------------------------------
Code:
Private Sub CommandButton1_Click()
Dim masterSheet As Worksheet
Dim hiddenSheet As Worksheet
Dim NewSheet As Worksheet
Dim myBook As Workbook
Dim lastRow As Long
Dim i As Long
Dim namesColumn
'Define your workbook - here set as the active workbook, assuming it contains masterSheet and hiddenSheet
Set myBook = ActiveWorkbook
'Define your worksheets - The sheets are named "Master" and "Hidden" respectively
Set masterSheet = myBook.Worksheets("Master")
Set hiddenSheet = myBook.Worksheets("Hidden")
'Names are on the Master sheet in column B (2)
namesColumn = 2
'Find the last row of the sheets list
lastRow = masterSheet.Cells(masterSheet.Rows.Count, namesColumn).End(xlUp).Row
'Cycle through the list - Assuming the list starts in column "A" from the 2nd row
For i = 14 To lastRow
With myBook
'New sheet
Set NewSheet = .Worksheets.Add(After:=.Worksheets("Master"))
End With
'Find name of the tab and naming the tab
tabname = masterSheet.Cells(i, namesColumn)
NewSheet.Name = tabname
'Copy from hidden template - You can choose the ranges if predefined or use .Cells(r,c) to do something fancier
hiddenSheet.Range("A1:L62").Copy _
Destination:=NewSheet.Range("A1:L62")
'Paste in the new tab
NewSheet.Cells(1, 1).Value = tabname
'Copy row information from operation to tab with same name on master sheet
'If masterSheet.Cells("Bi") = NewSheet.Name Then
'masterSheet.Range("Bi:Li").Copy _
Destination:=NewSheet
Next i
End Sub
Last edited by a moderator: