比较4列中的数据

时间:2016-08-31 08:51:18

标签: excel vba excel-vba excel-formula excel-2007

我是VB新手我需要比较两列的地址。

如果两个地址匹配,则检查两个数据是否匹配,如果不同时将源文件和转储文件地址和数据输出到文本文件。

如果在C列中找不到A列中的地址,则将源文件地址和数据输出到文本文件。

如果在A列中找不到C列中的地址,则将转储文件地址和数据输出到文本文件。

希望有人可以帮助我。谢谢!

  Source File           Dump File  
      a         b          c        d
1  address    data      address    data
2   s100       a         s010       x
3   S010       x         s020       b
4   S030       y         S030       y
5   s040       z         S040       d

一个问题

如果地址匹配,我认为它不会检查两个地址的数据是否相同。例如,源文件的地址为's040',数据为'z',但转储文件的地址为's040',数据为'd'

时间问题

需要很长时间,因为有900万次迭代。首先删除重复项然后运行此搜索更好吗?我尝试使用excel的删除重复功能,但它只适用于一列。整个周期需要25分钟。

如果连续地址在列表中是唯一的,则组合数据:

如果一列中有一大块唯一地址,我需要找到第一个唯一地址的起始地址和最后一个唯一地址 只输出那些并非所有FF的行:

'If all the data are FF's output like this
File: dump.s19
0x006180 – 0x007E8F
[Result] OK

'Here certain lines are all FF's which are not displayed here, only Non FF's lines need to printed as follows.
File: dump.s19
0x007EB0 – 0x00FFFF
S224007FF0FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF4247494EFFFFFFFFFFFFFFFF0000FFFF66
S224008010FFFFFFFF01019D160825A100100201FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFCA

S224008050302D4100E0FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA8
S224008070FFFFFFFFFFFFFFFFFFFFA3BF454E442EFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF9E
Result: NOK

我不确定如何将这个额外的逻辑集成到我的代码中。

'Set the address you're trying to find
            fa = Range(sf & cr).Value
            fa_data = Range(Chr(Asc(sf) + 1) & cr).Value
            Debug.Print "fa" & fa
'Find it
            Set targetcell = Range(si & 3 & ":" & si & lr_2).Find(What:=fa, LookIn:=xlValues, _
                SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=True)
'If Nothing is returned it is not found
            If targetcell Is Nothing Then
'Write your search cell and it's ajacent to your file.
                If l = 1 Then

                    startadd = Range(sf & cr).Value
                    If startadd <> "FFFFFF" Then

                        Dec_startadd = Val("&H" & startadd & "&H")
                        lgth = Len(Range(Chr(Asc(sf) + 1) & cr)) - 2
                        lgth = lgth / 2 - 1
                        endadd = Hex(Dec_startadd + lgth)
                        endadd = Right("000000" & endadd, 6)

                        Print #fn, "File:" & orig_filename
                        Print #fn, "0x" & startadd & " - 0x" & endadd
                        Print #fn, Range(Chr(Asc(sf) - 1) & cr).Value & Range(sf & cr).Value & Range(Chr(Asc(sf) + 1) & cr).Value
                        Print #fn, "Result: NOK"
                        Print #fn,
                        Print #fn,    

1 个答案:

答案 0 :(得分:1)

我为了它的乐趣而鞭打了它:

'Get a the next available file number
    fn = FreeFile
'Open your file ready for writing.
    Open "your full path and file name" For Output As #fn
'Set the First row to search from.
    fr = 2
'Find the last row.
    lr = ActiveCell.SpecialCells(xlLastCell).Row
'Set the column for the value that you are searching for.
    sf = "A"
'Set the column for that you are searching in.
    si = "C"
'You want to search two columns
    For l = 1 To 2
'Loop from first row to the last row.
        For cr = fr To lr
'Set the address you're trying to find
            fa = Range(sf & cr).Value
'Find it
            Set targetcell = Range(si & fr & ":" & si & lr).Find(What:=fa, LookIn:=xlValues, _
                SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False)
'If Nothing is returned it is not found
            If targetcell Is Nothing Then
'Write your search cell and it's ajacent to your file.
                Write #fn, Range(sf & cr).Value & "," & Range(Chr(Asc(sf) + 1) & cr).Value
            End If
'I always put a DoEvents in a loop; just in case you need to break out of it.
            DoEvents
        Next
'Now you've done one column swap them over and do it again.
        sf = "C"
        si = "A"
    Next
'It's done.
    Close #fn