为什么宏在某一点后停止复制所有行数据?

时间:2019-02-27 19:04:01

标签: excel vba

我已经编码了一个Excel宏,该宏从csv文件导入数据,然后根据检查值复制行,并将解析后的数据放入单独的工作表中。我正在检查12个值和前9个工作,但是一旦到达10、11和12,宏将仅复制1行。这是我的代码有问题还是这是excel的局限性?如果这是我的代码,该怎么调整?

顶部模块:

Sub Import_Parse_Refresh()
'Import Data CSV
    Call GetCSVList

'Parse Data Based on Report ID
    Call Data_Parse_All

'Refresh Each Pivot Table
    Call TableRefresh

'Delete Imported_Data that was created during the import
    Sheets("Imported_Data").Delete
    Sheets("Begin").Delete

'Save File As
    Call SaveFile

End Sub

Data_Parse_All模块:

Sub Data_Parse_All()

    Call Data_Parse_1
    Call Data_Parse_2
    Call Data_Parse_3
    Call Data_Parse_4
    Call Data_Parse_5
    Call Data_Parse_6
    Call Data_Parse_7
    Call Data_Parse_8
    Call Data_Parse_9
    Call Data_Parse_10
    Call Data_Parse_11
    Call Data_Parse_12

End Sub

Data_Parse_9-此代码用于所有12个Data_Parse_#模块,但只有1到9可以正常工作:

Sub Data_Parse_9()
'
    Sheets("Imported_Data").Select
    RowCount = Cells(Cells.Rows.Count, "I").End(xlUp).Row
    For i = 1 To RowCount
        Range("I" & i).Select
        check_value = ActiveCell
        If check_value = "9" Then
            ActiveCell.EntireRow.Cut
            Sheets("Report 9").Select
            RowCount = Cells(Cells.Rows.Count, "A").End(xlUp).Row
            Range("A" & RowCount + 1).Select
            ActiveSheet.Paste
            Sheets("Imported_Data").Select
        End If
    Next

End Sub

Data_Parse_10-代码相同,但这是仅复制一行的情况

Sub Data_Parse_10()
'
' Macro1_Data Macro
'
'assuming the data is in sheet1
    Sheets("Imported_Data").Select
    RowCount = Cells(Cells.Rows.Count, "I").End(xlUp).Row
    For i = 1 To RowCount
        Range("I" & i).Select
        check_value = ActiveCell
        If check_value = "10" Then
            ActiveCell.EntireRow.Cut
            Sheets("Report 10").Select
            RowCount = Cells(Cells.Rows.Count, "A").End(xlUp).Row
            Range("A" & RowCount + 1).Select
            ActiveSheet.Paste
            Sheets("Imported_Data").Select
        End If
    Next

End Sub

2 个答案:

答案 0 :(得分:2)

我敢打赌,返回不良结果的代码与将To RowCount循环的For-Next参数重置为循环中间的其他值(可能更小)有关。例如,如果Column A中的Sheets("Report 10")为空,则RowCount将被重置为1,从而在第一次迭代后退出循环。另外,如@urdearboy所述,您可以将其合并为一个动态循环。我会尝试类似的

Sub Data_Parse_All()
    Dim i As Long
    Dim Rowcount As Long
    Dim PasteRow As Long

    With Sheets("Imported_Data")
        Rowcount = .Cells(.Cells.Rows.Count, "I").End(xlUp).Row
        For i = 1 To Rowcount
            If .Range("I" & i) >= 1 And .Range("I" & i) <= 12 Then
                PasteRow = Sheets("Report " & .Range("I" & i)).Range("I" & Rows.Count).End(xlUp).Row + 1
                .Range("I" & i).EntireRow.Cut Sheets("Report " & .Range("I" & i)).Range("A" & PasteRow)
            End If
        Next i
    End With
End Sub

答案 1 :(得分:2)

认为,您可以将所有Data_Parse_#个子折叠到一个子中。这将使用check_value并将其用于获取目标工作表。

Sub data_Parse()
Dim rowCount As Long, i As Long, newRowCount As Long
Dim check_value As String
Dim destSheet As Worksheet


With ThisWorkbook.Sheets("Imported_Data")
    rowCount = .Cells(Rows.Count, "I").End(xlUp).row
    For i = 1 To rowCount
        check_value = .Cells(i, "I").Value
        Set destSheet = ThisWorkbook.Sheets("Report " & check_value)
        newRowCount = destSheet.Cells(Rows.Count, "A").End(xlUp).row
        .Rows(i).EntireRow.Cut
        destSheet.Range("A" & newRowCount + 1).Paste
        Application.CutCopyMode = False
    Next i
End With ' .Sheets("Imported_Data")
End Sub

(编辑:我知道使用.Delete,在循环(For i = rowCount to 1 Step -1时应该倒退,但是我不确定.Cut是否需要这样做,所以只需确保所有行占)。