简体   繁体   中英

Excel VBA - Transfer Data range from one sheet to another based on conditional text string

I am new to macros and am trying to transfer a data range from one worksheet to another based on a conditional text string.

Data in "inquires" sheet (Range "A5:G1200") needs to transfer to "quotes" sheet based on if column Range "K6:K1200" in "inquiries" sheet is a text string "Y" or "N". If text is "Y" we want the data rows with "y" to transfer, if the text is "N" we do not want rows with "N" to transfer.

Once the data range with "Y" transfers over to "quotes" sheet, we then need to transfer data from "Quotes" sheet (Range "A5:G1200" & and separate Range "L5:M1200") to "orders" sheet based on if column range "N6:N1200" in "quotes" sheet is a text string "Rec'vd" or "Closed". Same as above, If text is "Rec'vd" we want the data rows with "rec'vd" to transfer. If "Closed" then no data from "closed" rows should transfer.

Once all data is transferred to all 3 sheets based on text conditions. I want to have a 4th sheet that will filter this data based on a date range. I want to be able to type in a date range and then all data from each sheet ("inquires", "quotes", & "orders") to filter to that "master" 4th sheet. Each data sheet should have its own area (one below the other) on the master sheet to filter to.

Here is what I have so far..

Sub TransferTest1()
    Dim INQUIRE As Worksheet
    Dim QUOTE As Worksheet
    Dim ORDER As Worksheet
    Dim YString As String
    Dim RecString As String

    Set INQUIRE = ActiveWorkbook.Sheets("Inquiries")
    Set QUOTE = ActiveWorkbook.Sheets("Quotes")
    Set ORDER = ActiveWorkbook.Sheets("Orders")

    If INQUIRE.Range("K6:K1200") = "Y" Then
        INQUIRE.Range("A5:G1200").Copy QUOTE.Range("A5")
    End If

    If QUOTE.Range("N6:N1200") = "Rec'vd" Then
        QUOTE.Range("A5:G1200").Copy ORDER.Range("A5")
        QUOTE.Range("L5:M1200").Copy ORDER.Range("K5")
    End If
End Sub

I appreciate any and all help!

Robert Smithey

This is a typical use-case for Autofilter .

With INQUIRE.Range("A4:K1200")
    .AutoFilter 11, "Y"
    .offset(1).Resize(, 7).Copy QUOTE.Range("A5")  ' columns A:G
    .AutoFilter
End With

With QUOTE.Range("A4:N1200")
    .AutoFilter 14, "Rec'vd"
    .offset(1).Resize(, 7).Copy ORDER.Range("A5") ' columns A:G
    .offset(1).Resize(, 2).offset(11).Copy ORDER.Range("K5")  ' columns L:M
    .AutoFilter
End With

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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