运行超过500K行的最佳方式?

问题描述 投票:0回答:2

我有一个包含一堆行的文件,其中包含来自不同配置的某些部件号的数据。这些零件号中的一些在整个文件中重复,而在那些重复的零件号中可能包含某些数据,而有些则可能没有。我正在尝试找到确定某些数据文件通用性的最佳方法。因此,对于通用性而言,如果一行具有一个值,另一行为空白,则非空白行的值将被放入空白行。并且,如果这两行的数据不同,则会更改单元格上的字体颜色,指示该部件号具有两个不同的唯一值,应进行检查。

Dim i, j, n As Long
Dim lr As Long
Dim moaf As Workbook
Dim sht As Worksheet

Application.ScreenUpdating = False

Set moaf = Workbooks("MOAF3.xlsb")
Set sht = moaf.Worksheets("Wire Data")

n = InputBox("What column # are you trying to fill in?: ")
lr = Cells(Rows.count, 2).End(xlUp).Row

For i = 2 To lr
    lkup = Cells(i, 2).Value 'sets first lookup value
    Fill = Cells(i, n).Value 'sets the first data value to compare
    If Len(Fill) > 0 Then
        For j = 2 To lr
            lkup2 = Cells(j, 2).Value 'sets the second lookup value
            Fill2 = Cells(j, n).Value 'sets the second value to compare
            If lkup2 = lkup Then 'checks to see if p/ns are same
                If Len(Fill2) = 0 Then 'checks to see if second value is blank
                    Cells(j, n).Value = Fill 'if value is blank the cell takes value of non blank     cell
                ElseIf Fill <> Fill2 Then 'checks to see if the values are non matching and non zero
                    Cells(i, n).Font.ColorIndex = 3 'changes font color of two cells
                    Cells(j, n).Font.ColorIndex = 3 'changes font color of two cells
                End If
            End If
        Next j
    End If
Next i
Application.ScreenUpdating = True
End Sub

这样做通常会冻结我的excel,因为我的计算机具有32GB的RAM,并且是Windows10。是否有解决我的问题的更好方法,还是可以在不使用vb​​a的情况下完成?我已经在不使用vb​​a的情况下对方法进行了一些研究,但使用了sumifs,countifs,但实际上并没有进行任何深入研究。

excel vba large-data large-files
2个回答
0
投票

因此,如果我正确理解了您的问题,请从以下数据开始:

ID Column_header
 2             a
 3       _BLANK_
 4       _BLANK_
 5             b
 6       _BLANK_

您想把它变成:

ID Column_header
 2             a
 3             a
 4             a
 5             b
 6             b

我知道一个非常简单的技巧(我已将所有内容放在“ A”列中进行解释):

  • 选择该列中的每个单元格
  • 转到(Ctrl + G)特殊,空白
  • 在编辑栏中,键入=A2(您当前位于'A3'中,并且您要在其中复制单元格上方的单元格的值)
  • Ctrl + ENTER

[您会看到'A2'被复制为'A3','A3'被复制为'A4','A5'被复制为'A6'(这是对all空白单元格完成的事实,这是由于到Ctrl + ENTER)。

将其记录到宏中,它将更快。

我已经看到一个问题弹出:“好吧,但是我想更改字体颜色呢?”。好吧,新填充的单元格基于公式,因此=FORMULATEXT()的长度不会为零。您以此为条件格式的基础。

祝你好运


0
投票

内部for循环只需要从i开始,即:

for j = i to lr

这应该大约是运行时间的一半。

© www.soinside.com 2019 - 2024. All rights reserved.