简体   繁体   中英

excel VBA loop then copy based on condition

I am absolutely a novice when it comes to macros and VBA and need some help.

I have a workbook with 6 worksheets. The first work sheet ("All Grants FY15") has 5 columns ("Date", "Grant", "Type", "Amount" and "Category", A2:E2). I am using this worksheet to enter all grant expenses. The remaining worksheets ("City ESG", "County ESG", "CoC HMIS", "SSVF HMIS", and "HMIS Program Fees") have the same columns at the same placement. In each of these subsequent worksheets, I have the name of the grant (which matches the value that will be placed on "All Grants FY15", B2:B500) placed at F2.

I am looking for a way to loop through "All Grants FY15", read the value at B2:B500, and copy that row into the next blank row on the corresponding worksheet where F2 on that worksheet equals B2 on the "All Grants FY15".

Does that make sense? A

Step 1:

In the "All Grants FY15" sheet, add a column after the "Category" column to duplicate your date column using =A2 . To extend this formula to the last used row, double-click on the little square in the bottom-right-hand corner of the cell containing the first formula.

Step 2:

In each remaining worksheet, in the "Date" column, add the following formula in the first cell under the header:

=VLOOKUP($B2,Sheet1!$B$2:$F$500,5) - Change "Sheet1" as necessary

In the three columns after the "Grant" column - "Type", "Amount", and "Category" - add the following formulas:

=VLOOKUP($B2,Sheet1!$B$2:$F$500,2) - Change "Sheet1" as necessary

=VLOOKUP($B2,Sheet1!$B$2:$F$500,3) - Change "Sheet1" as necessary

=VLOOKUP($B2,Sheet1!$B$2:$F$500,4) - Change "Sheet1" as necessary

NOTE: If some of your "Date" cells may be blank on the "All Grants FY15" sheet, change the formula to =IF(ISBLANK(A2),"",A2)

If any of your data on the "All Grants FY15" sheet may be blank, change the remaining formulas to:

=IF(ISNA(VLOOKUP($B2,Sheet1!$B$2:$F$500,5)),"",VLOOKUP($B2,Sheet1!$B$2:$F$500,5))

=IF(ISNA(VLOOKUP($B2,Sheet1!$B$2:$F$500,5)),"",VLOOKUP($B2,Sheet1!$B$2:$F$500,2))

=IF(ISNA(VLOOKUP($B2,Sheet1!$B$2:$F$500,5)),"",VLOOKUP($B2,Sheet1!$B$2:$F$500,3))

=IF(ISNA(VLOOKUP($B2,Sheet1!$B$2:$F$500,5)),"",VLOOKUP($B2,Sheet1!$B$2:$F$500,4))

EXAMPLE

Here's an example to give you a better idea of where these formulas get placed:

"All Grants FY15" sheet:

       A        B        C         D           E           F
1  Date       Grant     Type     Amount     Category    (blank)
2  1/12/2014  abcd      efgh     1234       ijkl        1/12/2014
3             BCDE      FGHI     2345       JKLM
                                                           ^
                                                           |
                                                           |
                                                  =IF(ISBLANK(A2),"",A2)

"City ESG" sheet:

       A        B        C         D           E
1  Date       Grant     Type     Amount     Category
2  1/12/2014  abcd      efgh     1234       ijkl
3             BCDE      FGHI     2345       JKLM
       ^                  ^        ^          ^
       |                  |        |          |
       |                  |        |          |
       |                  |        |    =IF(ISNA(VLOOKUP($B2,'All Grants FY15'!$B$2:$F$13,4)),"",VLOOKUP($B2,'All Grants FY15'!$B$2:$F$13,4))
       |                  |        |
       |                  |    =IF(ISNA(VLOOKUP($B2,'All Grants FY15'!$B$2:$F$13,3)),"",VLOOKUP($B2,'All Grants FY15'!$B$2:$F$13,3))
       |                  |
       |              =IF(ISNA(VLOOKUP($B2,'All Grants FY15'!$B$2:$F$13,2)),"",VLOOKUP($B2,'All Grants FY15'!$B$2:$F$13,2))
       |
   =IF(ISNA(VLOOKUP($B2,'All Grants FY15'!$B$2:$F$13,5)),"",VLOOKUP($B2,'All Grants FY15'!$B$2:$F$13,5))

Obviously for cell B3, the "$B2" would be changed to "$B3" and so on.

I added an IF statement to check in column "F" on the source sheet for a 0 or 1 to test if already moved. When it gets moved, a 1 is marked in column "F" (ie, col #6). Since you only have 5 columns needing to be moved, I got rid of the lastCol variable and iserted From lCol 1 To 5 instead of lastCol. If something doesn't get copied, there will not be a 1 in column "F" and it's probably because none of the sheets has a value = to Column "B" -Grant.

Sub GrantCopy()

Dim lastRow As Long
Dim lastTRow As Long    'Last Target Row
Dim tRow As Long        'Target Row
Dim source As String    'The source sheet
Dim target As String    'Variable target sheet
Dim tempVal As String   'Hold value of Source!B2
Dim ws As Worksheet

source = "ALL Grants FY15"
lastRow = Sheets(source).Range("A" & Rows.Count).End(xlUp).Row

    For lRow = 3 To lastRow                 'Loop through source sheet
        If Sheets(source).Cells(lRow, "F").Value < 1 Then 'Check to make sure not already moved
            tempVal = Sheets(source).Cells(lRow, "B").Text

            For Each ws In Worksheets             'Loop through all sheets
                If ws.Name <> source Then       'Make sure not checking Source sheet
                    target = ws.Name

                    If Sheets(target).Range("F2").Text = tempVal Then      'Does "B(row)" = F2
                        lastTRow = Sheets(target).Range("A" & Rows.Count).End(xlUp).Row  'Get Last Row
                        tRow = lastTRow + 1             'Set new Row 1 after last

                        For lCol = 1 To 5        'Copy cells from one sheet to another loop columns
                            Sheets(target).Cells(tRow, lCol) = Sheets(source).Cells(lRow, lCol).Value
                        Next lCol
                        Sheets(source).Cells(lRow, "F") = 1   'Mark row as having been moved with a "1"
                    End If
                End If
            Next ws
        End If
    Next lRow
End Sub

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