【问题标题】:compare data in two sheets and write it in another sheet in same workbook比较两张表中的数据并将其写入同一工作簿的另一张表中
【发布时间】:2012-11-23 08:50:07
【问题描述】:

我希望我的代码执行的操作:有一个名为“DB”的工作表,其中包含从表中获取的数据。我有“XML”工作表,其中包含从 XML 获取的数据。我想从“DB”表中获取列名“FName”并在“XML”表中搜索相同的列名“FName”。如果列名匹配,则从“DB”表中获取相应的值并与“XML”表进行比较。我在同一个工作簿中有另一张“结果”表。我从“DB”表中获取了列名并粘贴了转置(按行排列)。我必须做以下场景:
1. 如果两个工作表中的列名匹配:
- 在两个工作表中搜索对应列名的值匹配。如果值匹配,在“结果”表中,我必须在列名为“FName”的行旁边的列中写下“匹配”或“不匹配”。

2.如果两个工作表中的列名都不匹配:
- 在“结果”表中,我必须在列名为“FName”的行旁边的列中写下“NO Matching COLUMN”。

目前,在这段代码中,我想解析“DB”表中的每一列并搜索该列。但我收到“应用程序定义或对象定义错误”。

请告诉我如何实现上述场景:

Dim FindString As Range
Dim Rng As Range

Dim i, j As Integer
Dim finalcol As Long

Worksheets("DB").Select

finalcol = Worksheets("DB").Cells(1, Application.Columns.Count).End(x1toleft).column
On Error Resume Next


For i = 1 To finalcol
 FindString = Cells(1, i).Value

If Trim(FindString) <> "" Then
    With Sheets("xml").Range("A:A")
        Set Rng = .Find(What:=FindString, _
                        After:=.Cells(.Cells.Count), _
                        LookIn:=xlValues, _
                        LookAt:=xlWhole, _
                        SearchOrder:=xlByRows, _
                        SearchDirection:=xlNext, _
                        MatchCase:=False)
        If Not Rng Is Nothing Then
            Application.Goto Rng, True
        Else
            MsgBox "Nothing found"
        End If
    End With
End If
Next i
 On Error GoTo 0

结束子

【问题讨论】:

  • 这似乎可以通过使用 excel 公式和完全避免使用 vba 来更容易地实现和维护。我不确定你在尝试什么,你能放一个 DB 数据、XML 数据和结果的样本吗?

标签: excel vba compare


【解决方案1】:

根据您的描述,这是我能想到的。行数取决于“DB”表中有多少行

 Private Function CheckColumn(SheetN As String, col As String)   
 'checks if a column is in sheet returns errorif not found 
 On Error GoTo NotHere
     CheckColumn = WorksheetFunction.Match(col, Sheets(SheetN).Rows(1), 0)
 On Error GoTo 0

Exit Function
    NotHere:
        MsgBox col & " was not found in sheet " & SheetN    
End Function

Sub MatchNoMatch()
Dim rngDB As Range
Dim colname As String
Dim sh1 As String
Dim sh2 As String
Dim sh3 As String
Dim dbcol As Integer
Dim xmlcol As Integer
Dim rcol As Integer
Dim Firstrow As Long
Dim Lastrow As Long

'column you want to search up
colname = "FNAME"

'the sheets you want to find the column in
sh1 = "DB"
sh2 = "XML"
sh3 = "RESULT"

'Gets the column number from each sheet
dbcol = CheckColumn(sh1, colname)
xmlcol = CheckColumn(sh2, colname)
rcol = CheckColumn(sh3, colname)

'Sets the range for sh1 to do compare
With Worksheets(sh1)
    Firstrow = .UsedRange.Cells(1).Row + 1
    Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
    Set rngDB = .Range(.Cells(Firstrow, dbcol), .Cells(Lastrow, dbcol))
End With

'checks each value against sh2 and writes in sh3. If more values in sh2 they will be ignored
For Each e In rngDB
    If e.Value <> Worksheets(sh2).Cells(e.Row, xmlcol) Then
        Worksheets(sh3).Cells(e.Row, rcol + 1) = "NO MATCH"
    Else
        Worksheets(sh3).Cells(e.Row, rcol + 1) = "MATCH"
    End If
Next e
End Sub

【讨论】:

  • 嗨@blueblook,我执行了你的代码。在“结果”表中,我得到 MATCH 或 NO MATCH 超过 100000 次。我在“FNAME”列下有值“102469”。是不是因为这个?我尝试将“FNAME”下的值更改为“3”,但它仍然在超过 100000 行中显示“MATCH”或 NOMatch。
  • 我修改了代码。这是由于 Set rngDB 的定义方式。数据停止后,FName 中是否有未使用的行?您可以通过按 End+arrow[down] 模拟 .End(xlDown) 来检查这一点。无论数据库表中有多少已使用的行,该代码都将运行。
猜你喜欢
  • 1970-01-01
  • 2017-03-11
  • 2020-01-18
  • 2017-04-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多