我想将文件拆分为单个文件,然后在同一路径中复制。 VBA宏应拆分所有工作表。宏应从已定义工作表(例如“测试”)之后的第一工作表开始。
我试图找到一种解决方案来定义一个工作表,但是没有成功。
Sub Splitbook()
Dim xPath As String
xPath = Application.ActiveWorkbook.Path
Application.ScreenUpdating = False
Application.DisplayAlerts = False
For Each xWs In ThisWorkbook.Sheets
xWs.Copy
Application.ActiveWorkbook.SaveAs Filename:=xPath & "\" & xWs.Name & ".xlsx"
Application.ActiveWorkbook.Close False
Next
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
答案 0 :(得分:1)
使用Worksheet.Index
识别要复制的纸张,如下所示:
Option Explicit
Sub Splitbook()
Dim xPath As String
xPath = ThisWorkbook.Path
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Dim i As Long
With ThisWorkbook
For i = .Worksheets("Test").Index + 1 To .Worksheets.Count
.Worksheets(i).Copy
Application.ActiveWorkbook.SaveAs Filename:=xPath & "\" & .Worksheets(i).Name & ".xlsx"
Application.ActiveWorkbook.Close False
Next
End With
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
答案 1 :(得分:0)
有一个worksheet.index函数。输入工作表的名称,获取其索引,然后仅获取比您想开始的索引大的索引。
dim wks as worksheet
dim startindex as integer
startindex = 0
for each wks in thisworkbook.worksheets
if wks.name = "targetsheet" then
startindex = wks.index
end if
if not startindex = 0 then
if wks.index > startindex then
'do some stuff
end if
end if
next
我不知道如果您不断更改工作表的顺序会发生什么。
编辑:这是一个简单的测试,它会在移动时立即重新索引,因此请确保它们的顺序正确。