简体   繁体   中英

VBA Code - Skip some parts if some others are excecuted

I have written this piece of code but have gutted out the additional filler in between. Depending on time points selected, it will hide the appropriate rows.

Container 1 will always be filled, however, if another container is not selected, I want it to hide all the remaining rows without processing the rest of the code. So if Container 1 and 2 are selected, it will run the code for these without running the rest of the code.

Rewriting this as loop would be incredibly complex as there are so many possible timepoints, it is more an issue of skipping the code that is not relevant. Almost like a goto line or something? I don't know!

Is there any other way to make this code run more efficiently than temporarily disabling the DisplayPageBreaks, ScreenUpdating and Enable Events? There is no calculation performed on the page, only row hides.

For Example, if Q26 is blank (No container 2) I want it to go to the end of the code without processing anything else but how I have written it, it still process the rest of the code.

Thanks for your help

If Worksheets("StabDataCapture").Range("q26").Value = "" Then Worksheets("Template").Rows("142:1048576").EntireRow.Hidden = True Else

Thank you for you help!

Sub Containers()

Dim xPctComp As Integer

Application.StatusBar = "Container 1: " & _
  Format(xPctComp, "##0%")

ActiveSheet.DisplayPageBreaks = False
Application.EnableEvents = False
Application.ScreenUpdating = False



'CONTAINER 1 ROW HIDES

'@@@@@@@@@@@@@@@@@@@@@@@@@@@ 60°C @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

'Show/Hide 1@60

    If Worksheets("StabDataCapture").Range("B33").Value = "" Then
        Worksheets("Template").Rows("8:8").EntireRow.Hidden = True
    End If

Application.StatusBar = "Container 2: " & _
  Format(xPctComp, "##25%")

If Worksheets("StabDataCapture").Range("q26").Value = "" Then Worksheets("Template").Rows("142:1048576").EntireRow.Hidden = True Else

'CONTAINER 2 ROW HIDES

'@@@@@@@@@@@@@@@@@@@@@@@@@@@ 60°C @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

'Show/Hide 1@60

    If Worksheets("StabDataCapture").Range("P33").Value = "" Then
                Worksheets("Template").Rows("146:146").EntireRow.Hidden = True
    End If

Application.StatusBar = "Container 3: " & _
  Format(xPctComp, "##50%")

'CONTAINER 3 ROW HIDES

 If Worksheets("StabDataCapture").Range("c91").Value = "" Then Worksheets("Template").Rows("280:1048576").EntireRow.Hidden = True Else

'@@@@@@@@@@@@@@@@@@@@@@@@@@@ 60°C @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

'Show/Hide 1@60

    If Worksheets("StabDataCapture").Range("B98").Value = "" Then
        Worksheets("Template").Rows("284:284").EntireRow.Hidden = True
    End If
Application.StatusBar = "Container 4: " & _
  Format(xPctComp, "##75%")

 If Worksheets("StabDataCapture").Range("q91").Value = "" Then Worksheets("Template").Rows("418:1048576").EntireRow.Hidden = True Else


'@@@@@@@@@@@@@@@@@@@@@@@@@@@ 60°C @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

'Show/Hide 1@60

    If Worksheets("StabDataCapture").Range("P98").Value = "" Then
                Worksheets("Template").Rows("422:422").EntireRow.Hidden = True
    End If

Application.EnableEvents = True
Application.ScreenUpdating = True
Application.StatusBar = ""

End Sub

You need a routine to reactivate the screen and events,

Sub Restart_Screen()
With Application
    .EnableEvents = True
    .ScreenUpdating = True
    .StatusBar = vbNullString
End With
End Sub

Using Exit Sub , it could look like this :

Sub test_vividillusion()
Dim xPctComp As Integer
Dim wS As Worksheet
Dim wsT As Worksheet
Set wS = Sheets("StabDataCapture")
Set wsT = Sheets("Template")

With Application
    .EnableEvents = False
    .ScreenUpdating = False
    .StatusBar = "Container 1: " & Format(xPctComp, "##0%")
End With
ActiveSheet.DisplayPageBreaks = False

'CONTAINER 1 ROW HIDES
'@@@@@@@@@@@@@@@@@@@@@@@@@@@ 60°C @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Show/Hide 1@60
If wS.Range("B33").Value = vbNullString Then wsT.Rows("8:8").EntireRow.Hidden = True
Application.StatusBar = "Container 2: " & Format(xPctComp, "##25%")

If wS.Range("q26").Value = vbNullString Then
    wsT.Rows("142:1048576").EntireRow.Hidden = True
    Restart_Screen
    Exit Sub
Else
End If

'CONTAINER 2 ROW HIDES
'@@@@@@@@@@@@@@@@@@@@@@@@@@@ 60°C @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Show/Hide 1@60
If wS.Range("P33").Value = vbNullString Then wsT.Rows("146:146").EntireRow.Hidden = True
Application.StatusBar = "Container 3: " & Format(xPctComp, "##50%")

If wS.Range("c91").Value = vbNullString Then
    wsT.Rows("280:1048576").EntireRow.Hidden = True
    Restart_Screen
    Exit Sub
Else
End If
'CONTAINER 3 ROW HIDES
'@@@@@@@@@@@@@@@@@@@@@@@@@@@ 60°C @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Show/Hide 1@60
If wS.Range("B98").Value = vbNullString Then wsT.Rows("284:284").EntireRow.Hidden = True
Application.StatusBar = "Container 4: " & Format(xPctComp, "##75%")

If wS.Range("q91").Value = vbNullString Then
    wsT.Rows("418:1048576").EntireRow.Hidden = True
    Restart_Screen
    Exit Sub
Else
End If

'@@@@@@@@@@@@@@@@@@@@@@@@@@@ 60°C @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
'Show/Hide 1@60
If wS.Range("P98").Value = vbNullString Then wsT.Rows("422:422").EntireRow.Hidden = True
Restart_Screen
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM