Hi Harry,
I tried to do what you asked for. I hope I understood your request. See the attached file and the code excerpts below. Items in red in the code excerpts highlight major points.
I made wholesale changes to the existing code in module ModFormSheetGeneration and added UserForm0 and UserForm2. There were also small changes to the UserForm code.
You should be able to add to the framework I created to customize to your needs. Everything could have been done in one UserForm, but the method I chose is probably easier for you to follow and maintain.
The UserForms are as follows:
a. UserForm0 (optional) opens the other UserForms as the User requires.
b. UserForm1 is for 'AF21'.
c. UserForm2 is for 'AF22'
d. You can add other UserForms as you require.
UserForm1 code excerpts:
Private Sub CommandButton1_Click()
Call CreateCloneSheet("AF21", "Arrival", "From UserForm1")
End Sub
Private Sub CommandButton2_Click()
Call CreateCloneSheet("AF21", "Depart", "From UserForm1")
End Sub
Private Sub CommandButton3_Click()
Call CreateCloneSheet("AF21", "Progress", "From UserForm1")
End Sub
Private Sub CommandButton4_Click()
Call CreateCloneSheet("AF21", "Noon", "From UserForm1")
End Sub
Private Sub CommandButton5_Click()
Call CreateCloneSheet("AF21", "PE", "From UserForm1")
End Sub
Private Sub CommandButton6_Click()
Call CreateCloneSheet("AF21", "Bunker", "From UserForm1")
End Sub
Excerpts from module ModFormSheetGeneration:
Option Explicit
'Flag to determine if UserForm0 must be opened when another UserForm closes
Public bNeedUserForm0 As Boolean
Sub DisplayUserForm0()
'NOTE: Due to the nature of UserForm code, if UserForm0 is used all
' UserForms must be opened as vbModal
'Set the Flag to Open UserForm0 when UserForm1 or UserForm2 Closes
'The Flag is cleared when the 'x' in this UserForm is selected
bNeedUserForm0 = True
UserForm0.Show vbModal 'This locks out all Worksheet Resources
End Sub
Sub DisplayUserForm1()
UserForm1.Show vbModal 'This locks out all Worksheet Resources
'UserForm1.Show vbModeless 'This enables access to Worksheet Resources - useful during debugging
'Display UserForm0 after this UserForm closes if needed
If bNeedUserForm0 = True Then
Call DisplayUserForm0
End If
End Sub
Sub DisplayUserForm2()
UserForm2.Show vbModal 'This locks out all Worksheet Resources
'UserForm1.Show vbModeless 'This enables access to Worksheet Resources - useful during debugging
'Display UserForm0 after this UserForm closes if needed
If bNeedUserForm0 = True Then
Call DisplayUserForm0
End If
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Cell 'AF21 Rectangles
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub CreatePortArrivalSheet()
Call CreateCloneSheet("AF21", "Arrival")
End Sub
Sub CreatePortDepartureSheet()
Call CreateCloneSheet("AF21", "Depart")
End Sub
Sub CreatePortProgressSheet()
Call CreateCloneSheet("AF21", "Progress")
End Sub
Sub CreateNoonSheet()
Call CreateCloneSheet("AF21", "Noon")
End Sub
Sub CreatePortEvaluationSheet()
Call CreateCloneSheet("AF21", "PE")
End Sub
Sub CreateBunkerEvaluationSheet()
Call CreateCloneSheet("AF21", "Bunker")
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'Cell 'AF22 Rectangles
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub CreateABCSheet()
Call CreateCloneSheet("AF22", "ABC")
End Sub
Sub CreateDEFSheet()
Call CreateCloneSheet("AF22", "DEF")
End Sub
Sub CreateCloneSheet(sControlCellAddress As String, sSheetDescription As String, Optional sSourceOfRequest As String)
Dim wks As Worksheet
Dim iCount As Long
Dim iReply As Long
Dim iSourceSheetOriginalVisibility As Long 'This must be a type 'long' and not a type 'boolean'
Dim bIllegalCharacterInSheetName As Boolean
Dim bNeedMore As Boolean
Dim sBaseNewSheetName As String
Dim sDestinationSheetName As String
Dim sDestinationSheetPreambleName As String
Dim sDestinationSheetDescription As String
Dim sMasterSheetCloneName As String
Dim sSheetName As String
Dim sSourceSheetName As String
Dim sValueFromControlCell As String
If sControlCellAddress = "AF21" Then
'Get the Source Sheet Name
Select Case sSheetDescription
Case "Arrival"
sSourceSheetName = "ArrivalMaster"
sDestinationSheetDescription = "Port Arrival Report"
Case "Depart"
sSourceSheetName = "DepartMaster"
sDestinationSheetDescription = "Port Departure Report"
Case "Progress"
sSourceSheetName = "ProgressMaster"
sDestinationSheetDescription = "Port Progress Report"
Case "Noon"
sSourceSheetName = "NoonMaster"
sDestinationSheetDescription = "Noon Report"
Case "PE"
sSourceSheetName = "PEMaster"
sDestinationSheetDescription = "Port Evaluation Report"
Case "Bunker"
sSourceSheetName = "BunkerMaster"
sDestinationSheetDescription = "Bunker Evaluation Report"
End Select
End If
If sControlCellAddress = "AF22" Then
'Get the Source Sheet Name
Select Case sSheetDescription
Case "ABC"
sSourceSheetName = "ArrivalMaster"
sDestinationSheetDescription = "Port Arrival Report"
Case "DEF"
sSourceSheetName = "DepartMaster"
sDestinationSheetDescription = "Port Departure Report"
Case "Progress"
sSourceSheetName = "ProgressMaster"
sDestinationSheetDescription = "Port Progress Report"
Case "Noon"
sSourceSheetName = "NoonMaster"
sDestinationSheetDescription = "Noon Report"
Case "PE"
sSourceSheetName = "PEMaster"
sDestinationSheetDescription = "Port Evaluation Report"
Case "Bunker"
sSourceSheetName = "BunkerMaster"
sDestinationSheetDescription = "Bunker Evaluation Report"
End Select
End If
'Set the value of NEED_TO_DEBUG_PROGRAMMING_ERROR to 'True' to debug this routine
'Set the value of NEED_TO_DEBUG_PROGRAMMING_ERROR to 'False' for normal operation
#Const NEED_TO_DEBUG_PROGRAMMING_ERROR = False
#If NEED_TO_DEBUG_PROGRAMMING_ERROR = True Then
Debug.Assert False
#End If
If Len(sSourceSheetName) = 0 Then
MsgBox "NOTHING DONE. Programming Error input into Sub CreateCloneSheet()." & vbCrLf & _
"Check the value of the 'Control Cell Address' (e.g. 'AF21') or " & vbCrLf & _
"Check the value of the 'Sheet Description' (e.g. 'Departure' or 'Noon')."
Exit Sub
End If
'Verify that the User wants to create the New Sheet
iReply = MsgBox(Buttons:=vbYesNo, _
Title:= _
"Create New Sheet Selection", _
Prompt:= _
"Select 'Yes' to Create a New '" & sDestinationSheetDescription & "' Sheet." & vbCrLf & _
"Select 'No' to do NOTHING.")
If iReply = vbNo Then
MsgBox "NOTHING DONE per User request."
Exit Sub
End If
'Get the value from the Control Cell (e.g. 'AF21')
sValueFromControlCell = ActiveSheet.Range(sControlCellAddress).Value
'Create the Destination Sheet Name
'Limit the Sheet Preamble Name to 4 characters
sDestinationSheetPreambleName = sSheetDescription
If Len(sDestinationSheetPreambleName) > 4 Then
sDestinationSheetPreambleName = Left(sDestinationSheetPreambleName, 4)
End If
sBaseNewSheetName = sDestinationSheetPreambleName & "_" & sValueFromControlCell & "_" & Format(Now(), "mmmdd")
'Loop until a UNIQUE 'New Sheet Name' has been found
'or TERMINATE if an error occurs in the 'New Sheet Name'
bNeedMore = True
While bNeedMore = True
'Increment the 'Duplicate' Counter
'Create the proposed 'New Sheet Name'
iCount = iCount + 1
If iCount = 1 Then
sDestinationSheetName = sBaseNewSheetName
Else
sDestinationSheetName = sBaseNewSheetName & "(" & iCount & ")"
End If
'Terminate if the Count exceeds an arbitrary limit
If iCount > 99 Then
MsgBox "NOTHING DONE." & vbCrLf & _
"The New 'Sheet Name' exceeds the ARBITRARY duplicate limitation of 99." & vbCrLf & _
"New Sheet Name: '" & sDestinationSheetName & "'"
GoTo MY_EXIT
End If
'Terminate if the New 'Sheet Name' exceeds the 31 character name limitation
'NOTE: No checking is done for illegal characters in the name
If Len(sDestinationSheetName) > 31 Then
MsgBox "NOTHING DONE." & vbCrLf & _
"The New 'Sheet Name' exceeds the Excel 31 character limitation." & vbCrLf & _
"New Sheet Name: '" & sDestinationSheetName & "'" & vbCrLf & _
"Number of characters: " & Len(sDestinationSheetName)
GoTo MY_EXIT
End If
'Terminate if the 'New Sheet Name' contains illegal characters
bIllegalCharacterInSheetName = DoesSheetNameContainIllegalCharacters(sDestinationSheetName)
If bIllegalCharacterInSheetName = True Then
MsgBox "NOTHING DONE." & vbCrLf & _
"The New 'Sheet Name' has an ILLEGAL Character." & vbCrLf & _
"Illegal Characters include: : \ / ? * [ ]" & vbCrLf & _
"New Sheet Name: '" & sDestinationSheetName & "'"
GoTo MY_EXIT
End If
'Exit the Loop if the 'Sheet Name' DOES NOT EXIST
'It is now OK to create the new Sheet
If LjmSheetExists(sDestinationSheetName) = False Then
bNeedMore = False
End If
Wend
'Delete all Unauthorized Master Sheet Clones (e.g. 'PEMaster (2) etc) which should not exist
Application.DisplayAlerts = False 'Inhibit 'Do you really want to delete ... message'
For Each wks In ThisWorkbook.Worksheets
sSheetName = wks.Name
If sSheetName Like "Master (*" Then
wks.Visible = xlSheetHidden
wks.Delete
End If
Next wks
Application.DisplayAlerts = True
'Unhide a VeryHidden 'Source Sheet' if Needed
iSourceSheetOriginalVisibility = ThisWorkbook.Worksheets(sSourceSheetName).Visible
If iSourceSheetOriginalVisibility = xlSheetVeryHidden Then
ThisWorkbook.Worksheets(sSourceSheetName).Visible = xlSheetHidden
End If
'Make a copy of the 'Source Sheet' (as the Last Sheet in the Workbook)
ThisWorkbook.Worksheets(sSourceSheetName).Copy After:=ThisWorkbook.Sheets(Sheets.Count)
'Change the name of the 'New' Sheet
'Make the new Sheet visible
sMasterSheetCloneName = sSourceSheetName & " (2)"
Set wks = Sheets(sMasterSheetCloneName)
wks.Name = sDestinationSheetName
wks.Visible = xlSheetVisible
'Hide a VeryHidden 'Sheet2' if it was originally 'Very Hidden'
If iSourceSheetOriginalVisibility = xlSheetVeryHidden Then
ThisWorkbook.Worksheets(sSourceSheetName).Visible = xlSheetVeryHidden
End If
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'The following lines must be modified each time a new UserForm is added
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'If the request came from the 'ListBox', then update the ListBox contents
If sSourceOfRequest = "From UserForm1" Then
Call PopulateUserForm1ListBox1
ElseIf sSourceOfRequest = "From UserForm2" Then
Call PopulateUserForm2ListBox1
End If
'Turn on Screen Updating
Application.ScreenUpdating = True
MsgBox "New Sheet SUCCESSFULLY created." & vbCrLf & _
"New Sheet Name: '" & sDestinationSheetName & "'"
MY_EXIT:
'Turn on Screen Updating
Application.ScreenUpdating = True
'Put the Focus on 'Sheet1'
Sheets("Sheet1").Select
'Clear object pointer
Set wks = Nothing
End Sub
Sub PopulateUserForm0ListBox1()
'This clears the UserForm 'ListBox', and then populates the 'ListBox'
'with all Sheet Names that contain an Underscore
Dim ws As Worksheet
Dim sSheetName As String
'Clear the contents of the ListBox
UserForm0.ListBox1.Clear
'Populate the ListBox
For Each ws In ThisWorkbook.Worksheets
sSheetName = ws.Name
If sSheetName Like "*_*" Then
UserForm0.ListBox1.AddItem sSheetName
End If
Next ws
End Sub
Sub PopulateUserForm1ListBox1()
'This clears the UserForm 'ListBox', and then populates the 'ListBox'
'with all Sheet Names that contain an Underscore
Dim ws As Worksheet
Dim sSheetName As String
'Clear the contents of the ListBox
UserForm1.ListBox1.Clear
'Populate the ListBox
For Each ws In ThisWorkbook.Worksheets
sSheetName = ws.Name
If sSheetName Like "*_*" Then
UserForm1.ListBox1.AddItem sSheetName
End If
Next ws
End Sub
Sub PopulateUserForm2ListBox1()
'This clears the UserForm 'ListBox', and then populates the 'ListBox'
'with all Sheet Names that contain an Underscore
Dim ws As Worksheet
Dim sSheetName As String
'Clear the contents of the ListBox
UserForm2.ListBox1.Clear
'Populate the ListBox
For Each ws In ThisWorkbook.Worksheets
sSheetName = ws.Name
If sSheetName Like "*_*" Then
UserForm2.ListBox1.AddItem sSheetName
End If
Next ws
End Sub
Lewis
Bookmarks