我不确定你的成品应该是什么,但这个子程序会按照你的要求做。它将使用 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