excel 将工作表复制到新工作簿

dwbf0jvd  于 2023-01-03  发布在  其他
关注(0)|答案(1)|浏览(337)

我希望有人能帮忙。我有一个excel宏文件(.xlsm),我正在从其他文件中提取数据并创建多个工作表。我尝试将其中一个工作表导出到每天使用动态文件名创建的新工作簿中,文件名包括当前日期。我收到错误消息“Excel无法将工作表插入目标工作簿,因为它包含的行和列比源工作簿少..."。我想这是因为我试图从.xlsm复制到.xlxs,我不知道如何解决这个问题。下面是我的代码:

Dim wb As Workbook
    Set wb = Workbooks.Add
    ThisWorkbook.Sheets("Data").Copy Before:=wb.Sheets(1)
    wb.SaveAs "\\NetorkDrive\Filename " & Format(Now(), "MM_DD_YY") & ".xlsx"

我希望将“数据”表中的信息复制到标题为“文件名Date.xlsx”的新工作簿中,但我收到了上述错误。

mrphzbgm

mrphzbgm1#

将工作表导出到新工作簿

Sub ExportWorksheet()

    Dim swb As Workbook: Set swb = ThisWorkbook ' workbook containing this code
    Dim sws As Worksheet: Set sws = swb.Sheets("Data")
    
    sws.Copy ' creates a single-worksheet workbook
    
    Dim dwb As Workbook: Set dwb = Workbooks(Workbooks.Count) ' the last
    
    Dim dPath As String
    dPath = "\\NetworkDrive\Filename " & Format(Date, "MM_DD_YY") & ".xlsx"
    
    Application.DisplayAlerts = False ' overwrite without confirmation
        dwb.SaveAs dPath, xlOpenXMLWorkbook
    Application.DisplayAlerts = True
    'dwb.Close SaveChanges:=False ' it just got saved
    
    MsgBox "Worksheet exported.", vbInformation

End Sub

相关问题