简体   繁体   中英

VBA Excel Find data in table on different sheet

I have two worksheets, Receiving and Order Archive, both of which have tables on them with the same headers: sku, qty, Patient Name, Product Info, Vendor, L/R, Clinician, Date Ordered, PO Number, Received, Cost .

I'm trying to write a macro that starts on receiving page table, and pulls all the rows found on the Order Archive sheet table named "OrderArchive", copies that row information onto "Receiving" table. However I only want it to pull ones that are designated in the Received column as "[Pending]".

Usually don't like to hand code out without any effort on your end, but give this a try and let me know if it's what you're looking for:

在此处输入图片说明

'I named the Receiving tab's table "receivingTable"
'I named the Order Archive tab's table "orderArchiveTable"

Dim cnt As Integer
Dim x As Integer
Dim y As Integer
Dim cntPending As Integer
Dim myArray() As Variant

'count number of rows that are in orderArchiveTable
cnt = Range("orderArchiveTable").Rows.Count

'Scan orderArchiveTable for 'Pending' Orders in the 'Received' column
cntPending = 0
For x = 1 To cnt

    'count number of row that are Pending to use for array size
    If Range("orderArchiveTable[Received]")(x).Value = "Pending" Then
        cntPending = cntPending + 1
    End If

Next x


If cntPending = 0 Then Exit Sub  'no pending orders


'ReDim array for correct size... remember that it starts at zero! NOT 1
ReDim myArray(cntPending - 1, 9)

'Fill array with values
y = 0
For x = 1 To cnt
    If Range("orderArchiveTable[Received]")(x).Value = "Pending" Then
        myArray(y, 0) = Range("orderArchiveTable[sku]")(x).Value
        myArray(y, 1) = Range("orderArchiveTable[qty]")(x).Value
        myArray(y, 2) = Range("orderArchiveTable[Patient Name]")(x).Value
        myArray(y, 3) = Range("orderArchiveTable[Vendor]")(x).Value
        myArray(y, 4) = Range("orderArchiveTable[L/R]")(x).Value
        myArray(y, 5) = Range("orderArchiveTable[Clinician]")(x).Value
        myArray(y, 6) = Range("orderArchiveTable[Date Ordered]")(x).Value
        myArray(y, 7) = Range("orderArchiveTable[PO Number]")(x).Value
        myArray(y, 8) = Range("orderArchiveTable[Received]")(x).Value
        myArray(y, 9) = Range("orderArchiveTable[Cost]")(x).Value

        'Not sure if you want to delete the rows taken, but you would do this here and subtract 1 from x and cnt
        Selection.ListObject.ListRows(x).Delete
        x = x - 1
        cnt = cnt - 1

        'go to next row in array
        y = y + 1
    End If
Next x

'count number of rows that are in receivingTable
cnt = Range("receivingTable").Rows.Count + 1

'Dump array into receivingTable
For x = 0 To UBound(myArray)
    Set NewRow = Range("receivingTable").ListObject.ListRows.Add(AlwaysInsert:=True)
    Range("receivingTable[sku]")(cnt + x).Value = myArray(x, 0)
    Range("receivingTable[qty]")(cnt + x).Value = myArray(x, 1)
    Range("receivingTable[Patient Name]")(cnt + x).Value = myArray(x, 2)
    Range("receivingTable[Vendor]")(cnt + x).Value = myArray(x, 3)
    Range("receivingTable[L/R]")(cnt + x).Value = myArray(x, 4)
    Range("receivingTable[Clinician]")(cnt + x).Value = myArray(x, 5)
    Range("receivingTable[Date Ordered]")(cnt + x).Value = myArray(x, 6)
    Range("receivingTable[PO Number]")(cnt + x).Value = myArray(x, 7)
    Range("receivingTable[Received]")(cnt + x).Value = myArray(x, 8)
    Range("receivingTable[Cost]")(cnt + x).Value = myArray(x, 9)
Next x

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