VBA Excel每n列

时间:2017-04-18 07:47:42

标签: excel vba excel-vba split

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

我发现很难用纯文本来解释这一点,所以我附上截图:在这个例子中,第一张包含原始数据,后续工作表包含宏的意图结果,其中每张表都被分割两栏:

enter image description here enter image description here enter image description here enter image description here

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:-2)

我不清楚你需要什么,一个宏? SE不是编码服务。请尝试使用freelancer.com或类似的来源。

如果您想要一个快速而有用的解决方案:记录您的手动任务以将此表拆分为宏。然后,如果需要,可以运行serval时间。 如果您不知道如何手动执行此操作,请发表评论,然后我会解释。