嵌套的 if else 语句无法通过目标单元格工作

Posted

技术标签:

【中文标题】嵌套的 if else 语句无法通过目标单元格工作【英文标题】:nested if else statment not working via target cell 【发布时间】:2019-03-04 10:45:16 【问题描述】:

您好,我有一个私有子(通过更改工作表),它将根据单元格值运行 26 个宏之一。所有这些都是根据包含公式的单元格中的值隐藏或取消隐藏特定行。

现在,当我手动更改单元格的值时,工作表更改事件和宏的所有工作都按预期工作,但是当单元格值通过公式更改时它们不起作用,或者更确切地说是它们的一部分。

我已经在每个宏中设置了工作表和范围变量,同样工作表中的目标单元格更改事件也正确设置(dim r as range 等)

工作表更改事件的代码如下 - 想知道是否有人可以发现或帮助解释为什么在没有手动单元格值更改的情况下运行时这似乎不起作用

如果可以避免,我不想以编程方式更改单元格中的值(如果“条件”则 r.value="A value" 26 次!)

这是工作表代码

Dim r As Range, r1 As Range, r2 As Range, r3 As Range, r4 As Range, r5 As Range, r6 As Range, r7 As Range, r8 As Range
Dim r9 As Range, r10 As Range, r11 As Range, r12 As Range, r13 As Range, r14 As Range, r15 As Range, r16 As Range


Private Sub Worksheet_Change(ByVal Target As Range)    

'Set range for selecting the region
Set r = Range("O19")

'Set facility ranges

Set r1 = Rows("22:24"): Set r2 = Rows("25:27"): Set r3 = Rows("28:30"): Set r4 = Rows("31:33")

'set product line ranges
Set r5 = Rows("37"): Set r6 = Rows("38"): Set r7 = Rows("39"): Set r8 = Rows("40"): Set r9 = Rows("41"): Set r10 = Rows("42"): Set r11 = Rows("43:45")
Set r12 = Rows("46:48"): Set r13 = Rows("49"): Set r14 = Rows("51:52"): Set r15 = Rows("36"): Set r16 = Rows("50")

'Hiding facility Rows based on product line

If r.Value = 1 Then ' Select Facility & all cells hidden
    Application.Run ("Select_Facility")
    ElseIf r.Value = 2 Then ' This is for North America & no Facility
    Application.Run ("NA_NoFacility")
    ElseIf r.Value = 3 Then ' This is for Breen Only
    Application.Run ("Breen")
    ElseIf r.Value = 4 Then ' This is for Conroe Only
    Application.Run ("Conroe")
    ElseIf r.Value = 5 Then ' This is for Lafayette Only
    Application.Run ("Lafayette")
    ElseIf r.Value = 6 Then ' This is for Breen & Conroe Only
    Application.Run ("Breen_Conroe")
    ElseIf r.Value = 7 Then ' This is for Breen & Lafayette Only
    Application.Run ("Breen_Lafayette")
    ElseIf r.Value = 8 Then ' This is for Conroe & Lafayette
    Application.Run ("Conroe_Lafayette")
    ElseIf r.Value = 9 Then ' This is for All North America
    Application.Run ("All_NA")
    ElseIf r.Value = 10 Then ' This is for Europe and no facility
    Application.Run ("Europe_NoFacility")
    ElseIf r.Value = 11 Then 'This is for Gateshead only
    Application.Run ("Gateshead")
    ElseIf r.Value = 12 Then 'This is for Kintore only
    Application.Run ("Kintore ")
    ElseIf r.Value = 13 Then 'This is for Kintore & Gateshead only
    Application.Run ("All_Europe")
    ElseIf r.Value = 14 Then ' This is for Middle East and no facility
    Application.Run ("Europe_NoFacility")
    ElseIf r.Value = 15 Then 'This is for Dubai only
    Application.Run ("Dubai")
    ElseIf r.Value = 16 Then 'This is for Saudi only
    Application.Run ("Saudi")
    ElseIf r.Value = 17 Then 'This is for Dubai and Saudi only
    Application.Run ("Dubai_Saudi")
    ElseIf r.Value = 18 Then ' This is for Far East & no Facility
    Application.Run ("FE_NoFacility")
    ElseIf r.Value = 19 Then ' This is for Loyang Only
    Application.Run ("Loyang")
    ElseIf r.Value = 20 Then ' This is for Tuas Only
    Application.Run ("Tuas")
    ElseIf r.Value = 21 Then ' This is for Perth Only
    Application.Run ("Perth")
    ElseIf r.Value = 22 Then ' This is for Loyang & Tuas Only
    Application.Run ("Loyang_Tuas")
    ElseIf r.Value = 23 Then ' This is for Loyang & Perth Only
    Application.Run ("Loyang_Perth")
    ElseIf r.Value = 24 Then ' This is for Tuas and Perth Only
    Application.Run ("Tuas_Perth")
    ElseIf r.Value = 25 Then ' This is for All far East facilities
    Application.Run ("All_FE")
    ElseIf r.Value = 26 Then ' This is for Global
    Application.Run ("All_Global")
End If

用户可以选择许多设施组合,这些组合给我 26 个变体 - 因此上面有 26 个选项 - 最初隐藏行的说明在每个 ElseIF 选项内,但它在那里也不起作用。

表单的工作方式是用户选择区域,然后将选择相关设施 - 这将更改目标单元格两次,并修改隐藏行和可见行。用户可以从中选择是或否。

【问题讨论】:

This event does not occur when cells change during a recalculation VBA code doesn't run when cell is changed by a formula的可能重复 【参考方案1】:

首先,您需要处理 Worksheet_Calculate() 事件,正如 Sam 已经提到的。但还有更多:为什么要使用 26 个条件的 if 子句?

创建一个包含两列的下一个工作表 (Application_Sheet),如下所示:

R_Value      Application
      1   Select_Facility
      2       No_Facility
    ...               ...

不要使用复杂的 if 子句,而是执行以下操作(未测试):

Application.Run(Range(Application_Sheet!B1).Offset(r.Value).Value)  

【讨论】:

Dominique 我使用了 26 个条件 if 子句,因为这是我能想到的实现我需要的唯一方法。我原以为我可以合并屏幕更新,但我知道这也有问题。虽然我也可以使用 Select Case。但很明显,代码在第一次更改时工作正常,但第二次没有被捕获。 您对Worksheet_Calculate() 有什么问题? 它崩溃了,但我需要在代码中添加一些中断之王,因为它似乎无限循环并锁定 excel,我怀疑这是由于宏代码而不一定是 worksheet_calculate()事件代码,基本上和你上面的一样【参考方案2】:

我已经实现了上面推荐的一些更改,虽然代码看起来更整洁,但我根本无法让它工作,这是我的 worksheet_calculate() 和引用的宏的代码。当我点击调试选项时,我不断收到代码执行中断错误,然后指向 Worksheet_Calculate() 名称。

Private Sub Worksheet_Calculate()

'Define the worksheets & Ranges for use in this routine
Dim r As Range: Set r = Range("N19")

'Hiding facility Rows based on product line
Application.Run (Range("AB1").Offset(r.Value).Value)

End Sub

其中一个宏是

Sub Select_Facility()

Dim ws As Worksheet: Set ws = ThisWorkbook.Worksheets("Supplier Details")

ws.Activate
Rows("22:24").EntireRow.Hidden = True 
Rows("25:27").EntireRow.Hidden = True 
Rows("28:30").EntireRow.Hidden = True 
Rows("31:33").EntireRow.Hidden = True 
Rows("37").EntireRow.Hidden = True 
Rows("38").EntireRow.Hidden = True 
Rows("39").EntireRow.Hidden = True 
Rows("40").EntireRow.Hidden = True 
Rows("41").EntireRow.Hidden = True 
Rows("42").EntireRow.Hidden = True 
Rows("43:45").EntireRow.Hidden = True 
Rows("46:48").EntireRow.Hidden = True 
Rows("49").EntireRow.Hidden = True 
Rows("51:52").EntireRow.Hidden = True 
Rows("36").EntireRow.Hidden = True 
ws.Rows("50").EntireRow.Hidden = True 

End Sub

也许我做错了一些简单的事情,但是....哦,在我开始之前,上面列表中的所有行都设置为隐藏。

【讨论】:

以上是关于嵌套的 if else 语句无法通过目标单元格工作的主要内容,如果未能解决你的问题,请参考以下文章

带有嵌套 if/else 语句的 While 循环

if--else 嵌套 怎么理解?

If 语句不允许插入语句工作,因为单元格为空

IT兄弟连 Java语法教程 流程控制语句 分支结构语句4

if else 语句取决于数组元素(swift3)

简化javascript中的嵌套if else语句[关闭]