如何在excel上堆叠列

如何在excel上堆叠列,excel,vba,stack,Excel,Vba,Stack,我有一个当前格式的数据电子表格: A1, A2, A3,B1,B2,B3,C1,C2,C3 我正在尝试堆叠数据,使其符合以下格式: A1, A2, A3, B1, B2, B3 C1, C2, C3 这样我就可以以正确的格式导入一些软件 我在网上找到了下面的代码,它确实可以堆叠数据,但是它可以堆叠每一列,而我需要它来堆叠每三列,我想知道是否有人可以修改这个宏来做到这一点 提前谢谢 Option Explicit Sub Stack_cols() On Error GoTo Stack

我有一个当前格式的数据电子表格:

A1, A2, A3,B1,B2,B3,C1,C2,C3
我正在尝试堆叠数据,使其符合以下格式:

A1, A2, A3,
B1, B2, B3
C1, C2, C3
这样我就可以以正确的格式导入一些软件

我在网上找到了下面的代码,它确实可以堆叠数据,但是它可以堆叠每一列,而我需要它来堆叠每三列,我想知道是否有人可以修改这个宏来做到这一点

提前谢谢

Option Explicit


Sub Stack_cols()


On Error GoTo Stack_cols_Error

Dim lNoofRows As Long, lNoofCols As Long
Dim lLoopCounter As Long, lCountRows As Long
Dim sNewShtName As String
Dim shtOrg As Worksheet, shtNew As Worksheet

'Turn off the screen update to make macro run faster
Application.ScreenUpdating = False
'Ask for a new sheet name, if not provided use newsht
sNewShtName = InputBox("Enter the new worksheet name", "Enter name", "newsht")
'Set a sheet variable for the sheet where the data resides
Set shtOrg = ActiveSheet
'Add a new worksheet, rename it and set it to a variable
If Not SheetExists(sNewShtName) Then
    Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = sNewShtName
    Set shtNew = Worksheets(sNewShtName)
Else
    MsgBox "Worksheet name exists. Try again", vbInformation, "Sheet Exists"
    Exit Sub
End If

With shtOrg
    'Get the last column number
    'Replace .Range("IV1") with .Range("XFD1") for Excel 2007
    lNoofCols = .Range("IV1").End(xlToLeft).Column
    'Start a loop to copy and paste data from the first column to the last column
    For lLoopCounter = 1 To lNoofCols
    'Count the number of rows in the looping column
        'Replace .Cells(65536, lLoopCounter) with .Cells(1048576, lLoopCounter) for Excel 2007
        lNoofRows = .Cells(65536, lLoopCounter).End(xlUp).Row
        .Range(.Cells(1, lLoopCounter), .Cells(lNoofRows, lLoopCounter)).Copy Destination:=shtNew.Range(shtNew.Cells(lCountRows + 1, 1), shtNew.Cells(lCountRows + lNoofRows, 1))
        'count the number of rows in the new worksheet
        lCountRows = lCountRows + lNoofRows
    Next lLoopCounter
End With

On Error GoTo 0
SmoothExit_Stack_cols:
        Application.ScreenUpdating = True
        Exit Sub

Stack_cols_Error:
    MsgBox "Error " & Err.Number & " (" & Err.Description & ") in Sub:Stack_cols"
    Resume SmoothExit_Stack_cols
End Sub
'Check if a worksheet exists or not
Public Function SheetExists(sShtName As String) As Boolean
    On Error Resume Next


Dim wsSheet As Worksheet, bResult As Boolean
bResult = False
Set wsSheet = Sheets(sShtName)

On Error GoTo 0
If Not wsSheet Is Nothing Then
    bResult = True
End If
SheetExists = bResult
End Function
编辑

这就是我试图叠加的数据


这也是我想要的外观

您发布的代码对于您试图实现的目标来说似乎过于复杂,请尝试以下方法:

Sub stackColumns()

Dim lngRow As Long, lngCol As Long, i As Long
lngRow = 1
lngCol = 0

For i = 1 To ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
    ActiveSheet.Cells(lngRow, lngCol + 1).Value = ActiveSheet.Cells(1, i).Value
    If lngRow > 1 Then ActiveSheet.Cells(1, i).Clear
    lngCol = (lngCol + 1) Mod 3
    If lngCol = 0 Then lngRow = lngRow + 1
Next i

End Sub
这假设数据从单元格A1开始

编辑:要适合您的实际数据集,请使用此(甚至更短)版本:


您发布的代码对于您要实现的目标来说似乎过于复杂,请尝试以下方法:

Sub stackColumns()

Dim lngRow As Long, lngCol As Long, i As Long
lngRow = 1
lngCol = 0

For i = 1 To ActiveSheet.Cells(1, Columns.Count).End(xlToLeft).Column
    ActiveSheet.Cells(lngRow, lngCol + 1).Value = ActiveSheet.Cells(1, i).Value
    If lngRow > 1 Then ActiveSheet.Cells(1, i).Clear
    lngCol = (lngCol + 1) Mod 3
    If lngCol = 0 Then lngRow = lngRow + 1
Next i

End Sub
这假设数据从单元格A1开始

编辑:要适合您的实际数据集,请使用此(甚至更短)版本:

请尝试以下操作:

尝试以下操作:


我喜欢保持简单,所以这里是另一个例子

Dim x As Long, y As Long, ws As Worksheet
Set ws = ThisWorkbook.Sheets("Sheet3") 'change sheet as needed

x = 1
y = 2

    With ws
        For x = 1 To .Cells(1, .Columns.Count).End(xlToLeft).Column Step 3
            .Cells(1, x).Resize(, 3).Copy Destination:=ws.Cells(y, 1)
            y = y + 1
        Next x

        .Cells(1, 4).Resize(, .Cells(1, .Columns.Count).End(xlToLeft).Column).Clear
    End With

我喜欢保持简单,所以这里是另一个例子

Dim x As Long, y As Long, ws As Worksheet
Set ws = ThisWorkbook.Sheets("Sheet3") 'change sheet as needed

x = 1
y = 2

    With ws
        For x = 1 To .Cells(1, .Columns.Count).End(xlToLeft).Column Step 3
            .Cells(1, x).Resize(, 3).Copy Destination:=ws.Cells(y, 1)
            y = y + 1
        Next x

        .Cells(1, 4).Resize(, .Cells(1, .Columns.Count).End(xlToLeft).Column).Clear
    End With

我在运行突出显示.Cells(1,1).Resize(1,lc).Clear并显示“无法更改合并单元格的一部分”的代码时遇到此错误,尽管该代码中没有合并单元格worksheet@MacauleyPowell,则错误说明不是这样。合并单元格是一场噩梦,我建议尽快将其清除。正如添加的屏幕截图所示,为我运行此代码非常顺利。您好,我可以向您保证没有合并列,这是一个全新的工作表。我在运行代码时遇到此错误,该代码突出显示.Cells(1,1)。Resize(1,lc)。清除并说“无法更改合并单元格的一部分”-尽管合并单元格中没有合并单元格worksheet@MacauleyPowell,错误表明情况并非如此。合并单元格是一场噩梦,我建议尽快将其清除。正如添加的屏幕截图所示,为我运行这段代码非常顺利。您好,我可以向您保证没有合并列,这是一个全新的工作表。这似乎效果最好。我可能没有尽可能的描述。不,对不起,我试图添加一个屏幕截图,但不能完全弄清楚。有没有办法在评论上添加截图,这样我可以更好地向您展示?没有,您需要添加截图作为问题的编辑。我更新了我的答案,这将完全满足您的要求。如果这解决了你的问题,请考虑投票并将答案标记为被接受,这样其他人可能会觉得更容易。这似乎是最好的。我可能没有尽可能的描述。不,对不起,我试图添加一个屏幕截图,但不能完全弄清楚。有没有办法在评论上添加截图,这样我可以更好地向您展示?没有,您需要添加截图作为问题的编辑。我更新了我的答案,这将完全满足您的要求。如果这解决了你的问题,请考虑投票并将答案标记为被接受,这样其他人可能会觉得更容易。