Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 循环If语句并在多个数据列表选择后运行宏_Vba_Excel - Fatal编程技术网

Vba 循环If语句并在多个数据列表选择后运行宏

Vba 循环If语句并在多个数据列表选择后运行宏,vba,excel,Vba,Excel,我希望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 S

我希望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
  • 名为“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
    

  • 如果您将p列更改为“Keep-no action”,那么您真的需要循环而不是只处理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
    
    以下是您可以使用的循环:

    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
    

    请不要只告诉我们您想要什么。请告诉我们您尝试了什么和
    您被卡在哪里
    您遇到了什么错误
    如果target.column=16,那么当您更改p列时
    将生效上面的代码工作得很好!!非常感谢:)你是一个救命恩人。也许你可以把它作为答案,这样它就不会像没有答案一样突然出现。