2011-04-08 69 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

相關問題