最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

excel - Is there a way to copy multiple sheets into a new file? - Stack Overflow

programmeradmin6浏览0评论

This is my code to break up a data base in a source file and then create sub files for every member listed in the database. I'm having a hard time figuring out how to include a copy of 3 additional sheets("Program", "Banked", "Drive") from the source file in each of the sub files.

Option Explicit
Sub Demo()
    Dim i As Long, j As Long, sPath As String, lastRow As Range
    Dim rowCnt As Long, ColCnt As Long, rngData As Range
    Dim arrData, arrRow, oSht As Worksheet, oWK As Workbook
    Const START_ROW = 8
    Const MTH_DIR = "202406"
    sPath = ThisWorkbook.Path & "\" & MTH_DIR
    If Len(Dir(sPath, vbDirectory)) = 0 Then MkDir sPath
    If Right(sPath, 1) <> "\" Then sPath = sPath & "\"
    Set oSht = ThisWorkbook.Sheets("Sheet1") ' ActiveSheet
    Set rngData = oSht.Range("A1").CurrentRegion
    rowCnt = rngData.Rows.Count
    ColCnt = rngData.Columns.Count
    arrData = rngData.Value
    oSht.Copy
    Set oWK = ActiveWorkbook
    With oWK.Sheets(1)
        Set lastRow = .Cells(START_ROW, 1).Resize(, ColCnt)
        .Rows(START_ROW + 1 & ":" & rowCnt).Delete
        With lastRow.Borders(xlEdgeBottom)
            .LineStyle = xlContinuous
            .Weight = xlThick
        End With
    End With
    ReDim arrRes(0, 1 To ColCnt)
    For i = START_ROW To rowCnt
        If Len(arrData(i, 1)) > 0 Then
            If i > START_ROW Then
                For j = 1 To ColCnt
                    arrRes(0, j) = arrData(i, j)
                Next
                lastRow.Value = arrRes
            End If
            oWK.SaveAs sPath & arrData(i, 1) & ".xlsx"
        End If
    Next
    oWK.Close False
End Sub
发布评论

评论列表(0)

  1. 暂无评论