I have previously created a patch (with help from this forum) that allows you to choose a spreadsheet to open and then change details in the spreadsheet, save the changes and then close the spreadsheet.
I am trying to use a similar patch and I can't get it to work. I can get the spreadsheet to open but I can't get the change to take place. What I want it to do is open the spreadsheet and then go to the tab called Summary Form. When it is in that tab I need it to unprotect it. It then has to look at each cell between Column G19 and G40 and if there is a 0 in that cell then do nothing. If there is a number in there then I want it to unhide a tab. I am using a similar code from the original spreadsheet where it works fine but it doesn't seem to like being transported into this patch. Can anyone see a way for me to make this work?
Modified Code and original code:
Original Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Set sheet tab to hide
Dim i As Integer
Dim Sheetname As String
Dim Sheetname2 As String
Dim SheetLen As Integer
Dim SheetLen2 As Integer
If Intersect(Target, Range("$G$19:$G$39")) Is Nothing Then Exit Sub
Sheetname = Target.Offset(0, 9).Value
SheetLen = Len(Sheetname)
Sheetname2 = Target.Offset(0, 10).Value
SheetLen2 = Len(Sheetname2)
For i = 3 To Worksheets.Count
If Left(Sheets(i).Name, SheetLen) = Sheetname Then
Sheets(i).Visible = False
ElseIf Left(Sheets(i).Name, SheetLen2) = Sheetname2 Then
Sheets(i).Visible = True
End If
Next i
End Sub
Modified Code:
Private Sub Workbook_Open()
' Get the appropriate file information
Dim sFileName As String
sFileName = Application.GetOpenFilename
' They have cancelled.
If sFileName = "False" Then Exit Sub
Workbooks.Open Filename:=sFileName
' Unprotects the workbook
ActiveWorkbook.Sheets("Summary Form").Unprotect ("test1")
'Set sheet tab to hide
Dim i As Integer
Dim a As Integer
Dim Sheetname As String
Dim Sheetname2 As String
Dim SheetLen As Integer
Dim SheetLen2 As Integer
For a = 19 To 40
If Intersect(Target, Cells(a, 7)) Is Nothing Then
Exit For
End If
Next a
Sheetname = Target.Offset(0, 9).Value
SheetLen = Len(Sheetname)
Sheetname2 = Target.Offset(0, 10).Value
SheetLen2 = Len(Sheetname2)
For i = 3 To Worksheets.Count
If Left(Sheets(i).Name, SheetLen) = Sheetname Then
Sheets(i).Visible = False
ElseIf Left(Sheets(i).Name, SheetLen2) = Sheetname2 Then
Sheets(i).Visible = True
End If
Next i
ActiveWorkbook.Sheets("Summary Form").Protect ("test1")
' Save & Close the updated workbook
ActiveWorkbook.Close True
' Close the update workbook without saving
ActiveWorkbook.Close False
End Sub
Any help would be greatly appreciated.
Thanks
I am trying to use a similar patch and I can't get it to work. I can get the spreadsheet to open but I can't get the change to take place. What I want it to do is open the spreadsheet and then go to the tab called Summary Form. When it is in that tab I need it to unprotect it. It then has to look at each cell between Column G19 and G40 and if there is a 0 in that cell then do nothing. If there is a number in there then I want it to unhide a tab. I am using a similar code from the original spreadsheet where it works fine but it doesn't seem to like being transported into this patch. Can anyone see a way for me to make this work?
Modified Code and original code:
Original Code:
Private Sub Worksheet_Change(ByVal Target As Range)
'Set sheet tab to hide
Dim i As Integer
Dim Sheetname As String
Dim Sheetname2 As String
Dim SheetLen As Integer
Dim SheetLen2 As Integer
If Intersect(Target, Range("$G$19:$G$39")) Is Nothing Then Exit Sub
Sheetname = Target.Offset(0, 9).Value
SheetLen = Len(Sheetname)
Sheetname2 = Target.Offset(0, 10).Value
SheetLen2 = Len(Sheetname2)
For i = 3 To Worksheets.Count
If Left(Sheets(i).Name, SheetLen) = Sheetname Then
Sheets(i).Visible = False
ElseIf Left(Sheets(i).Name, SheetLen2) = Sheetname2 Then
Sheets(i).Visible = True
End If
Next i
End Sub
Modified Code:
Private Sub Workbook_Open()
' Get the appropriate file information
Dim sFileName As String
sFileName = Application.GetOpenFilename
' They have cancelled.
If sFileName = "False" Then Exit Sub
Workbooks.Open Filename:=sFileName
' Unprotects the workbook
ActiveWorkbook.Sheets("Summary Form").Unprotect ("test1")
'Set sheet tab to hide
Dim i As Integer
Dim a As Integer
Dim Sheetname As String
Dim Sheetname2 As String
Dim SheetLen As Integer
Dim SheetLen2 As Integer
For a = 19 To 40
If Intersect(Target, Cells(a, 7)) Is Nothing Then
Exit For
End If
Next a
Sheetname = Target.Offset(0, 9).Value
SheetLen = Len(Sheetname)
Sheetname2 = Target.Offset(0, 10).Value
SheetLen2 = Len(Sheetname2)
For i = 3 To Worksheets.Count
If Left(Sheets(i).Name, SheetLen) = Sheetname Then
Sheets(i).Visible = False
ElseIf Left(Sheets(i).Name, SheetLen2) = Sheetname2 Then
Sheets(i).Visible = True
End If
Next i
ActiveWorkbook.Sheets("Summary Form").Protect ("test1")
' Save & Close the updated workbook
ActiveWorkbook.Close True
' Close the update workbook without saving
ActiveWorkbook.Close False
End Sub
Any help would be greatly appreciated.
Thanks