2016-10-11 34 views
2

我写这段代码但烧毁了两者之间的附加填料。根据选定的时间点,它将隐藏适当的行。VBA代码 - 跳过一些地方,如果一些别人excecuted

容器1每时每刻都会有,但是,如果没有选择另一个容器中,我希望它可以隐藏所有剩余的行而不处理代码的其余部分。因此,如果选择容器1和2,它将运行这些代码而不运行代码的其余部分。

,因为有这么多的可能的时间点,改写本作为循环将是极其复杂的,它更跳过不相关的代码的问题。几乎像goto线或什么的?我不知道!

是否有任何其他的方式,使这个代码的运行效率比暂时禁用DisplayPageBreaks,ScreenUpdating和启用活动?没有计算在页面上执行,只有行隐藏。

例如,如果Q26是空白的(无容器2)我希望它去的代码末尾,没有处理任何东西,但我怎么写它,它仍然处理代码的其余部分。

感谢您的帮助

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

谢谢您的帮助!

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 [email protected] 

    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 [email protected] 

    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 [email protected] 

    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 [email protected] 

    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 
+0

你可以尝试使用'GoTo',但这不是很推荐......或者只是用更多的逻辑重新排列你的代码!不需要循环,但使用'If's或'Select's和正确的缩进,你可以这样做 – R3uK

+0

使用局部变量,例如w =工作表(“StabDataCapture”)并稍后引用它们(或With块)可能会提供一个非常小的改进(只有在为每个选定单元格的更改调用此函数时才会计数)。我也会尝试Application.Calculation = xlManual,即使你认为没有太多的计算。但是,我没有看到为什么你的代码应该很慢。正如R3uK也指出的那样,这种缩进是可怕的。 – z32a7ul

+0

如果您也从另一个调用此函数,则保存DisplayPageBreaks,EnableEvents等的值,并在最后将其恢复到原始值。否则,这个子的调用者可能会放弃这些优化,如果它也使用它们的话。 – z32a7ul

回答

1

你需要一个程序来重新激活屏幕和事件,

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

使用Exit Sub,它看起来是这样的:

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 [email protected] 
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 [email protected] 
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 [email protected] 
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 [email protected] 
If wS.Range("P98").Value = vbNullString Then wsT.Rows("422:422").EntireRow.Hidden = True 
Restart_Screen 
End Sub 
+0

然而,这样做的确很好,但是会有超过400行代码需要更新,即使使用查找和替换也需要一整天的时间。我一定会在未来的实现中使用它。 我把它分解成模块,这一行代码(x3)是我如何解决这个问题。 感谢您的帮助! 。 – vividillusion

+0

如果工作表( “StabDataCapture”)范围( “Q26”)值<> 0,则container2的否则工作表( “模板”)行( “142:1048576”)。EntireRow.Hidden =真 – vividillusion