我有一个需要拆分的电子表格。例如,我有A、B、C和D列,我需要拆分工作表,以便:
第1页-A和B栏第2页-A和C栏第3页-A和D栏
这是针对大约350列的,并且列A需要是常数。
我有下面的代码:
Sub t()
Dim lc As Long, sh As Worksheet, newSh, ws1 As Worksheet
Set ws1 = ThisWorkbook.Sheets("Sheet2")
Set sh = ActiveSheet
With sh
lc = .Cells.Find("*", , xlValues, xlPart, xlByColumns, xlPrevious).Column
For i = 1 To lc
If Application.CountA(.Columns(i)) > 0 Then
Set newSh = Sheets.Add
ws1.Range("a:a").Copy Range("a:a")
Intersect(.UsedRange, .Columns(i)).Copy newSh.Range("A1")
newSh.Copy
ActiveWorkbook.SaveAs newSh.Range("a1").Value & ".xlsx"
ActiveWorkbook.Close
Application.DisplayAlerts = False
newSh.Delete
Application.DisplayAlerts = True
End If
Next
End With
End Sub
但这只是拆分出单独的列,我每次都需要添加列A
1条答案
按热度按时间owfi6suc1#
在这段代码中:
您两次都输出到列A。因此,列A每次都被复制,但被您复制的其他列覆盖。通过在第二行输出到
"B1"
来修复此问题。