简体   繁体   中英

copy a worksheet into another workbook using macro

With Application.FileDialog(msoFileDialogOpen)
    .Filters.Clear
    .Filters.Add "Excel 2002-03", "*.xls", 1
    .Filters.Add "Excel 2007", "*.xlsx; *.xlsm; *.xlsa", 2
    .AllowMultiSelect = False
    .Show



    If .SelectedItems.Count > 0 Then
        Workbooks.Open .SelectedItems(1)
        Set wkbSourceBook = ActiveWorkbook
        Set rngSourceRange = Application.InputBox(prompt:="Select source range", Title:="Source Range", Default:="A1", Type:=8)
        wkbCrntWorkBook.Activate
        Set rngDestination = Application.InputBox(prompt:="Select destination cell", Title:="Select Destination", Default:="A1", Type:=8)
        rngSourceRange.Copy rngDestination
        rngDestination.CurrentRegion.EntireColumn.AutoFit
        wkbSourceBook.Close False
    End If

End With

I am creating a dialog box to take the file name as input and then need to copy a complete sheet to another workbook instead of selecting the range and stuff. how to do that

You can use the Copy method of the Worksheet object.

wkbSourceBook.Sheets(1).Copy After:=wkbCrntWorkBook.Worksheets(1)

You could choose whichever sheet you wanted by name. It will copy with the existing name, and if it already exists will become "Name (2)". You can then rename it from there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM