如果其他陈述无法通过目标单元格

时间:2019-03-04 10:45:17

标签: excel vba

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

现在,当我手动更改单元格的值时,工作表更改事件和宏均按预期工作,但是当通过公式更改单元格值时,它们将不起作用,或者它们的某些位会起作用。

我已经设置了工作表,并在每个宏中设置了范围变量,同样,工作表中的目标单元格也正确地发生了更改事件(将r设为范围等)

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

如果可以避免,我不想以编程方式更改单元格中的值(如果“条件”,则r.value =“ A值” 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选项中,但在那儿也无法使用。

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

2 个答案:

答案 0 :(得分: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)  

答案 1 :(得分:1)

我已经实现了上面建议的一些更改,尽管代码看起来更整洁,但我根本无法使它正常工作,这是我的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

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