简体   繁体   中英

Open a document in sheet2 of a workbook

I have a code which once assigned to a button and is clicked opens up a .dat file for me.

However, this opens the file on a separate workbook and I want to be able to open in so that it shows in Sheet2 of the same workbook.

My code is:

Sub Open_Workbook()

    Dim my_FileName As Variant

    my_FileName = Application.GetOpenFilename

    If my_FileName <> False Then
        Workbooks.Open Filename:=my_FileName
    End If

End Sub

You can't do it directly, but this will open it, copy all the data into sheet 2 and then close the dat file without saving

Sub Open_Workbook()

    Dim my_FileName As Variant

    my_FileName = Application.GetOpenFilename

    If my_FileName <> False Then
        Workbooks.Open Filename:=my_FileName
        activeworkbook.sheets(1).usedrange.copy thisworkbook.sheets(2).range("a1")
        activeworkbook.close false
    End If

End Sub

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