VBA查找和格式化方法有2个不同的数据库

时间:2015-09-17 15:55:35

标签: database vba find compare

我有2个不同格式的数据库(drakev_db,kingdom_db),它们是常见的值。我必须比较2个数据库,并在匹配情况下从drakev_db获取UWI代码并将其放入kingdom_db。

问题是: 1.唯一的常用键是坐标,其中100%不匹配 - 我检查了前7个字符 2.“井号”和“井号”的格式不同,带有重音符号,格式为0025或025 3.我有3000个带有UWI代码的数据,我要比较9000个数据,所以我不想手动做

直到现在我才意识到: - 从drakev_db获取第一个X坐标,搜索它 - 保存匹配的单元格行号 与drakev_db的Y坐标相同 - 如果有2个匹配,并且X和Y坐标行号(k和m)等于,那么我将数据放在merged_db

惠特这种方法我发现了955场比赛,但还有很多。 我的问题是:

  1. 我应该使用什么样的搜索方法?
  2. 当我没有共用密钥时,我该怎么办?
  3. 如何格式化名称以便能够相互比较?
  4. 任何想法,脚本? :)

    注意:这是我的第3个宏,第一个帖子,抱歉为业余格式化

        Private Sub CommandButton2_Click()
    
                Set king = ThisWorkbook.Sheets("kingdom_db")
                Set drakev = ThisWorkbook.Sheets("drakev1_db")
                Set merged = ThisWorkbook.Sheets("merged_db")
                Set uwi = ThisWorkbook.Sheets("uwi_gen")
    
                   merged.Range("a3:j5000").Delete
                   uwi.Range("a3:e10000").Delete
    
                         nrow = 2
                         nrow2 = 2
                         uwinb = 520000000231#
    
                    For y = 4 To 9450
    
                                        cell = king.Cells(y, 7).Value
                                        mit = Left$(cell, 7)
                                        mymatch = Array(mit)
    
                                        cell2 = king.Cells(y, 8).Value
                                        mit2 = Left$(cell2, 7)
                                        mymatch2 = Array(mit2)
    
                            With Sheets("drakev1_db").Range("p3:p5430")
    
                            For i = LBound(mymatch) To UBound(mymatch)
                                 Set res = Nothing
                                 Set res = .Find(What:=mymatch, _
                                            After:=.Cells(.Cells.count), _
                                            LookIn:=xlFormulas, _
                                            LookAt:=xlPart, _
                                            SearchOrder:=xlByRows, _
                                            SearchDirection:=xlNext, _
                                            MatchCase:=False)
    
                                    If res Is Nothing Then
                                                      Else: k = res.Row
                                    End If
                            Next i
                            End With
    
    
                               With Sheets("drakev1_db").Range("q3:q5430")
    
                               For l = LBound(mymatch2) To UBound(mymatch2)
                                    Set res2 = Nothing
                                    Set res2 = .Find(What:=mymatch2, _
                                                After:=.Cells(.Cells.count), _
                                                LookIn:=xlFormulas, _
                                                LookAt:=xlPart, _
                                                SearchOrder:=xlByRows, _
                                                SearchDirection:=xlNext, _
                                                MatchCase:=False)
    
                                     If res2 Is Nothing Then
                                                        Else: m = res2.Row
                                     End If
                                Next l
                                End With
    
                                         If res Is Nothing And res2 Is Nothing Then
                                                            nrow2 = nrow2 + 1
                                                            uwinb = uwinb + 1
                                                             uwi.Cells(nrow2, 1).Value = king.Cells(y, 1).Value
                                                             uwi.Cells(nrow2, 2).Value = king.Cells(y, 2).Value
                                                             uwi.Cells(nrow2, 3).Value = king.Cells(y, 7).Value
                                                             uwi.Cells(nrow2, 4).Value = king.Cells(y, 8).Value
    
                                                             uwi.Cells(nrow2, 5).Value = uwinb
    
                                            ElseIf res Is Nothing Or res2 Is Nothing Then
                                                             nrow2 = nrow2 + 1
                                                            uwinb = uwinb + 1
                                                             uwi.Cells(nrow2, 1).Value = king.Cells(y, 1).Value
                                                             uwi.Cells(nrow2, 2).Value = king.Cells(y, 2).Value
                                                             uwi.Cells(nrow2, 3).Value = king.Cells(y, 7).Value
                                                             uwi.Cells(nrow2, 4).Value = king.Cells(y, 8).Value
    
                                                             uwi.Cells(nrow2, 5).Value = uwinb
    
                                           ElseIf Not k = m Then
    
                                           Else: talalat = k
                                                  nrow = nrow + 1
    
                                                      merged.Cells(nrow, 1).Value = king.Cells(y, 1).Value
                                                      merged.Cells(nrow, 2).Value = king.Cells(y, 2).Value
                                                      merged.Cells(nrow, 3).Value = king.Cells(y, 6).Value
                                                      merged.Cells(nrow, 4).Value = king.Cells(y, 7).Value
                                                      merged.Cells(nrow, 5).Value = king.Cells(y, 8).Value
                                                      merged.Cells(nrow, 7).Value = drakev.Cells(talalat, 4).Value
                                                      merged.Cells(nrow, 8).Value = drakev.Cells(talalat, 3).Value
                                                      merged.Cells(nrow, 9).Value = drakev.Cells(talalat, 16).Value
                                                      merged.Cells(nrow, 10).Value = drakev.Cells(talalat, 17).Value
    
    
                                                    End If
    
                        Next
                                                                 merged.Activate
                                                                ActiveSheet.Range("h3:h10000").Select
    
                                                                Selection.NumberFormat = "0"
    
                                                                uwi.Activate
                                                                ActiveSheet.Range("e3:e10000").Select
                                                                Selection.NumberFormat = "0"
    
                                                                ThisWorkbook.Sheets("merged_db").Activate
    
                If Not k = "" Then MsgBox "good job@"
                End Sub
    

0 个答案:

没有答案