【问题标题】:Trying to Copy column and paste into another sheet based on another cells value VBA尝试根据另一个单元格值 VBA 复制列并粘贴到另一个工作表中
【发布时间】:2020-03-06 20:30:57
【问题描述】:

仅当另一个单元格等于“是”时,我才尝试复制列的内容。如果单元格等于“是”,我想将选定的列范围粘贴到另一个工作簿中。然后循环到下一个“是”。

    If M2 = "Yes" then copy AD2:AD200 if "NO" go to the next "if"
    If M3 = "Yes" then copy AE2:AE200 if "NO" go to the next "if"
    If M4 = "Yes" then copy AF2:AF200 if "NO" go to the next "if" 

等等……

我要寻找的最后一个是 M11。

然后将复制范围粘贴到工作表“QA 状态中的案例”中的最后一个空白单元格,

    range("AL200",range("al200").end(xlUp).select

这是我目前所拥有的:

    Sheets("Sheet1").Select
    If Range("M8").Value = True Then
    Range("aj2:aj200").Select
    Selection.Copy
    Sheets("Cases in QA Status").Select
    End If
    End Sub

【问题讨论】:

    标签: vba loops if-statement


    【解决方案1】:

    您可以尝试以下方法吗?

    If Sheets("Sheet1").Range("M8").Value = True Then
        Sheets("Sheet1").Range("aj2:aj200").Copy
        ActiveSheet.Paste Destination:=Sheets("Cases in QA Status").Range("A1")
    End If
    

    并重复其他列:)。

    当然,如果您在列中有“是”,则将True 替换为"Yes"

    【讨论】:

    • @GhiaHoldren 如果您对任何答案感到满意:您可以标记一个最佳答案并投票给任意数量的有用答案。
    【解决方案2】:

    1。复制方法

    Sub test()
        Dim Ws As Worksheet, toWs As Worksheet
        Dim vDB As Variant, rngDB As Range
        Dim Target As Range
        Dim i As Long
    
        Set Ws = Sheets("Sheet1")
        Set toWs = Sheets("Cases in QA Status")
        With Ws
            vDB = .Range("m2", .Range("m" & Rows.Count).End(xlUp))
        End With
    
        For i = 1 To UBound(vDB, 1)
            If vDB(i, 1) = "Yes" Then 'vDB(i, 1) = True then
                Set rngDB = Ws.Range("ad2").Resize(199).Offset(, i - 1)
                Set Target = toWs.Range("al" & Rows.Count).End(xlUp).Offset(1, 0)
                rngDB.Copy Target
            End If
        Next i
    End Sub
    

    2。使用数组

    Sub test2()
        Dim Ws As Worksheet, toWs As Worksheet
        Dim vDB As Variant, vData As Variant
        Dim Target As Range
        Dim i As Long
    
        Set Ws = Sheets("Sheet1")
        Set toWs = Sheets("Cases in QA Status")
        With Ws
            vDB = .Range("m2", .Range("m" & Rows.Count).End(xlUp))
        End With
    
        For i = 1 To UBound(vDB, 1)
            If vDB(i, 1) = "Yes" Then 'vDB(i, 1) = True then
                vData = Ws.Range("ad2").Resize(199).Offset(, i - 1)
                Set Target = toWs.Range("al" & Rows.Count).End(xlUp).Offset(1, 0)
                Target.Resize(199) = vData
            End If
        Next i
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2019-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多