vba拆分工作簿的工作表至多个独立工作簿

Sub DetachWorkbook()
    On Error Resume Next
    Dim pathStr As String, i As Long, activeWb As String
    With Application.FileDialog(msoFileDialogFolderPicker)
        If .Show Then
            pathStr = .SelectedItems(1)
        Else
            Exit Sub
        End If
    End With

    pathStr = pathStr & IIf(Right(pathStr, 1) = "\", "", "\")
    Application.ScreenUpdating = False

    activeWb = ActiveWorkbook.Name
    For i = 1 To Sheets.Count
        If Sheets(i).Name <> "Sheet1" Then
            Sheets(i).Copy
            ActiveWorkbook.SaveAs Filename:=pathStr & Workbooks(activeWb).Sheets(i).Name & IIf(Application.Version * 1 < 12, ".xls", ".xlsx"), FileFormat:=xlWorkbookDefault, CreateBackup:=False

            With ActiveSheet.UsedRange
                Set cell = .Find("=*]*'!", LookIn:=xlFormulas, SearchOrder:=xlByRows, LookAt:=xlPart, MatchCase:=True)
                If cell Is Nothing Then GoTo line
                FirstAddress = cell.Address
                Do
                    cell = cell.Value
                    Set cell = .FindNext(cell)
                    If cell Is Nothing Then Exit Do
                    If cell.Address = FirstAddress Then Exit Do
                Loop
            End With

            line:
                ActiveWindow.Close
                Workbooks(activeWb).Activate
        End If
    Next i

    Application.ScreenUpdating = True
    Shell "Explorer.exe " & pathStr, vbNormalFocus
End Sub


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值