簡體   English   中英

嘗試根據另一個單元格值 VBA 復制列並粘貼到另一個工作表中

[英]Trying to Copy column and paste into another sheet based on another cells value VBA

僅當另一個單元格等於“是”時,我才嘗試復制列的內容。 如果單元格等於“是”,我想將選定的列范圍粘貼到另一個工作簿中。 然后循環到下一個“是”。

    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。

然后將復制范圍粘貼到“質量檢查狀態中的案例”表中的最后一個空白單元格,

    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

您可以嘗試以下方法嗎?

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

並重復其他列:)。

當然,如果列中有 "Yes" ,則將True替換為"Yes"

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM