Deactivate lines - Can the code be shortened

afal1221

New Member
Joined
Jul 10, 2023
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi!

I have inherited a workbook that is getting to long and hit the dreaded "Procedure too large"
Is there a simple way to shorten this code using a range even though it needs to look at 2 different worksheets to hide or unhide the row?

Here is an example of the code:

VBA Code:
Private Sub Worksheet_Deactivate()
    If Sheets("SPECS-P").Range("O4").Value = 0 Then
        Sheets("Proposal-P").Rows("142:145").EntireRow.Hidden = True
    ElseIf Sheets("SPECS-P").Range("O4").Value > 0 Then
        Sheets("Proposal-P").Rows("142:145").EntireRow.Hidden = False
    End If
   
        If Sheets("SPECS-P").Range("O5").Value = 0 Then
        Sheets("Proposal-P").Rows("146").EntireRow.Hidden = True
    ElseIf Sheets("SPECS-P").Range("O5").Value > 0 Then
        Sheets("Proposal-P").Rows("146").EntireRow.Hidden = False
    End If
   
     If Sheets("SPECS-P").Range("O6").Value = 0 Then
        Sheets("Proposal-P").Rows("147").EntireRow.Hidden = True
    ElseIf Sheets("SPECS-P").Range("O6").Value > 0 Then
        Sheets("Proposal-P").Rows("147").EntireRow.Hidden = False
    End If
   
    If Sheets("SPECS-P").Range("O7").Value = 0 Then
        Sheets("Proposal-P").Rows("148").EntireRow.Hidden = True
    ElseIf Sheets("SPECS-P").Range("O7").Value > 0 Then
        Sheets("Proposal-P").Rows("148").EntireRow.Hidden = False
    End If
   
    If Sheets("SPECS-P").Range("O8").Value = 0 Then
        Sheets("Proposal-P").Rows("149").EntireRow.Hidden = True
    ElseIf Sheets("SPECS-P").Range("O8").Value > 0 Then
        Sheets("Proposal-P").Rows("149").EntireRow.Hidden = False
    End If
   
     If Sheets("SPECS-P").Range("O9").Value = 0 Then
        Sheets("Proposal-P").Rows("150").EntireRow.Hidden = True
    ElseIf Sheets("SPECS-P").Range("O9").Value > 0 Then
        Sheets("Proposal-P").Rows("150").EntireRow.Hidden = False
    End If

    If Sheets("SPECS-P").Range("O10").Value = 0 Then
        Sheets("Proposal-P").Rows("151").EntireRow.Hidden = True
    ElseIf Sheets("SPECS-P").Range("O10").Value > 0 Then
        Sheets("Proposal-P").Rows("151").EntireRow.Hidden = False
    End If

(I have another 194 sets that need to do the same thing.

Any advise would be greatly appreciated!
Kind regards.
 
Last edited by a moderator:

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
I have inherited a workbook that is getting to long and hit the dreaded "Procedure too large"
Since I can't experiment on your data, I cannot say if it will get rid of that error, but there are certainly many ways to shorten your code.
One way (not tested):
VBA Code:
Private Sub Worksheet_Deactivate()
    Dim SpecWS As Worksheet, PropWS As Worksheet
    Dim SpecRange As Range, R As Range
    Dim RNum As Long
    
    Set SpecWS = Sheets("SPECS-P")
    Set PropWS = Sheets("Proposal-P")
    
    Set SpecRange = SpecWS.Range("O4:O202") '8+194 = 202 (i.e. "I have another 194 sets that need to do the same thing").
    RNum = 146 'starting value
    
    Application.ScreenUpdating = False
    For Each R In SpecRange
        If R.Row = 4 Then 'special case
            If R.Value = 0 Then
                PropWS.Rows("142:145").EntireRow.Hidden = True
            ElseIf R.Value > 0 Then
                PropWS.Rows("142:145").EntireRow.Hidden = False
            End If
        Else
            If R.Value = 0 Then
                PropWS.Rows(RNum).EntireRow.Hidden = True
            ElseIf R.Value > 0 Then
                PropWS.Rows(RNum).EntireRow.Hidden = False
            End If
            RNum = RNum + 1
        End If
    Next R
    Application.ScreenUpdating = True
End Sub


(Tip: For future posts , you should try to use code tags like I did above when posting your code. It makes it easier to read.)

 
Upvote 0
Solution
Without seeing all of the code, the following should give you an idea of an approach you can use:

VBA Code:
Private Sub Worksheet_Deactivate()
'
    Dim CellNumber      As Long
    Dim proposalSheet   As Worksheet,  specsSheet      As Worksheet
 
    Set specsSheet = Sheets("SPECS-P")
    Set proposalSheet = Sheets("Proposal-P")
'
    Application.ScreenUpdating = False
'
    For CellNumber = 4 To 10                                                                    ' <--- Set this to the range of cell numbers in the O column of 'SPECS-P'
        If CellNumber = 4 Then
            If specsSheet.Range("O" & CellNumber).Value = 0 Then
                proposalSheet.Rows(138 + CellNumber & ":" & 138 + CellNumber + 3).EntireRow.Hidden = True
            ElseIf specsSheet.Range("O" & CellNumber).Value > 0 Then
                proposalSheet.Rows(138 + CellNumber & ":" & 138 + CellNumber + 3).EntireRow.Hidden = False
            End If
        Else
            If specsSheet.Range("O" & CellNumber).Value = 0 Then
                proposalSheet.Rows(141 + CellNumber).EntireRow.Hidden = True
            ElseIf specsSheet.Range("O" & CellNumber).Value > 0 Then
                proposalSheet.Rows(141 + CellNumber).EntireRow.Hidden = False
            End If
        End If
    Next
'
    Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Assuming col O will either be 0 or >0 then another option is
VBA Code:
Private Sub Worksheet_Deactivate()
   Dim i As Long
   With Sheets("SPECS-P")
      Sheets("Proposal-P").Rows("142:145").EntireRow.Hidden = .Range("O4").Value = 0
      For i = 5 To 200
         Sheets("Proposal-P").Rows(i + 141).EntireRow.Hidden = .Range("O" & i).Value = 0
      Next i
   End With
End Sub
 
Upvote 0
One last thought: testing whether a row is hidden/unhidden is faster that setting a row to be hidden/unhidden. For 200 or so rows, it isn't going to matter, everything will be fast - but if you have several thousand rows that you are hiding/unhiding you probably want to put code in to first test the row's hidden property and only change the rows that need changing (along with turning off screen updating).
 
Upvote 0
Another approach that uses arrays so that we don't have to access the sheets so many times (ie. could/should be faster) would be:

VBA Code:
Sub Worksheet_DeactivateV3()
'
    Dim ArrayRow                        As Long, ProposalRow                            As Long
    Dim FullRangeOfRowsToBeHidden       As Range, FullRangeOfRowsToBeUnhidden           As Range
    Dim RangeRow                        As Range
    Dim RemainingRangeOfRowsToBeHidden  As Range, RemainingRangeOfRowsToBeUnhidden      As Range
    Dim SpecsColumn_O_Array             As Variant
    Dim proposalSheet                   As Worksheet
'
    Application.ScreenUpdating = False                                                      ' Turn ScreenUpdating off
    Application.Calculation = xlCalculationManual                                           ' Turn calculation mode off
'
    SpecsColumn_O_Array = Sheets("SPECS-P").Range("O4:O204").Value2                         ' <--- Set this to the range of cell numbers in Column 'O' of 'SPECS-P'
'
    Set proposalSheet = Sheets("Proposal-P")
'
    For ArrayRow = 1 To UBound(SpecsColumn_O_Array, 1)                                      ' Loop through the rows of SpecsColumn_O_Array
        ProposalRow = ArrayRow + 144                                                        '   Save the row offset for 'Proposal-P' into ProposalRow
'
'''''''''''''''''''''''''''''''''''''''''
' Handle the single Row Hiding/Unhiding '
'''''''''''''''''''''''''''''''''''''''''
        If ArrayRow <> 1 Then                                                               '   If ArrayRow is not the first row in SpecsColumn_O_Array then ...
            If SpecsColumn_O_Array(ArrayRow, 1) = 0 And _
                    Not IsEmpty(SpecsColumn_O_Array(ArrayRow, 1)) Then                      '       If the value in SpecsColumn_O_Array = 0 then ...
                If Not FullRangeOfRowsToBeHidden Is Nothing Then                            '           If we have already saved a row number to FullRangeOfRowsToBeHidden then ...
                    Set FullRangeOfRowsToBeHidden = Union(FullRangeOfRowsToBeHidden, _
                            proposalSheet.Rows(ProposalRow))                                '               Add the ProposalRow to the rows already in FullRangeOfRowsToBeHidden
                Else                                                                        '           Else ...
                    Set FullRangeOfRowsToBeHidden = proposalSheet.Rows(ProposalRow)         '               Save the ProposalRow to FullRangeOfRowsToBeHidden
                End If
            ElseIf SpecsColumn_O_Array(ArrayRow, 1) > 0 Then                                '       Else if the value in SpecsColumn_O_Array > 0 then ...
                If Not FullRangeOfRowsToBeUnhidden Is Nothing Then                          '           If we have already saved a row number to FullRangeOfRowsToBeUnhidden then ...
                    Set FullRangeOfRowsToBeUnhidden = Union(FullRangeOfRowsToBeUnhidden, _
                            proposalSheet.Rows(ProposalRow))                                '               Add the ProposalRow to the rows already in FullRangeOfRowsToBeUnhidden
                Else                                                                        '           Else ...
                    Set FullRangeOfRowsToBeUnhidden = proposalSheet.Rows(ProposalRow)       '               Save the ProposalRow to FullRangeOfRowsToBeUnhidden
                End If
            End If
'
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Handle 'SPECS-P' $O4 value where 4 rows will be hidden/unhidden '
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        Else                                                                                '   Else ...
            If SpecsColumn_O_Array(ArrayRow, 1) = 0 And _
                    Not IsEmpty(SpecsColumn_O_Array(ArrayRow, 1)) Then                      '       If the value in SpecsColumn_O_Array = 0 then ...
                If Not FullRangeOfRowsToBeHidden Is Nothing Then                            '           If we have already saved a row number to FullRangeOfRowsToBeHidden then ...
                    Set FullRangeOfRowsToBeHidden = Union(FullRangeOfRowsToBeHidden, _
                            proposalSheet.Rows("142:145"))                                  '               Add the ProposalRow to the rows already in FullRangeOfRowsToBeHidden
                Else                                                                        '           Else ...
                    Set FullRangeOfRowsToBeHidden = proposalSheet.Rows("142:145")           '               Save row range of 142:145 to FullRangeOfRowsToBeHidden
                End If
            ElseIf SpecsColumn_O_Array(ArrayRow, 1) > 0 Then                                '       Else if the value in SpecsColumn_O_Array > 0 then ...
                If Not FullRangeOfRowsToBeUnhidden Is Nothing Then                          '           If we have already saved a row number to FullRangeOfRowsToBeUnhidden then ...
                    Set FullRangeOfRowsToBeUnhidden = Union(FullRangeOfRowsToBeUnhidden, _
                            proposalSheet.Rows("142:145"))                                  '               Add the row range of 142:145 to the rows already in FullRangeOfRowsToBeUnhidden
                Else                                                                        '           Else ...
                    Set FullRangeOfRowsToBeUnhidden = proposalSheet.Rows("142:145")         '               Save row range of 142:145 to FullRangeOfRowsToBeUnhidden
                End If
            End If
        End If
    Next                                                                                    ' Loop back
'
''''''''''''''''''''''''''
' Smart Hide/Unhide Rows '
''''''''''''''''''''''''''
    If Not FullRangeOfRowsToBeHidden Is Nothing Then                                        ' If there are rows that should be hidden then ...
        On Error Resume Next                                                                '   In the case that there are no ranges of rows that need to be hidden, skip error
            Set RemainingRangeOfRowsToBeHidden = _
                    FullRangeOfRowsToBeHidden.SpecialCells(xlCellTypeVisible)               '       Get the range of visible rows that haven't been hidden yet
        On Error GoTo 0                                                                     '   Return error handling back over to Excel
'
        If Not RemainingRangeOfRowsToBeHidden Is Nothing Then _
                RemainingRangeOfRowsToBeHidden.EntireRow.Hidden = True                      '   If there are rows that still need to be hidden then hide those rows
    End If
'
    If Not FullRangeOfRowsToBeUnhidden Is Nothing Then                                      ' If there are rows that should be visible then ...
        For Each RangeRow In FullRangeOfRowsToBeUnhidden.Rows                               '   Loop through each row in FullRangeOfRowsToBeUnhidden
            If RangeRow.EntireRow.Hidden Then                                               '       If the row is currently hidden then ...
                If Not RemainingRangeOfRowsToBeUnhidden Is Nothing Then                     '           If we have already saved a row number to RemainingRangeOfRowsToBeUnhidden then ...
                    Set RemainingRangeOfRowsToBeUnhidden = _
                            Union(RemainingRangeOfRowsToBeUnhidden, RangeRow)               '               Add the RangeRow to the rows already in RemainingRangeOfRowsToBeUnhidden
                Else                                                                        '           Else ...
                    Set RemainingRangeOfRowsToBeUnhidden = RangeRow                         '               Save the RangeRow to RemainingRangeOfRowsToBeUnhidden
                End If
            End If
        Next                                                                                '   Loop back
'
        If Not RemainingRangeOfRowsToBeUnhidden Is Nothing Then _
                RemainingRangeOfRowsToBeUnhidden.EntireRow.Hidden = False                   '   If we have saved atleast 1 row number to RemainingRangeOfRowsToBeUnhidden then ...
'                                                                                           '           unhide those rows
    End If
'
    Application.Calculation = xlCalculationAutomatic                                        ' Turn calculation mode back on
    Application.ScreenUpdating = True                                                       ' Turn ScreenUpdating back on
End Sub

Hopefully somebody will check my 'math' on that to see if it still works properly.
 
Upvote 0
Since I can't experiment on your data, I cannot say if it will get rid of that error, but there are certainly many ways to shorten your code.
One way (not tested):
VBA Code:
Private Sub Worksheet_Deactivate()
    Dim SpecWS As Worksheet, PropWS As Worksheet
    Dim SpecRange As Range, R As Range
    Dim RNum As Long
  
    Set SpecWS = Sheets("SPECS-P")
    Set PropWS = Sheets("Proposal-P")
  
    Set SpecRange = SpecWS.Range("O4:O202") '8+194 = 202 (i.e. "I have another 194 sets that need to do the same thing").
    RNum = 146 'starting value
  
    Application.ScreenUpdating = False
    For Each R In SpecRange
        If R.Row = 4 Then 'special case
            If R.Value = 0 Then
                PropWS.Rows("142:145").EntireRow.Hidden = True
            ElseIf R.Value > 0 Then
                PropWS.Rows("142:145").EntireRow.Hidden = False
            End If
        Else
            If R.Value = 0 Then
                PropWS.Rows(RNum).EntireRow.Hidden = True
            ElseIf R.Value > 0 Then
                PropWS.Rows(RNum).EntireRow.Hidden = False
            End If
            RNum = RNum + 1
        End If
    Next R
    Application.ScreenUpdating = True
End Sub


(Tip: For future posts , you should try to use code tags like I did above when posting your code. It makes it easier to read.)


Thank for the help and insight on how to request help in the future. This worked for the most part except the +1 on the RNum - If I leave it as +1 then it would leave the row below 146 (147) as hidden. I would have thought the opposite would occur. Off to the next fix.
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,278
Members
452,902
Latest member
Knuddeluff

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