根据一列中的文本将一列数据分成两部分

时间:2020-05-02 09:32:48

标签: excel vba excel-formula

在我的excel中,C列始终将包含<a href="{% url 'evolucion_paciente' evolucion.id %}" class="btn btn-warning">Ver</a> response文本。我的目标是基于此分隔A:C列。如果C列包含文本resolution,则将A:C列复制到response,否则将E:G复制到A:C

我现在正在使用以下代码:

I:K

当我在 Sub SLACalc() Dim DTA As Workbook Dim SLADATA As Worksheet Set DTA = Excel.Workbooks("main.xlsm") Set SLADATA = DTA.Worksheets("SLA DATA") For i = 2 To SLADATA.Cells(Rows.Count, "A").End(xlUp).Row If InStr(Cells(i, "C").Value, "response") > 0 Then SLADATA.Cells(i, "E").Value = SLADATA.Cells(i, "A").Value SLADATA.Cells(i, "F").Value = SLADATA.Cells(i, "B").Value SLADATA.Cells(i, "G").Value = SLADATA.Cells(i, "C").Value Else SLADATA.Cells(i, "I").Value = SLADATA.Cells(i, "A").Value SLADATA.Cells(i, "J").Value = SLADATA.Cells(i, "B").Value SLADATA.Cells(i, "K").Value = SLADATA.Cells(i, "C").Value End If Next i End Sub 中的行较少时,此方法工作正常。现在我的行​​数接近20,000,并且在Excel中面临很多性能问题。无论如何,我可以改善代码以使其更快地运行。

1 个答案:

答案 0 :(得分:1)

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android = "http://schemas.android.com/apk/res/android" xmlns:app = "http://schemas.android.com/apk/res-auto" xmlns:tools = "http://schemas.android.com/tools" android:layout_width = "match_parent" android:layout_height = "175dp" android:layout_margin = "4dp"> <ImageView android:id = "@+id/imageView" android:layout_width = "0dp" android:layout_height = "0dp" app:layout_constraintBottom_toBottomOf = "parent" app:layout_constraintEnd_toEndOf = "parent" app:layout_constraintStart_toStartOf = "parent" app:layout_constraintTop_toTopOf = "parent" tools:ignore = "ContentDescription" tools:srcCompat = "@tools:sample/avatars" /> </androidx.constraintlayout.widget.ConstraintLayout>

Assuming you want to split the table on the same row as per you code

您可以像这样减少循环代码

First,

For i = 2 To SLADATA.Cells(Rows.Count, "A").End(xlUp).Row If InStr(Cells(i, "C").Value, "response") > 0 Then SLADATA.Range(Cells(i, "E"), Cells(i, "G")).Value = SLADATA.Range(Cells(i, "A"), Cells(i, "C")).Value Else SLADATA.Range(Cells(i, "I"), Cells(i, "K")).Value = SLADATA.Range(Cells(i, "A"), Cells(i, "C")).Value End If Next i

尝试阵列:阵列有助于大幅减少处理时间。

Second

使用this timer;我可以检查处理时间。第一种方法更快。可能是因为,它避免了从数组中存储和检索数据。

但是,如果您只想像罗恩·罗森菲尔德(Ron Rosenfeld)在对问题的评论中所建议的那样使用单独的表,则最好使用自动过滤器。它将比数组更快地工作。

Sub SLACalc2()
    Dim DTA As Workbook
    Dim SLADATA As Worksheet
    Set DTA = Excel.Workbooks("main.xlsm")
    Set SLADATA = DTA.Worksheets("SLA DATA")
    LRow = SLADATA.Cells(Rows.Count, "A").End(xlUp).Row
    DataArr = SLADATA.Range("A2:C" & LRow).Value

    For i = 1 To UBound(DataArr)
        If Application.Index(DataArr, i, 3) = "response" Then
            SLADATA.Range(Cells(i + 1, "E"), Cells(i + 1, "G")).Value = Application.Index(DataArr, i)
         Else
            SLADATA.Range(Cells(i + 1, "I"), Cells(i + 1, "K")).Value = Application.Index(DataArr, i)
        End If
    Next i

End Sub
相关问题