Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Excel 我如何找到我的代码的哪一部分在循环过程中没有改变复制部分?_Excel_Vba_Loops_Datatable - Fatal编程技术网

Excel 我如何找到我的代码的哪一部分在循环过程中没有改变复制部分?

Excel 我如何找到我的代码的哪一部分在循环过程中没有改变复制部分?,excel,vba,loops,datatable,Excel,Vba,Loops,Datatable,代码执行到完成,但我不会在下一个循环之前重置任何内容,生成的表与第一张表完全相同! 我哪里做错了 也许,与pivot缓存有关 我使用的文档。您必须将此行中的变量WS\u Count更改为I: 原件: wsA.ListObjects.Add(SourceType:=xlSrcRange, _ Source:=Selection.CurrentRegion, _ xlListObjectHasHeaders:=xlYes _ ).Name = "myTable" &

代码执行到完成,但我不会在下一个循环之前重置任何内容,生成的表与第一张表完全相同! 我哪里做错了

也许,与pivot缓存有关


我使用的文档。

您必须将此行中的变量WS\u Count更改为I:

原件:

wsA.ListObjects.Add(SourceType:=xlSrcRange, _
    Source:=Selection.CurrentRegion, _
    xlListObjectHasHeaders:=xlYes _
    ).Name = "myTable" & WS_Count
用于:

尽我的时间清理代码:

Sub UnpivotData()

    'downloaded from contextures.com
    'code to unpivot named Excel table
    'uses first table on the sheet,
    'if more than one table
    Dim myList As ListObject
    Dim NumCols As Long
    Dim PT01 As PivotTable
    Dim wbA As Workbook
    Dim wbNew As Workbook
    Dim wsA As Worksheet
    Dim wbm As Worksheet
    Dim wsNew As Worksheet
    Dim wsPT As Worksheet
    Dim wsNewData As Worksheet
    Dim myData As Range
    Dim mySep As String
    Dim myJoin As String
    Dim ColStart As Long
    Dim ColEnd As Long
    Dim ColCount As Long
    Dim RowStart As Long
    Dim RowEnd As Long
    Dim RowCount As Long
    Dim DataStart As Range
    Dim DataEnd As Range
    Dim iCol As Long
    Dim myFormula As String
    Dim msgSep As String
    Dim msgLabels As String
    Dim msgEnd As String

    Dim wsCounter As Integer

    On Error GoTo errHandler

    ' Reference the current workbook
    Set wbA = ThisWorkbook

    ' Define current separator
    mySep = "|"

    'join operator for Excel formulas
    myJoin = "&"

    ' Set first columns that wont be unpivoted
    NumCols = 7

    ' Loop through the current workbook sheets
    For Each wsA In wbA.Worksheets

        ' Set a worksheet counter
        wsCounter = wsCounter + 1

        ' Convert current region to table / listobject
        wsA.ListObjects.Add(SourceType:=xlSrcRange, _
        Source:=wsA.Cells.CurrentRegion, _
        xlListObjectHasHeaders:=xlYes _
        ).Name = "myTable" & wsCounter

        ' Copy worksheet to new file and set a reference
        wsA.Copy
        Set wbNew = ActiveWorkbook
        Set wsNew = wbNew.Sheets(1)

        ' Reference the table / listobject in the new file
        Set myList = wsNew.ListObjects(1)

        With myList
            ColStart = .HeaderRowRange.Columns(1).Column
            RowStart = .HeaderRowRange.Columns(1).Row
            RowCount = .DataBodyRange.Rows.Count
            RowEnd = .DataBodyRange.Rows(RowCount).Row
            'insert column for the combined labels
            wsNew.Columns(NumCols + ColStart).Insert Shift:=xlToRight
            ColCount = .DataBodyRange.Columns.Count
            ColEnd = .DataBodyRange.Columns(ColCount).Column
        End With

        'build formula to combine labels
        myFormula = "=("
            For iCol = 1 To NumCols
            myFormula = myFormula & "[@" _
            & myList.HeaderRowRange(1, iCol).Value _
            & "]" & myJoin & Chr(34) _
            & mySep & Chr(34) & myJoin
        Next iCol

        myFormula = Left(myFormula, Len(myFormula) - 5)
        myFormula = myFormula & ")"

        With myList
            .DataBodyRange.Cells(1, NumCols + 1).Formula = myFormula
            .DataBodyRange.Columns(NumCols + 1).Value _
            = .DataBodyRange.Columns(NumCols + 1).Value
            Set DataStart = .HeaderRowRange(1, NumCols + 1)
        End With

        Set DataEnd = wsNew.Cells(RowEnd, ColEnd)
        Set myData = wsNew.Range(DataStart, DataEnd)

        'create multiple consolidation pivot table
        wbNew.PivotCaches.Create(SourceType:=xlConsolidation, _
        SourceData:=wsA.Name & "!" _
        & myData.Address(, , xlR1C1)).CreatePivotTable _
        TableDestination:="", _
        TableName:="PT1"
        Set wsPT = ActiveSheet
        Set PT01 = wsPT.PivotTables(1)

        With PT01
            .ColumnFields(1).Orientation = xlHidden
            .RowFields(1).Orientation = xlHidden
        End With

        'move combined labels to right, and split
        'then move back to left side of table
        wsPT.Range("A2").ShowDetail = True
        Set wsNewData = ActiveSheet
        With wsNewData
            .Columns("B:C").Cut
            .Columns("A:B").Insert Shift:=xlToRight
            .Columns("C:C").TextToColumns _
            Destination:=.Range("C1"), _
            DataType:=xlDelimited, _
            TextQualifier:=xlDoubleQuote, _
            ConsecutiveDelimiter:=False, _
            Tab:=False, _
            Semicolon:=False, _
            Comma:=False, _
            Space:=False, _
            Other:=True, _
            OtherChar:=mySep
            .Range(.Cells(1, 3), .Cells(1, NumCols + 2)) _
            .EntireColumn.Cut
            .Range(.Cells(1, 1), .Cells(1, NumCols)) _
            .EntireColumn.Insert Shift:=xlToRight
        End With

        With myList.HeaderRowRange
            .Resize(, NumCols).Copy _
            Destination:=wsNewData.Cells(1, 1)
        End With


        ' Note: I couldn't understand what you wanted to do with this lines. Replace the current data?
        wsNewData.Copy after:=wbA.Worksheets(wbA.Worksheets.Count)
        wbNew.Close savechanges:=False

    Next wsA

    msgEnd = "Data is unpivoted in new worksheets"


exitHandler:
    Application.ScreenUpdating = True
    MsgBox msgEnd
    Application.EnableEvents = True
    Exit Sub

errHandler:
    msgEnd = "Could not unpivot the data"
    Resume exitHandler

End Sub
代码更新:将工作表复制回当前文件


如果有帮助,请标出答案。

谢谢你,我知道有一个我没有点的I,真的!!而且还要为清理代码付出额外的努力。我在构建时有点懒。在循环结束时,我试图复制新表,在原始文件中添加新表,然后新工作簿关闭我更新了代码。请注意,首先,您不需要在新工作簿中创建流程,但由于您有以前的代码,我尝试将其保留为您的代码。一旦我知道单独的表是正确的,我将确保代码更加简洁:
Sub UnpivotData()

    'downloaded from contextures.com
    'code to unpivot named Excel table
    'uses first table on the sheet,
    'if more than one table
    Dim myList As ListObject
    Dim NumCols As Long
    Dim PT01 As PivotTable
    Dim wbA As Workbook
    Dim wbNew As Workbook
    Dim wsA As Worksheet
    Dim wbm As Worksheet
    Dim wsNew As Worksheet
    Dim wsPT As Worksheet
    Dim wsNewData As Worksheet
    Dim myData As Range
    Dim mySep As String
    Dim myJoin As String
    Dim ColStart As Long
    Dim ColEnd As Long
    Dim ColCount As Long
    Dim RowStart As Long
    Dim RowEnd As Long
    Dim RowCount As Long
    Dim DataStart As Range
    Dim DataEnd As Range
    Dim iCol As Long
    Dim myFormula As String
    Dim msgSep As String
    Dim msgLabels As String
    Dim msgEnd As String

    Dim wsCounter As Integer

    On Error GoTo errHandler

    ' Reference the current workbook
    Set wbA = ThisWorkbook

    ' Define current separator
    mySep = "|"

    'join operator for Excel formulas
    myJoin = "&"

    ' Set first columns that wont be unpivoted
    NumCols = 7

    ' Loop through the current workbook sheets
    For Each wsA In wbA.Worksheets

        ' Set a worksheet counter
        wsCounter = wsCounter + 1

        ' Convert current region to table / listobject
        wsA.ListObjects.Add(SourceType:=xlSrcRange, _
        Source:=wsA.Cells.CurrentRegion, _
        xlListObjectHasHeaders:=xlYes _
        ).Name = "myTable" & wsCounter

        ' Copy worksheet to new file and set a reference
        wsA.Copy
        Set wbNew = ActiveWorkbook
        Set wsNew = wbNew.Sheets(1)

        ' Reference the table / listobject in the new file
        Set myList = wsNew.ListObjects(1)

        With myList
            ColStart = .HeaderRowRange.Columns(1).Column
            RowStart = .HeaderRowRange.Columns(1).Row
            RowCount = .DataBodyRange.Rows.Count
            RowEnd = .DataBodyRange.Rows(RowCount).Row
            'insert column for the combined labels
            wsNew.Columns(NumCols + ColStart).Insert Shift:=xlToRight
            ColCount = .DataBodyRange.Columns.Count
            ColEnd = .DataBodyRange.Columns(ColCount).Column
        End With

        'build formula to combine labels
        myFormula = "=("
            For iCol = 1 To NumCols
            myFormula = myFormula & "[@" _
            & myList.HeaderRowRange(1, iCol).Value _
            & "]" & myJoin & Chr(34) _
            & mySep & Chr(34) & myJoin
        Next iCol

        myFormula = Left(myFormula, Len(myFormula) - 5)
        myFormula = myFormula & ")"

        With myList
            .DataBodyRange.Cells(1, NumCols + 1).Formula = myFormula
            .DataBodyRange.Columns(NumCols + 1).Value _
            = .DataBodyRange.Columns(NumCols + 1).Value
            Set DataStart = .HeaderRowRange(1, NumCols + 1)
        End With

        Set DataEnd = wsNew.Cells(RowEnd, ColEnd)
        Set myData = wsNew.Range(DataStart, DataEnd)

        'create multiple consolidation pivot table
        wbNew.PivotCaches.Create(SourceType:=xlConsolidation, _
        SourceData:=wsA.Name & "!" _
        & myData.Address(, , xlR1C1)).CreatePivotTable _
        TableDestination:="", _
        TableName:="PT1"
        Set wsPT = ActiveSheet
        Set PT01 = wsPT.PivotTables(1)

        With PT01
            .ColumnFields(1).Orientation = xlHidden
            .RowFields(1).Orientation = xlHidden
        End With

        'move combined labels to right, and split
        'then move back to left side of table
        wsPT.Range("A2").ShowDetail = True
        Set wsNewData = ActiveSheet
        With wsNewData
            .Columns("B:C").Cut
            .Columns("A:B").Insert Shift:=xlToRight
            .Columns("C:C").TextToColumns _
            Destination:=.Range("C1"), _
            DataType:=xlDelimited, _
            TextQualifier:=xlDoubleQuote, _
            ConsecutiveDelimiter:=False, _
            Tab:=False, _
            Semicolon:=False, _
            Comma:=False, _
            Space:=False, _
            Other:=True, _
            OtherChar:=mySep
            .Range(.Cells(1, 3), .Cells(1, NumCols + 2)) _
            .EntireColumn.Cut
            .Range(.Cells(1, 1), .Cells(1, NumCols)) _
            .EntireColumn.Insert Shift:=xlToRight
        End With

        With myList.HeaderRowRange
            .Resize(, NumCols).Copy _
            Destination:=wsNewData.Cells(1, 1)
        End With


        ' Note: I couldn't understand what you wanted to do with this lines. Replace the current data?
        wsNewData.Copy after:=wbA.Worksheets(wbA.Worksheets.Count)
        wbNew.Close savechanges:=False

    Next wsA

    msgEnd = "Data is unpivoted in new worksheets"


exitHandler:
    Application.ScreenUpdating = True
    MsgBox msgEnd
    Application.EnableEvents = True
    Exit Sub

errHandler:
    msgEnd = "Could not unpivot the data"
    Resume exitHandler

End Sub