![](/img/trans.png)
[英]Code to compare each cell in a column to every cell in another column
[英]How to check values in each cell from one column to each cell in another column
我有2組值,我需要從2列中突出顯示常用值(字母數字)。 行數超過50,000行。 有什么辦法編寫代碼嗎? 基本上,我需要從A2到A59000的Col I的每個單元格檢查
一個想法:使用VBScript字典避免Rows * Rows循環和用於實驗的模塊:
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.