2011-04-08 91 views
0

我得到了2组值,我需要突出显示来自2列的常用值(字母数字)。行数超过50,000行。任何方式来编写它的代码?基本上,我需要检查从柱A的每个细胞免受山口我每个细胞从A2到A59000如何检查每个单元格中的值从一列到另一列中的每个单元格

+0

只是为了澄清,如果两列中都存在一个值,则要在两列中突出显示它。如果它只存在于一列中,则不做任何处理。那是对的吗? – Kevin 2011-04-08 20:35:46

+0

你有多少个不同的数值(估计)?列是否已分类?你写了暴力代码(2个嵌套循环)吗? – 2011-04-08 20:42:39

回答

0

一个想法:使用VBScript解释,以避免行*行环圈,并为您的实验模块:

Attribute VB_Name = "Module1" 
' needs Reference to Microsoft Scripting Runtime (for Dictionary) 

Option Explicit 

Const cnRows = 1000 
Const cnLChar = 80 
Const cnFNum = 1000 
Const cnLNum = 1100 

Function IntRange(iFrom, iTo) 
    IntRange = iFrom + Fix((iTo - iFrom) * Rnd()) 
End Function 

Sub fill() 
    Dim sngStart As Single 
    sngStart = Timer 
    Dim sheetTest As Worksheet 
    Set sheetTest = Sheet2 
    Dim nRow, nCol 
    For nRow = 1 To cnRows 
     For nCol = 1 To 2 
      sheetTest.Cells(nRow, nCol) = Chr(IntRange(65, cnLChar)) & IntRange(cnFNum, cnLNum) 
     Next 
    Next 
    With sheetTest.Cells.Interior 
     .ColorIndex = 0 
     .Pattern = xlSolid 
     .PatternColorIndex = xlAutomatic 
    End With 
    sheetTest.Cells(nRow, 1) = Timer - sngStart 
End Sub 

Sub bruteForce() 
    Dim sngStart As Single 
    sngStart = Timer 
    Dim sheetTest As Worksheet 
    Set sheetTest = Sheet2 
    Dim nRow1 As Integer 
    Dim nRow2 As Integer 
    For nRow1 = 1 To cnRows 
     For nRow2 = 1 To cnRows 
      If sheetTest.Cells(nRow1, 1) = sheetTest.Cells(nRow2, 2) Then 
       With sheetTest.Cells(nRow1, 1).Interior 
        .ColorIndex = 8 
        .Pattern = xlSolid 
        .PatternColorIndex = xlAutomatic 
       End With 
      End If 
     Next 
    Next 
    sheetTest.Cells(nRow1, 1) = Timer - sngStart 
End Sub 

Sub useDict() 
    Dim sngStart As Single 
    sngStart = Timer 
    Dim sheetTest As Worksheet 
    Set sheetTest = Sheet2 
    Dim dicElms As New Scripting.Dictionary 
    Dim nRow As Integer 
    For nRow = 1 To cnRows 
     dicElms(sheetTest.Cells(nRow, 1).Text) = 0 
    Next 
    For nRow = 1 To cnRows 
     If dicElms.Exists(sheetTest.Cells(nRow, 2).Text) Then 
      With sheetTest.Cells(nRow, 2).Interior 
       .ColorIndex = 8 
       .Pattern = xlSolid 
       .PatternColorIndex = xlAutomatic 
      End With 
     End If 
    Next 
    sheetTest.Cells(nRow, 2) = Timer - sngStart 
End Sub 
+0

@kevin我得到了14行58000行,其中有2列(Col A和Col I)具有相同的值(唯一的)但顺序不同(Col A-H如set 1,Col I-O如Set 2)。现在我需要突出显示Col A和Col I的值。我有样品值 Col A Col B Os.20617.1.S1_at Os.20617.1.S1_at Os.12345.1.S1_s_at Os.27061.2.A1_x_at Os.20572.2 .S1_at Os.54653.1.S1_at Os.10435.1.S1_at Os.47388.1.S1_s_at 估计的共同价值是从48000 58000行 – solomon 2011-04-09 08:08:42

+0

@ Ekkehard.Homer:我看你想测试你的代码的运行。你知道它的大部分将是由于你循环单元格,对吧?而且你可以通过将整个范围加载到Variant数组中并在那里进行处理来将代码速度提高一个数量级+另外一次你应该访问单元格是为相关的单元格着色。 – 2011-04-10 10:47:59

+0

@ Jean-FrançoisCorbett:由于第一个循环只访问一次单元值,将其放入字典中,第二个循环无法避免,运行时间差异按zilch顺序排列。 – 2011-04-10 12:37:57

相关问题