0

我有一个包含以下“字段”的点列表(列中的每个字段,B 到 G):

点名称 (B)、东距 (C)、北距 (D)、测量人员 (E)、测量日期 (F)、测量方法 (G)

用户必须输入
测量人员 (H2)
测量日期 (I2)
测量方法 (J2)
线 (H4) 点名称 第一部分
开始 (I4)
结束 (J4)

我想要:
- 检查点是否存在
- 如果点存在并且“字段”为空,则使用用户必须在某些特定单元格中输入的信息填充它们
- 如果单元格已经填充以检索信息以显示它在其他一些单元格中
,我来到了这些代码行并且它们可以工作,但是检查过程需要太长时间。

谁能帮我弄清楚如何更快地做到这一点?因为每次执行检查都需要很长时间。

我对此不太擅长,我认为可能有一种更快的方法来做到这一点;欢迎任何意见或建议

    Sub CheckProd()
    Dim FR1, Bin, Track, Min, MinBin, Max, MaxBin, Tre As Integer
    Bin = 10 ^ Range("O2").Value
    Track = Range("H4").Value 'Input value (first part of the point name)
    MinBin = Range("I4").Value ' Input Value (second part of the point name - Start)
    MaxBin = Range("J4").Value ' Input Value (second part of the point name - End)
    If MaxBin > MinBin Then ' calculates first and last point to update
        Min = Bin * Track + MinBin
        Max = Bin * Track + MaxBin
        Else
        Min = Bin * Track + MaxBin
        Max = Bin * Track + MinBin
    End If
    Tre = Max - Min + 1 'Counts number of points to update
    FR1 = Range("B65536").End(xlUp).Row 'Counts total design points points
    Range("K2:M65536").ClearContents
    Check = Min - 1
    For i = 1 To Tre
        Check = Check + 1
        Find = False
        For J = 2 To FR1
            Station = Cells(J, "B").Value
            datte = Cells(J, "F").Value
            If (Check = Station) Then
                Find = True
                If IsEmpty(Cells(J, "F")) Then
                    Cells(J, "E").Value = Cells(2, "H").Value 'Updates Crew number
                    Cells(J, "F").Value = Cells(2, "I").Value 'Updates Survey Date
                    Cells(J, "G").Value = Cells(2, "J").Value 'Updates Survey Method
                Else
                    FRL = Range("K65536").End(xlUp).Row
                    Cells(FRL + 1, "K").Value = Station 'Shows the point already reported
                    Cells(FRL + 1, "L").Value = "Reportado" 'Shows the status "Reported"
                    Cells(FRL + 1, "M").Value = datte ' Shows the date when the point was reported
                End If
            End If
            If ((J = FR1) And (Not Find)) Then
                FRM = Range("K65536").End(xlUp).Row
                Cells(FRM + 1, "K").Value = Check 'Shows the point without design coordinates
                Cells(FRM + 1, "L").Value = "No Preplot" 'Shows the status "No Preplot"
            End If
            If (Find) Then J = FR1
        Next J
    Next i
End Sub
4

1 回答 1

2

直到 For 循环的所有事情都将是超快的。显然,速度命中在您的双 For 循环中。

   For i = 1 To Tre
        Check = Check + 1
        Find = False
        For J = 2 To FR1
              'performance problem happens here...
        Next J
    Next i

代码还不错。

但很明显,您正在通过大量数据进行查找。通过长循环多次执行此操作并不是一件好事,因为您基本上通过大量迭代不断检查单个单元格值而没有什么好处(即查找 3 个值)。

而是考虑使用 VLookup() 或 Index(Match()) 函数替换此“搜索算法”,该函数使用 Cells(J, "B").Value 中的值来查找 Cells(2, "H") 中的 3 个值.Value, Cells(2, "I").Value 和 Cells(2, "J").Value。

涉及代码的更好方法是在开始时将所有值读入数组。为此,首先将数据加载到数组中。好的,您现在不再浪费时间与 Excel 交谈。

Dim arr()
arr = Range("H2:J666").Value2

现在重新编写你的“搜索算法”来处理这个数组。为此,您将重建 For 循环以遍历变量 arr 的元素和维度。IE

For rowCount = 0 to 664
   For columnCount = 0 to 2
       If arr(rowCount, columnCount) = CheckValue(GetStationValue(station)) Then
             ' we have found the correct set of values
             Range("E" & J).Value = arr(rowCount,columnCount)
             Range("F" & J).Value = arr(rowCount,columnCount)
             Range("G" & J).Value = arr(rowCount,columnCount)
        Else
              ' do other update of data
        End If
    Next
Next

' 其中 GetStation 值只是一个单独的函数,用于根据原始站变量值获取动态“站”值(如果需要更新此值,请使用全局变量),然后 CheckValue 将其与您正在使用的校验和进行比较.

希望这可以帮助。

于 2013-05-31T00:05:06.993 回答