简体   繁体   English

Loop If 语句也会在选择多个数据列表后运行宏

[英]Loop If statements as well run macros after multiple data list selections

  1. I would like the Work Change macro to apply for the entire column.我希望 Work Change 宏应用于整个列。

     Private Sub Worksheet_Change(ByVal Target As Range) If Target.Address(True, True) = "$P$1" Then Select Case Target Case "Keep - no action" Call KeepNoAction Case Else Selection.ClearContents End Select End If End Sub
  2. the macro called, "KeepNoAction" I would like it to loop for multiple columns/ rows:名为“KeepNoAction”的宏我希望它循环多列/行:

     Sub KeepNoAction() If Range("P1").Value = "Keep - no action" Then Range("N1").Select Selection.Copy Range("S1").Select Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=False Application.CutCopyMode = False Selection.AutoFill Destination:=Range("S1:AB1"), Type:=xlFillDefault Range("S1:AB1").Select Else End If If Range("P2").Value = "Keep - no action" Then Range("N2").Select Selection.Copy Range("S2").Select Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=False Application.CutCopyMode = False Selection.AutoFill Destination:=Range("S2:AB2"), Type:=xlFillDefault Range("S2:AB2").Select End If If Range("P3").Value = "Keep - no action" Then Range("N3").Select Selection.Copy Range("S3").Select Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _ :=False, Transpose:=False Application.CutCopyMode = False Selection.AutoFill Destination:=Range("S3:AB3"), Type:=xlFillDefault Range("S3:AB3").Select End If

Are you changing Column P to "Keep - no action", then would you really need to loop instead of just working with the target.row?您是否将 P 列更改为“保持 - 无操作”,那么您真的需要循环而不是仅使用 target.row 吗?

Private Sub Worksheet_Change(ByVal Target As Range)
    Dim r As Integer
    If Target.Column = 16 Then
        If Target = "Keep - no action" Then
            r = Target.Row

            Cells(r, "S").Value = Cells(r, "N").Value
            Cells(r, "S").AutoFill Destination:=Range("S" & r & ":AB" & r), Type:=xlFillSeries 'Type:=xlFillDefault
        End If

        If Target = "Something else" Then
            MsgBox "Do something else"
        End If

    End If

End Sub

Here is a loop you can work with:这是您可以使用的循环:

Sub DoIt()
    Dim LstRw As Long, Rng As Range, c As Range

    LstRw = Cells(Rows.Count, "P").End(xlUp).Row
    Set Rng = Range("P1:P" & LstRw)

    For Each c In Rng.Cells

        If c = "Keep - no action" Then
            r = c.Row
            Cells(r, "S").Value = Cells(r, "N").Value
            Cells(r, "S").AutoFill Destination:=Range("S" & r & ":AB" & r), Type:=xlFillSeries    'Type:=xlFillDefault
        End If

    Next c

End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM