Excel 将文件的每一行与第二个文件中的数千行进行比较时,提高速度

Excel 将文件的每一行与第二个文件中的数千行进行比较时,提高速度,excel,vba,Excel,Vba,我有两个Excel文件。在第一个文件中,我有485行,在第二个文件中,有10700行。对于第一个文件中的每一行,我比较第二个文件中每一行的两个值 例如,对于第一个文件的第一行,我将其与第二个文件的10700行进行比较,并对第一个文件的每一行进行比较 我测试了第一个文件的前三行,需要五分钟 如何提高程序的速度 我有一个intel i7-6600U 2.6Ghz的16Go ram Sub test() Dim sht As Worksheet 'Derniere ligne des fichie

我有两个Excel文件。在第一个文件中,我有485行,在第二个文件中,有10700行。对于第一个文件中的每一行,我比较第二个文件中每一行的两个值

例如,对于第一个文件的第一行,我将其与第二个文件的10700行进行比较,并对第一个文件的每一行进行比较

我测试了第一个文件的前三行,需要五分钟

如何提高程序的速度

我有一个intel i7-6600U 2.6Ghz的16Go ram

Sub test()

Dim sht As Worksheet

'Derniere ligne des fichiers
Dim LastRowPosa As Long
Dim LastRowBio As Long

'Cellules des fichiers
Dim rngPosaSejour As Range
Dim rngPosaDate As Range
Dim rngBioSejour As Range
Dim rngBioDate As Range

'item trouvé
Dim itemFound As Boolean

Dim cheminFichier As String

Dim datesEquals As Boolean
Dim sejourEquals As Boolean
Dim isAlbumine As Boolean

Dim tgo As String

'Variables incrémentielles
Dim i As Integer
Dim j As Integer
'Application Excel pour stocker le fichier BIO
Dim XL As Excel.Application
Dim WBK As Excel.Workbook

filePath= "C:\Users\me\Downloads\biologie.xls"

'New xl App
Set XL = CreateObject("Excel.Application")

'Loading the two files
Set WBK = XL.Workbooks.Open(filePath)
Set sht = ActiveSheet

'Get last row of each files
LastRowPosa = sht.Range("A1").CurrentRegion.Rows.Count
LastRowBio = WBK.Sheets("Sheet 1").Range("A1").CurrentRegion.Rows.Count

tgo = "Albumine"

For i = 2 To 3
    Set rngPosaSejour = Application.Range("B" & i)
    Set rngPosaDate = Application.Range("P" & i)

    For j = 2 To LastRowBio
        If WBK.Sheets("Sheet 1").Range("I" & j) = tgo Then    
            Set rngBioSejour = WBK.Sheets("Sheet 1").Range("A" & j)
            Set rngBioDate = WBK.Sheets("Sheet 1").Range("C" & j)
            sejourEquals = rngPosaSejour.Value = rngBioSejour.Value
            datesEquals = Format(rngBioDate, "dd/mm/yyyy") = Format(rngPosaDate, "dd/mm/yyyy")
            isAlbumine = tgo = WBK.Sheets("Sheet 1").Range("C" & j)
            If sejourEquals And datesEquals Then
                sht.Range("I" & i).Value = WBK.Sheets("Sheet 1").Range("j" & j)
            End If    
        End If
    Next j
Next i

End Sub

尝试用以下内容替换主循环:

Application.Calculation = xlCalculationManual

For i = 2 To 3
    valPosaSejour = Application.Range("B" & i).Value2
    valPosaDate = Application.Range("P" & i).Value2

    For j = 2 To LastRowBio
        With WBK.Sheets("Sheet 1")
        If .Range("I" & j) = tgo Then
            valBioSejour = .Range("A" & j).Value2
            valBioDate = .Range("C" & j).Value2
            isAlbumine = tgo = .Range("C" & j).Value2
            If (valPosaSejour = valBioSejour) And (valBioDate = rngPosaDate) Then
                sht.Range("I" & i).Value = .Range("j" & j)
            End If
        End If
        End With
    Next j
Next i

Application.Calculation = xlCalculationAutomatic

不要设置处理范围(在每种情况下只使用一次),只需加载值并进行比较。

尝试用以下内容替换主循环:

Application.Calculation = xlCalculationManual

For i = 2 To 3
    valPosaSejour = Application.Range("B" & i).Value2
    valPosaDate = Application.Range("P" & i).Value2

    For j = 2 To LastRowBio
        With WBK.Sheets("Sheet 1")
        If .Range("I" & j) = tgo Then
            valBioSejour = .Range("A" & j).Value2
            valBioDate = .Range("C" & j).Value2
            isAlbumine = tgo = .Range("C" & j).Value2
            If (valPosaSejour = valBioSejour) And (valBioDate = rngPosaDate) Then
                sht.Range("I" & i).Value = .Range("j" & j)
            End If
        End If
        End With
    Next j
Next i

Application.Calculation = xlCalculationAutomatic

不要设置处理范围(在每种情况下只使用一次),只需加载值并进行比较。

使用变量数组更快

Sub test()

Dim sht As Worksheet

'Derniere ligne des fichiers
Dim LastRowPosa As Long
Dim LastRowBio As Long

'Cellules des fichiers
Dim rngPosaSejour As Range
Dim rngPosaDate As Range
Dim rngBioSejour As Range
Dim rngBioDate As Range

'item trouve
Dim itemFound As Boolean

Dim cheminFichier As String

Dim datesEquals As Boolean
Dim sejourEquals As Boolean
Dim isAlbumine As Boolean

Dim tgo As String

'Variables incrementielles
Dim i As Integer
Dim j As Integer
'Application Excel pour stocker le fichier BIO
Dim XL As Excel.Application
Dim WBK As Excel.Workbook


Set sht = ActiveSheet '<~ set sht first

filePath = "C:\Users\me\Downloads\biologie.xls"

'New xl App
Set XL = CreateObject("Excel.Application")

'Loading the two files

Set WBK = XL.Workbooks.Open(filePath)

Dim vDB As Variant, vData As Variant
Dim vR() As Variant

'Get last row of each files
'LastRowPosa = sht.Range("A1").CurrentRegion.Rows.Count
vDB = sht.Range("A1").CurrentRegion
'LastRowBio = WBK.Sheets("Sheet 1").Range("A1").CurrentRegion.Rows.Count
vData = WBK.Sheets("Sheet 1").Range("A1").CurrentRegion
ReDim vR(1 To UBound(vDB, 1), 1 To 1)
tgo = "Albumine"

For i = 2 To UBound(vDB, 1) '3
    'Set rngPosaSejour = Application.Range("B" & i)
    'Set rngPosaDate = Application.Range("P" & i)
    For j = 2 To UBound(vData, 1) 'LastRowBio
        'If WBK.Sheets("Sheet 1").Range("I" & j) = tgo Then
            If vData(j, 9) = tgo Then
            'Set rngBioSejour = WBK.Sheets("Sheet 1").Range("A" & j)
            'Set rngBioDate = WBK.Sheets("Sheet 1").Range("C" & j)
                If vDB(i, 2) = vData(j, 1) And vDB(i, 16) = vData(j, 3) Then
                    'sejourEquals = rngPosaSejour.Value = rngBioSejour.Value
                    'datesEquals = Format(rngBioDate, "dd/mm/yyyy") = Format(rngPosaDate, "dd/mm/yyyy")
                    'isAlbumine = tgo = WBK.Sheets("Sheet 1").Range("C" & j)
                    'If sejourEquals And datesEquals Then
                    '    sht.Range("I" & i).Value = WBK.Sheets("Sheet 1").Range("j" & j)
                    'End If
                    vR(i, 9) = vData(j, 10)
                End If
            End If
        End If
    Next j
Next i
sht.Range("i1").Resize(UBound(vR, 1)) = vR
End Sub
子测试()
将sht变暗为工作表
“德涅尔菲舍尔酒店
暗淡的最后一行波萨一样长
黯淡的拉斯洛比奥一样长
“菲舍尔细胞
变暗rngPosaSejour As范围
Dim rngPosaDate作为范围
暗rngBioSejour As范围
碘酸钠作为量程
"鳟鱼",
Dim项被发现为布尔值
像绳子一样暗淡的化学纤维
Dim datesEquals作为布尔值
作为布尔值的Dim sejourEquals
作为布尔值的弱isalbumin
将tgo设置为字符串
'Variables incrementieles
作为整数的Dim i
作为整数的Dim j
应用Excel倾倒stocker le fichier BIO
Dim-XL作为Excel.Application
将WBK设置为Excel.工作簿

使用variant数组设置sht=ActiveSheet'更快

Sub test()

Dim sht As Worksheet

'Derniere ligne des fichiers
Dim LastRowPosa As Long
Dim LastRowBio As Long

'Cellules des fichiers
Dim rngPosaSejour As Range
Dim rngPosaDate As Range
Dim rngBioSejour As Range
Dim rngBioDate As Range

'item trouve
Dim itemFound As Boolean

Dim cheminFichier As String

Dim datesEquals As Boolean
Dim sejourEquals As Boolean
Dim isAlbumine As Boolean

Dim tgo As String

'Variables incrementielles
Dim i As Integer
Dim j As Integer
'Application Excel pour stocker le fichier BIO
Dim XL As Excel.Application
Dim WBK As Excel.Workbook


Set sht = ActiveSheet '<~ set sht first

filePath = "C:\Users\me\Downloads\biologie.xls"

'New xl App
Set XL = CreateObject("Excel.Application")

'Loading the two files

Set WBK = XL.Workbooks.Open(filePath)

Dim vDB As Variant, vData As Variant
Dim vR() As Variant

'Get last row of each files
'LastRowPosa = sht.Range("A1").CurrentRegion.Rows.Count
vDB = sht.Range("A1").CurrentRegion
'LastRowBio = WBK.Sheets("Sheet 1").Range("A1").CurrentRegion.Rows.Count
vData = WBK.Sheets("Sheet 1").Range("A1").CurrentRegion
ReDim vR(1 To UBound(vDB, 1), 1 To 1)
tgo = "Albumine"

For i = 2 To UBound(vDB, 1) '3
    'Set rngPosaSejour = Application.Range("B" & i)
    'Set rngPosaDate = Application.Range("P" & i)
    For j = 2 To UBound(vData, 1) 'LastRowBio
        'If WBK.Sheets("Sheet 1").Range("I" & j) = tgo Then
            If vData(j, 9) = tgo Then
            'Set rngBioSejour = WBK.Sheets("Sheet 1").Range("A" & j)
            'Set rngBioDate = WBK.Sheets("Sheet 1").Range("C" & j)
                If vDB(i, 2) = vData(j, 1) And vDB(i, 16) = vData(j, 3) Then
                    'sejourEquals = rngPosaSejour.Value = rngBioSejour.Value
                    'datesEquals = Format(rngBioDate, "dd/mm/yyyy") = Format(rngPosaDate, "dd/mm/yyyy")
                    'isAlbumine = tgo = WBK.Sheets("Sheet 1").Range("C" & j)
                    'If sejourEquals And datesEquals Then
                    '    sht.Range("I" & i).Value = WBK.Sheets("Sheet 1").Range("j" & j)
                    'End If
                    vR(i, 9) = vData(j, 10)
                End If
            End If
        End If
    Next j
Next i
sht.Range("i1").Resize(UBound(vR, 1)) = vR
End Sub
子测试()
将sht变暗为工作表
“德涅尔菲舍尔酒店
暗淡的最后一行波萨一样长
黯淡的拉斯洛比奥一样长
“菲舍尔细胞
变暗rngPosaSejour As范围
Dim rngPosaDate作为范围
暗rngBioSejour As范围
碘酸钠作为量程
"鳟鱼",
Dim项被发现为布尔值
像绳子一样暗淡的化学纤维
Dim datesEquals作为布尔值
作为布尔值的Dim sejourEquals
作为布尔值的弱isalbumin
将tgo设置为字符串
'Variables incrementieles
作为整数的Dim i
作为整数的Dim j
应用Excel倾倒stocker le fichier BIO
Dim-XL作为Excel.Application
将WBK设置为Excel.工作簿

设置sht=ActiveSheet'如果没有任何代码,没有人能够提供帮助。VBA相对较慢。用C#、Java或汇编重写。可能更适合,但请确保包含您正在使用的代码。我共享了编解码器#,Java已经被提到。我要说的是,一种为比较和交叉引用表格数据而优化的语言当然是SQL。如果没有任何代码,没有人能够提供帮助。VBA的速度相对较慢。用C#、Java或汇编重写。可能更适合,但请确保包含您正在使用的代码。我共享了编解码器#,Java已经被提到。我要说的是,为比较和交叉引用表格数据而优化的语言当然是SQL。太棒了!谢谢让前面的代码进行注释可以很好地将差异可视化,无论是在降低执行速度的语句方面,还是在可读性/代码长度方面+1.太好了!谢谢让前面的代码进行注释可以很好地将差异可视化,无论是在降低执行速度的语句方面,还是在可读性/代码长度方面+1.