Creating a patch in vba

Feester

New Member
Joined
May 21, 2007
Messages
26
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
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".

Forum statistics

Threads
1,214,516
Messages
6,119,980
Members
448,934
Latest member
audette89

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top