book1.xls | ||||||
---|---|---|---|---|---|---|
A | B | C | D | |||
16 | DoubleClickForNewPCO1Sheet | PCO1 | ||||
17 | PCO1rev1 | |||||
18 | PCO1rev2 | |||||
19 | PCO1rev3 | |||||
20 | PCO1rev4 | |||||
21 | PCO1rev5 | |||||
22 | DoubleClickForNewPCO2Sheet | PCO2 | ||||
23 | PCO2rev1 | |||||
24 | PCO2rev2 | |||||
25 | PCO2rev3 | |||||
26 | PCO2rev4 | |||||
27 | PCO2rev5 | |||||
28 | DoubleClickForNewPCO3Sheet | PCO3 | ||||
29 | PCO3rev1 | |||||
30 | PCO3rev2 | |||||
31 | PCO3rev3 | |||||
32 | PCO3rev4 | |||||
33 | PCO3rev5 | |||||
Sheet1 |
Private Sub Worksheet_BeforeDoubleClick(ByVal Target As Range, Cancel As Boolean)
'-------------------------------------------------------------------------------
'Purpose:
'When a cell in column A is double-clicked,
' to add a sheet based on the cell value to the right in Column B.
'
'Sub will first see if the main PCO sheet exists.
' If not, it creates it.
' If so, it looks for the next revision sheets until it
' finds the next revision sheet to be made.
'
'The double-click event is triggered only by
' clicking in a cell in column A, after row 15 and before row 1000
' and there should be no cell to the right with the words "rev" in it.
'
'-------------------------------------------------------------------------------
If Not Intersect(Target, Range("A16:A1000")) Is Nothing Then
'//second check - only cells to the left of main PCO numbers, not revisions
If Not InStr(1, Target.Offset(0, 1).Value, "rev", vbTextCompare) Then
Cancel = True
Call CreateNextPCOSheet(Target)
Me.Activate
Target.Select
End If
End If
End Sub
'-----------------------------------------
Sub CreateNextPCOSheet(ByRef rngClicked As Range)
Dim ws As Worksheet
Dim rng As Range
Dim a
Dim i As Long
Dim blnSheetMade As Boolean
'//values for PCO and 5 revisions - sheet names
Set rng = rngClicked.Offset(0, 1).Resize(6, 1)
If WorksheetFunction.CountA(rng)<> 6 Then
MsgBox "An error occurred creating the next PCO sheet: Excel cannot find the sheet names."
Exit Sub
End If
a = rng.Value
'//check if these sheets exist in turn
For i = 1 To UBound(a)
If Not MySheetExists(a(i, 1)) Then
'//Sheet doesn't exist. Create one.
Worksheets("Template").Copy After:=Worksheets(Worksheets.Count)
ActiveSheet.Name = a(i, 1)
'//unhide row for revision
rng.Cells(i).EntireRow.Hidden = False
blnSheetMade = True
Exit For
End If
Next i
'//Oops. Already have all 5 revisions
If blnSheetMade = False Then
MsgBox "Only 5 revisions sheets are available."
End If
End Sub
'-----------------------------------------
Private Function MySheetExists(ByVal strSheetName As String)
Dim s As String
On Error Resume Next
s = ThisWorkbook.Worksheets(strSheetName).Name
If Err Then
MySheetExists = False
Else
MySheetExists = True
End If
End Function