在工作簿中拆分具有相同列范围的工作表,以使用VBA代码分隔Excel文件

时间:2018-11-04 23:42:21

标签: excel vba

我的第一个关于stackoverflow的问题是VBA的新问题。我到处都看过了,觉得我已经尽了一切努力来找到上述问题的解决方案。

我想根据工作表名称将工作簿中每个工作表的相同列复制到新文件中。

我发现下面的VBA代码可以复制整个工作表,但是很难做到只将每张工作表上的Range(“ A:K”)复制到一个新文件中。 我以为下面的代码可能有用,但我的态度很严重。

Sub Splitbook()
'Updateby20140612
Dim xPath As String
Dim rng As Range
xPath = Application.ActiveWorkbook.Path
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For Each xWs In ThisWorkbook.Sheets
    Set rng = Range("A:K")
    xWs.rng.Copy
    Application.ActiveWorkbook.SaveAs Filename:=xPath & "\" & xWs.Name & ".xlsx"
    Application.ActiveWorkbook.Close False
Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub

任何帮助将不胜感激。非常感谢。

1 个答案:

答案 0 :(得分:0)

未经测试:

Sub Splitbook()

    'Updateby20140612
    Dim xPath As String, xWs as Worksheet
    Dim rng As Range, wb as workbook

    xPath = Application.ActiveWorkbook.Path

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    For Each xWs In ThisWorkbook.Sheets
        set wb = workbooks.add()
        xWs.Range("A:K").Copy wb.sheets(1).range("A1")
        wb.SaveAs Filename:=xPath & "\" & xWs.Name & ".xlsx"
        wb.Close False
    Next

    Application.DisplayAlerts = True
    Application.ScreenUpdating = True

End Sub