将值从一张纸复制到另一张快速方式代码无性能

时间:2018-02-01 12:37:23

标签: excel vba excel-vba

我有以下代码,我想更快地将值从源表复制到目标表。非常感谢。请帮忙!

我的代码并不是真正的高效。复制我的价值观和麦芽汁需要很长时间。感谢您花时间学习我的帖子。

Sub BRM_ID1()
Dim SourceData As Worksheet


Set SourceData = ActiveWorkbook.Worksheets("SZCategoryData")

Dim TailoredData As Worksheet
Set TailoredData = ActiveWorkbook.Worksheets("SZCategory tailored")

Dim i As Long

For i = 2 To 1000
    Dim j As Long

    For j = 4 To 1000

         If SourceData.Cells(i, 6).Value = "BRM_ID" And SourceData.Cells(i, 1).Value = TailoredData.Cells(j, 1) Then
            TailoredData.Cells(j, 2).Value = SourceData.Cells(i, 5)
        ElseIf SourceData.Cells(i, 6).Value = "RELEASE" And SourceData.Cells(i, 1).Value = TailoredData.Cells(j, 1) Then

            TailoredData.Cells(j, 9).Value = SourceData.Cells(i, 5)


        ElseIf SourceData.Cells(i, 6).Value = "PCP TYPE" And SourceData.Cells(i, 1).Value = TailoredData.Cells(j, 1) Then
            TailoredData.Cells(j, 3).Value = SourceData.Cells(i, 5)

        ElseIf SourceData.Cells(i, 6).Value = "RELEASE" And SourceData.Cells(i, 1).Value = TailoredData.Cells(j, 1) Then
            TailoredData.Cells(j, 9).Value = SourceData.Cells(i, 5)

        ElseIf SourceData.Cells(i, 6).Value = "BRM REQ ID" And SourceData.Cells(i, 1).Value = TailoredData.Cells(j, 1) Then
            TailoredData.Cells(j, 4).Value = SourceData.Cells(i, 5)

        ElseIf SourceData.Cells(i, 6).Value = "PCP FLAG 2" And SourceData.Cells(i, 1).Value = TailoredData.Cells(j, 1) Then
            TailoredData.Cells(j, 7).Value = SourceData.Cells(i, 5)
        ElseIf SourceData.Cells(i, 6).Value = "UAT DROP" And SourceData.Cells(i, 1).Value = TailoredData.Cells(j, 1) Then
            TailoredData.Cells(j, 8).Value = SourceData.Cells(i, 5)
        ElseIf SourceData.Cells(i, 6).Value = "1A WORKPACKAGE" And SourceData.Cells(i, 1).Value = TailoredData.Cells(j, 1) Then
            TailoredData.Cells(j, 6).Value = SourceData.Cells(i, 5)

        ElseIf SourceData.Cells(i, 6).Value = "PCP FLAG" And SourceData.Cells(i, 1).Value = TailoredData.Cells(j, 1) Then
            TailoredData.Cells(j, 13).Value = SourceData.Cells(i, 5)

        ElseIf SourceData.Cells(i, 6).Value = "WN TYPE" And SourceData.Cells(i, 1).Value = TailoredData.Cells(j, 1) Then
            TailoredData.Cells(j, 10).Value = SourceData.Cells(i, 5)




End If

    Next
Next

End Sub

2 个答案:

答案 0 :(得分:1)

您是否尝试在运行循环时禁用计算?

Application.Calculation = xlCalculationManual
// Your code
Application.Calculation = xlCalculationAutomatic

每次更新单元格时,整个电子表格都会更新,这使得该过程不具备真正的性能

如果您需要触发手动计算,请调用calculate方法(docs

答案 1 :(得分:0)

首先抱歉我的英语不好。 我认为这可以帮助。但是Vba的表现很难。

  • 首先,您应该减少您的IF声明。

  • 其次,您不应在For循环中声明您的变量。

  • 第三,如果你不想搜索更多的行,那么整数就足够了。你不需要长。

  • 第四,前一位发言者表示使用Application.Calculation

  • 我建议你使用Select case。只是因为清晰度,所以没有差别

这里改编的代码:

Sub BRM_ID1()

    Dim SourceData As Worksheet
    Set SourceData = ActiveWorkbook.Worksheets("SZCategoryData")
    Dim TailoredData As Worksheet
    Set TailoredData = ActiveWorkbook.Worksheets("SZCategory tailored")

    Dim i As Integer
    Dim j As Integer

    Application.Calculation = xlCalculationManual

    For i = 2 To 1000

        For j = 4 To 1000

            If SourceData.Cells(i, 1).Value = TailoredData.Cells(j, 1) Then

                    If SourceData.Cells(i, 6).Value = "BRM_ID" Then

                        TailoredData.Cells(j, 2).Value = SourceData.Cells(i, 5)

                    ElseIf SourceData.Cells(i, 6).Value = "RELEASE" Then

                        TailoredData.Cells(j, 9).Value = SourceData.Cells(i, 5)

                    ElseIf SourceData.Cells(i, 6).Value = "PCP TYPE" Then

                        TailoredData.Cells(j, 3).Value = SourceData.Cells(i, 5)

                    ElseIf SourceData.Cells(i, 6).Value = "RELEASE" Then

                        TailoredData.Cells(j, 9).Value = SourceData.Cells(i, 5)

                    ElseIf SourceData.Cells(i, 6).Value = "BRM REQ ID" Then

                        TailoredData.Cells(j, 4).Value = SourceData.Cells(i, 5)

                    ElseIf SourceData.Cells(i, 6).Value = "PCP FLAG 2" Then

                        TailoredData.Cells(j, 7).Value = SourceData.Cells(i, 5)

                    ElseIf SourceData.Cells(i, 6).Value = "UAT DROP" Then

                        TailoredData.Cells(j, 8).Value = SourceData.Cells(i, 5)

                    ElseIf SourceData.Cells(i, 6).Value = "1A WORKPACKAGE" Then

                        TailoredData.Cells(j, 6).Value = SourceData.Cells(i, 5)

                    ElseIf SourceData.Cells(i, 6).Value = "PCP FLAG" Then

                        TailoredData.Cells(j, 13).Value = SourceData.Cells(i, 5)

                    ElseIf SourceData.Cells(i, 6).Value = "WN TYPE" Then

                        TailoredData.Cells(j, 10).Value = SourceData.Cells(i, 5)

                    End If
            End If

        Next
    Next

    Application.Calculation = xlCalculationAutomatic

End Sub

我希望它有所帮助。问问题。