Excel 2013 - 在一个工作表中使用多个Private Sub命令

时间:2016-02-07 04:59:43

标签: excel vba excel-vba

我使用以下代码运行我的一个宏,具体取决于下拉字段中选择的项目,下面是我使用的代码:

这完全有效,除了我想在第一个下面再次重复这个过程。它只是根据下拉字段中的答案粘贴信息。我想在Cell A136中再次这样做 - 任何帮助都会受到赞赏,无论何时我尝试我都会收到错误,或者什么都不做。

Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A43")) Is Nothing Then
Select Case Range("A43")
    Case "Amputation": Amputation
    Case "Asthma": Asthma
    Case "Burns_No_Smoke_Inhalation_Not_On_Limb": Burns_No_Smoke_Inhalation_Not_On_Limb
    Case "Burns_No_Smoke_Inhalation_On_Limb": Burns_No_Smoke_Inhalation_On_Limb
    Case "Burns_Smoke_Inhalation_Not_On_Limb": Burns_Smoke_Inhalation_Not_On_Limb
    Case "Burns_Smoke_Inhalation_On_Limb": Burns_Smoke_Inhalation_On_Limb
    Case "Closed_Fracture": Closed_Fracture
    Case "CORD": CORD
    Case "CPR_Patient_Does_Not_Recover": CPR_Patient_Does_Not_Recover
    Case "CPR_Patient_Does_Recover": CPR_Patient_Does_Recover
    Case "Dislocation": Dislocation
    Case "Hyperthermia": Hyperthermia
    Case "Hypothermia": Hypothermia
    Case "Open_Fracture_Major_Bleeding": Open_Fracture_Major_Bleeding
    Case "Open_Fracture_Minor_Bleeding": Open_Fracture_Minor_Bleeding
    Case "Open_Wound_Major_Controlable_Bleeding": Open_Wound_Major_Controlable_Bleeding
    Case "Open_Wound_Minor_Bleeding": Open_Wound_Minor_Bleeding
    Case "Open_Wound_Needs_Tourniquet": Open_Wound_Needs_Tourniquet
    Case "Spinal_Injury_Conscious_No_KED": Spinal_Injury_Conscious_No_KED
    Case "Spinal_Injury_Conscious_With_KED": Spinal_Injury_Conscious_With_KED
    Case "Spinal_Injury_Unconscious_No_KED": Spinal_Injury_Unconscious_No_KED
    Case "Spinal_Injury_Unconscious_With_KED": Spinal_Injury_Unconscious_With_KED

End Select     
End If
End Sub

1 个答案:

答案 0 :(得分:4)

通过将整个Select Case语句替换为:

,可以大大简化代码
Application.Run Range("A43").Value

然后您的事件处理程序将成为:

Private Sub Worksheet_Change(ByVal Target As Range)

If Not Intersect(Target, Range("A43")) Is Nothing Then
    Application.Run Range("A43").Value
ElseIf Not Intersect(Target, Range("A136")) Is Nothing Then
    Application.Run Range("A136").Value
End If

End Sub

对于您需要做更多工作而不仅仅是调用另一个宏的情况,您可以将整个Select Case部分放在一个单独的函数中,并将相关单元格作为Range参数传递给它