Insert a sheet into workbook using the open dialog

Johnzea

New Member
Joined
Apr 5, 2019
Messages
23
I would like to import an xlsm sheet with data into my workbook at the end of the last tab and have the new inserted sheet/tab name the xlsm filename being inserted less the path. below is my code but I cannot get it to work right. any thought to what i am missing or have wrong?

I switched to decaf tonight so i'm a little slow....any help is very much appreciated...thank you


With Application.FileDialog(msoFileDialogFilePicker)
.AllowMultiSelect = False
.Filters.Add "Excel Files", "*.xlsx; *.xlsm; *.xls; *.xlsb", 1
.Show

fullpath = .SelectedItems.Item(1)
End With

If InStr(fullpath, ".xls") = 0 Then
Exit Sub
End If
Sheets.Add(, Sheets(Sheets.Count)).Name = fullpath
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
Do you need something like this:

Code:
Sub Insert_sheet()
  Dim fullpath As Variant, wPath As String, wFile As String, wb As Workbook
  
  With Application.FileDialog(msoFileDialogFilePicker)
    .AllowMultiSelect = False
    .Filters.Add "Excel Files", "*.xlsx; *.xlsm; *.xls; *.xlsb", 1
    If Not .Show Then Exit Sub
    fullpath = .SelectedItems.Item(1)
  End With
  wPath = Left(fullpath, InStrRev(fullpath, "\") - 1)
  wFile = Mid(fullpath, InStrRev(fullpath, "\") + 1)
  wFile = Left(wFile, InStrRev(wFile, ".") - 1)
  Set wb = Workbooks.Open(fullpath)
  wb.Sheets(1).Copy after:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
  ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count).Name = wFile
  wb.Close False
End Sub
 
Upvote 0
I'm glad to help you. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,827
Messages
6,121,818
Members
449,049
Latest member
cybersurfer5000

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