Importing excel form and adjust worksheet

Dutchmaste

New Member
Joined
Jun 14, 2016
Messages
23
Office Version
  1. 365
Platform
  1. Windows
I currently have the following macro. But instead of importing it into Worksheet "Import" I would like it to create a worksheet with the name of the excel file without the extention and to import it into that sheet. I know it is possible and tried to implement from others asked on this forum but can't figure it out.

VBA Code:
Sub Get_Data_From_File()
    Dim FileToOpen As Variant
    Dim OpenBook As Workbook
    Application.ScreenUpdating = False
   
    FileToOpen = Application.GetOpenFilename(Title:="Browse for your File & Import Range", FileFilter:="Excel Files (*xls*),*xls*")
    If FileToOpen <> False Then
        Set OpenBook = Application.Workbooks.Open(FileToOpen)
        OpenBook.Sheets(1).Range("A5:I200").Copy
        ThisWorkbook.Worksheets("Import").Range("A1").PasteSpecial xlPasteValues
        OpenBook.Close False
        End If

End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
As long as the only period in the file name preceeds the extension, try this:
VBA Code:
Sub Get_Data_From_File()

    Dim FileToOpen As Variant
    Dim OpenBook As Workbook
    Dim wbName() As String
    Dim wsName As String
    Application.ScreenUpdating = False
  
    FileToOpen = Application.GetOpenFilename(Title:="Browse for your File & Import Range", FileFilter:="Excel Files (*xls*),*xls*")
    If FileToOpen <> False Then
        Set OpenBook = Application.Workbooks.Open(FileToOpen)
'       Get name of workbook for name of sheet
        wbName = Split(OpenBook.Name, ".")
        wsName = wbName(0)
'       Create new sheet in ThisWorkbook
        ThisWorkbook.Sheets.Add.Name = wsName
'
        OpenBook.Sheets(1).Range("A5:I200").Copy
        ThisWorkbook.Worksheets(wsName).Range("A1").PasteSpecial xlPasteValues
        OpenBook.Close False
    End If

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,950
Messages
6,122,438
Members
449,083
Latest member
Ava19

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top