-1

我尝试使用 VLookup 和 Index&Match,但看起来只有 VBA 循环可以帮助我:

我有一个以第一个工作表中的日期开头的列表。现在我想遍历这个列表,检查列表第一列中日期的月份和年份是否与第二个工作表中的月份和年份匹配。如果是这样,则应将匹配日期行的其余信息复制到工作表 2。

我在这里上传了带有一些示例数据的工作表:https ://cignifi.box.com/s/a6k03lh0bfe9ob53d4jy

每一个帮助将不胜感激!我对 VBA 有一些了解,但对这类问题还不够好……

非常感谢!

4

1 回答 1

0

我不确定你的成品应该是什么,但这个子程序会按照你的要求做。它将使用 Sheet1 上表中的信息覆盖 Sheet2 上已有的任何信息。如果这不能满足您的要求,请告诉我。
您可以通过在开始时更改变量来调整传入和传出哪些列数据。

编辑:添加代码以检查下一个空白的帐户名称列,并使用该行号复制到:

Sub ProcessTableData()
    Dim wsSource, wsDestination As Worksheet
    Dim rowSourceStart, colSourceDate, rSource, colDestStart As Long
    Dim rowDestMonthYear, cDest, rowDestInsertAt As Long
    Dim destBlankColumnCount As Integer
    Dim colDestRegion, colDestAccountName, colDestPotentialName As Integer
    Dim colDestAmount, colDestWeightedAmount As Integer
    Dim colSourceRegion, colSourceAccountName, colSourcePotentialName As Integer
    Dim colSourceAmount, colSourceWeightedAmount As Integer

    Set wsSource = Sheet1
    Set wsDestination = Sheet2

    'Destination column offsets from month column
    colDestRegion = 0
    colDestAccountName = 1
    colDestPotentialName = 2
    colDestAmount = 3
    colDestWeightedAmount = 4

    'Source columns
    colSourceRegion = 5
    colSourceAccountName = 3
    colSourcePotentialName = 4
    colSourceAmount = 6
    colSourceWeightedAmount = 7

    colSourceDate = 2 'Source column for date
    rowSourceStart = 3 'Source starting row

    rowDestMonthYear = 2 'Destination row to check for month & year matching

    rSource = rowSourceStart
    'loop until the date field on the source sheet is blank
    Do While wsSource.Cells(rSource, colSourceDate).Value <> ""
        cDest = 1
        destBlankColumnCount = 0
        'loop through the destination columns until we've seen 5 blanks
        '(only 3 are ever expected)
        Do Until destBlankColumnCount > 5
            If wsDestination.Cells(rowDestMonthYear, cDest).Value <> "" Then
                destBlankColumnCount = 0
                'check if month matches
                If Month(wsSource.Cells(rSource, colSourceDate).Value) = wsDestination.Cells(rowDestMonthYear, cDest).Value Then
                    'check if year matches
                    If Year(wsSource.Cells(rSource, colSourceDate).Value) = wsDestination.Cells(rowDestMonthYear, (cDest + 1)).Value Then
                        'find last row to copy data to by finding the next blank "Account Name" row
                        rowDestInsertAt = (rowDestMonthYear + 2)
                        Do Until wsDestination.Cells(rowDestInsertAt, (cDest + colDestAccountName)).Value = ""
                            rowDestInsertAt = rowDestInsertAt + 1
                        Loop
                        'copy field data
                        wsDestination.Cells(rowDestInsertAt, (cDest + colDestAccountName)).Value = wsSource.Cells(rSource, colSourceAccountName).Value
                        wsDestination.Cells(rowDestInsertAt, (cDest + colDestPotentialName)).Value = wsSource.Cells(rSource, colSourcePotentialName).Value
                        wsDestination.Cells(rowDestInsertAt, (cDest + colDestRegion)).Value = wsSource.Cells(rSource, colSourceRegion).Value
                        wsDestination.Cells(rowDestInsertAt, (cDest + colDestAmount)).Value = wsSource.Cells(rSource, colSourceAmount).Value
                        wsDestination.Cells(rowDestInsertAt, (cDest + colDestWeightedAmount)).Value = wsSource.Cells(rSource, colSourceWeightedAmount).Value
                    End If
                End If
            Else
                destBlankColumnCount = destBlankColumnCount + 1
            End If
            cDest = cDest + 1
        Loop
        rSource = rSource + 1
    Loop

End Sub
于 2013-03-08T23:34:34.197 回答