宏经历了几个模块

问题描述 投票:0回答:1

我再次询问你的建议......我有几个宏,如果某个单元格或范围在另一个工作表上发生变化,应该由Worksheet_Calculate激活。一切正常,但我注意到,一些宏不会停在他们的“End Sub”,而是跳转到另一个宏,这会导致表中的列不被整理出来。这是我的:Worksheet_Calculate

    Private Sub Worksheet_Calculate()
Static OldVal As Variant

If Range("AL2").Value <> OldVal Then
    OldVal = Range("AL2").Value
    Call RF

ElseIf Range("AM2").Value <> OldVal Then
    OldVal = Range("AM2").Value
    Call SEAL

ElseIf Range("AN2").Value <> OldVal Then
    OldVal = Range("AN2").Value
    Call SUVPCR

ElseIf Range("AO2").Value <> OldVal Then
    OldVal = Range("AO2").Value
    Call Segment

ElseIf Range("AU2").Value <> OldVal Then
    OldVal = Range("AU2").Value
    Call RRC

ElseIf Range("AW2").Value <> OldVal Then
    OldVal = Range("AW2").Value
    Call WG

ElseIf Range("AY2").Value <> OldVal Then
    OldVal = Range("AY2").Value
    Call dB

ElseIf Range("BA2").Value <> OldVal Then
    OldVal = Range("BA2").Value
    Call Noise_em


End If

End Sub

RF的宏似乎没问题,如果我单独用F8运行它,它会执行而不会去其他地方:

Sub RF()

On Error Resume Next
If Sheets("All_list").Range("AL2").Value = "No" Then
ActiveWorkbook.Worksheets("All_list").ListObjects("All").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("All_list").ListObjects("All").Sort.SortFields.Add2 _
        Key:=Range("All[[#All],[RF]]"), SortOn:=xlSortOnValues, Order:= _
        xlAscending, DataOption:=xlSortTextAsNumbers
    With ActiveWorkbook.Worksheets("All_list").ListObjects("All").Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

ElseIf Sheets("All_list").Range("AL2").Value = "Yes" Then
ActiveWorkbook.Worksheets("All_list").ListObjects("All").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("All_list").ListObjects("All").Sort.SortFields.Add2 _
        Key:=Range("All[[#All],[RF]]"), SortOn:=xlSortOnValues, Order:= _
        xlDescending, DataOption:=xlSortTextAsNumbers
    With ActiveWorkbook.Worksheets("All_list").ListObjects("All").Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

ElseIf Sheets("All_list").Range("AL2").Value = "All" Then
ActiveWorkbook.Worksheets("All_list").ListObjects("All").Sort.SortFields.Clear
End If

End Sub

但是这个(Segment)也可以单独使用,但不能在Worksheet_calculate中,因为它出于某种原因跳转到RF后缀。

   Sub Segment()

Dim x() As Variant

With Sheets("All_list")

.Range("AP2:AP10").Clear
.Range("AO2:AO10" & .Cells(.Rows.Count, "AO").End(xlUp).Row).Copy
.Range("AP2").PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False

x = Application.Transpose(Sheets("All_list").Range("AP2:AP10").Value)

ActiveWorkbook.Worksheets("All_list").ListObjects("All").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("All_list").ListObjects("All").Sort.SortFields.Add2 _
        Key:=Range("All[Segment]"), SortOn:=xlSortOnValues, Order:=xlAscending, _
        CustomOrder:=Join(x, ","), DataOption:=xlSortNormal

End With

    With ActiveWorkbook.Worksheets("All_list").ListObjects("All").Sort
        .Header = xlYes
        .MatchCase = False
        .Orientation = xl`enter code here`TopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With


End Sub

它基本上意味着像RF(Seal,SUVPCR)这样的宏可以一起运行,甚至是Segment,但是在段之后似乎没有任何排序,所有内容都会切断它。当我用它来代替它,例如RRC,它起作用时,RRC起作用,但在它之后再没有任何东西。很感谢任何形式的帮助。谢谢

excel vba sorting worksheet-function
1个回答
0
投票

大家好,感谢您的支持,已经弄清楚了

Private Sub Worksheet_Calculate()
Static OldVal As Variant
Static OldVal2 As Variant
Static OldVal3 As Variant
Static OldVal4 As Variant
Static OldVal5 As Variant
Static OldVal6 As Variant
Static OldVal7 As Variant
Static OldVal8 As Variant

Application.EnableEvents = False

If Range("AL2").Value <> OldVal Then
    OldVal = Range("AL2").Value
    Call RF

ElseIf Range("AM2").Value <> OldVal2 Then
    OldVal2 = Range("AM2").Value
    Call SEAL

ElseIf Range("AN2").Value <> OldVal3 Then
    OldVal3 = Range("AN2").Value
    Call SUVPCR


ElseIf Range("AO2").Value <> OldVal4 Then
    OldVal4 = Range("AO2").Value
    Call Segment

ElseIf Range("AU2").Value <> OldVal5 Then
    OldVal5 = Range("AU2").Value
    Call RRC

ElseIf Range("AW2").Value <> OldVal6 Then
    OldVal6 = Range("AW2").Value
    Call WG

ElseIf Range("AY2").Value <> OldVal7 Then
    OldVal7 = Range("AY2").Value
    Call dB

ElseIf Range("BA2").Value <> OldVal8 Then
    OldVal8 = Range("BA2").Value
    Call Noise_em

End If

Application.EnableEvents = True

End Sub

我忘了将OldVal分别分配到每个范围,这个技巧使我的一天。尽管在这种情况下似乎没有任何影响,但我还提到了关于安全的事件。再次感谢支持

© www.soinside.com 2019 - 2024. All rights reserved.