我收到了一份月度老化报告。我需要比较这两个报告,找出每个报告上显示的项目。我需要一个VBA来查看两个工作表的B列,如果匹配,只提取这些行。另外,我需要一个vba来查明是否有变化。对于那些不熟悉老化报告的人,这份报告告诉我客户付给我的时间到期了多远。因此,如果客户1,在20151023报告时报告的老化0-30(第S列),那么在报告中拉出20160223,该客户应该在年龄90-120(第V列)。我认为显示此信息的最佳方法是显示匹配数据,而无更改表中没有更改。然后,在名为Changes的表格中,我想显示大约6列信息:报告日期,交易者,合同号,状态,老化历史记录,& AR金额。老龄化历史记录将告诉我该报告被拉动时该客户处于什么状态。此信息将来自列标题(S-X)。 AR金额将是该列中出现的金额。我会尝试上传样本。
答案 0 :(得分:0)
以下为您提供了一种比较两张纸中相同列的方法。第一种方法在第二张工作表的列中的任何位置查找匹配值,如果匹配的单元格位于两个工作表的同一行,则第二种方法仅返回匹配
Sub CompareSheets(sheet1 As Worksheet, sheet2 As Worksheet, columnNumber As Long)
Dim ws1 As Worksheet, ws2 As Worksheet
Dim ws1Data As Range, ws2Data As Range
Dim col As Long
Dim i As Integer
Dim cell1 As Range, cell2 As Range
Set ws1 = sheet1
Set ws2 = sheet2
col = columnNumber
ws1Data = ws1.Columns(col).Cells
ws2Data = ws2.Columns(col).Cells
在任一列的任何位置找到匹配项
' XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
' This code will find any match between the two sheets
' XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
'Checks each cell in the target column of sheet 1
For Each cell1 In ws1Data
'Checks each cell in the target column of sheet 2
For Each cell2 In ws2Data
'If the cell from sheet 1 matches any cell in sheet 2 then do stuff
If cell1 = cell2 Then
'Do stuff with the data here
End If
Next cell2
Next cell1
' XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
' XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
仅当匹配的单元格位于同一行时才查找匹配
' ////////////////////////////////////////////////////////
' This code will find a match ONLY if both entries are
' on the same row in their respective sheets
' ////////////////////////////////////////////////////////
'Checks each cell in the target column of sheet 1
For Each cell1 In ws1Data
'Checks each cell in the target column of sheet 2
For Each cell2 In ws2Data
'If the cell from sheet 1 matches any cell in sheet 2
'AND there are on the same row in both sheets then do stuff
If (cell1 = cell2) And (cell1.Row = cell2.Row) Then
'Do stuff with the data here
End If
Next cell2
Next cell1
' /////////////////////////////////////////////////////////
' /////////////////////////////////////////////////////////
End Sub
然后你可以制作一个调用子
的按钮或宏Call CompareSheets(firstworksheet, secondworksheet, 2) '2 is the column number for B
一个恰当的例子
Call CompareSheets(Worksheets(1), Worksheets(2), 2)
关于匹配后你想要做什么,这真的是另一个帖子。