【问题标题】:VBA Excel Split a sheet every n columnVBA Excel每n列拆分一张表
【发布时间】:2017-04-18 07:47:41
【问题描述】:

我有一个带有两个“标题列”的转置表。该表向右延伸太长,我希望能够将该表拆分为每“n”列的多个工作表,同时还将两个标题列复制到每个新工作表。我能找到的所有示例只拆分每“n”行,而我想拆分每“n”列。

我发现很难用纯文本来解释这一点,所以我附上了屏幕截图:在这个例子中,第一张表包含原始数据,随后的表包含宏的预期结果,其中表被拆分为每个两列:

【问题讨论】:

    标签: vba excel split


    【解决方案1】:
    Sub colsplit()
    Dim wssrc As Worksheet
    Dim wsdest As Worksheet
    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Set wssrc = ActiveWorkbook.Sheets("Source")
    'getting No of columns
    lcol = wssrc.Cells(1, Columns.Count).End(xlToLeft).Column 
    On Error GoTo resetsettings
    'getting User input to split count
    col = InputBox("Enter Number of columns to split")
    If IsNumeric(col) And col <> "" And col > 0 Then
    desti = 1
    For i = 3 To lcol
    Set wsdest = Sheets.Add(After:=Sheets(Sheets.Count))
    wsdest.Name = "split" & desti
    'copying header columns to new sheets
    wssrc.Columns(1).EntireColumn.Copy Destination:=wsdest.Cells(1, 1)
    wssrc.Columns(2).EntireColumn.Copy Destination:=wsdest.Cells(1, 2)
    desti = desti + 1
    x = 3
    For j = i To (i + col - 1)
    'Copying other columns to new sheet
    wssrc.Columns(j).EntireColumn.Copy Destination:=wsdest.Cells(1, x)
    x = x + 1
    Next j
    i = i + col - 1
    Next i
    Else
    End If
    resetsettings:
    Application.ScreenUpdating = True
    Application.DisplayAlerts = True
    End Sub
    

    此代码将拆分列并将它们粘贴到新工作表中。

    【讨论】:

    • 哇,这正是我想要的!非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多