如何解决长时间替换值的问题

时间:2019-04-02 13:16:04

标签: excel vba

我有一个约有45,000行。目前,我正在遍历一列,并以任何值为0的单元格为目标。这些行号存储在数组中。然后,我遍历该数组,并根据数组值更改另一个单元格。我有5000行需要重新分配的值,并且运行该代码段要花费一个多小时(将行号保存到数组仅需要几秒钟)。关于如何使代码运行更快的任何想法?这是代码:

'Initialize array
Dim myArray() As Variant
Dim x As Long

'Looks for the last row on the "Dates" sheet
Dim lastRow As Long
With ThisWorkbook.Sheets("Dates")
    lastRow = .Cells(.Rows.Count, "C").End(xlUp).Row + 1
End With

Dim i As Integer
i = 2

Dim uCounter As Integer
'Loop through all the dates on the "Dates" sheet
While i <= lastRow
    'Each date has a counter next to it
    uCounter = Worksheets("Dates").Range("B" & i).Value
    Dim uDate As String
    'Store the date as a string
    uDate = Worksheets("Dates").Range("C" & i).Value
    Dim startRow As Long, endRow As Long
    'Finds the first and last instance of the date on the CERF Data page (45,000 rows)
    With Sheets("CERF Data")
        startRow = .Range("AN:AN").Find(what:=uDate, after:=.Range("AN1"), LookIn:=xlValues).Row
        endRow = .Range("AN:AN").Find(what:=uDate, after:=.Range("AN1"), LookIn:=xlValues, searchdirection:=xlPrevious).Row
    End With

    Dim j As Long
    For j = startRow To endRow
        'If the cell in column BB is 0, and the counter is above 0 save row number to array, j being the variable representing row number
        If Sheets("CERF Data").Range("BB" & j).Value = 0 And uCounter > 0 Then
            'save row number to array
            ReDim Preserve myArray(x)
            myArray(x) = j
            x = x + 1
            'decrement counter by 1
            uCounter = uCounter - 1
        End If
        If uCounter = 0 Then Exit For
    Next j
i = i + 1
Wend

Dim y As Long
'Loop through the array and assign a value of 2 to all the rows in the array for column AS
For y = LBound(myArray) To UBound(myArray)
    Sheets("CERF Data").Range("AS" & myArray(y)).Value = 2
Next y

谢谢!

1 个答案:

答案 0 :(得分:0)

没有更多信息,这就是我可以给您的信息:

只需1次循环遍历所有行,请检查BB列上的值是否等于0以及日期是否在您的日期范围内:

sourceSets.main.java.srcDirs = ['src']