GetFileName_Multiple Files

marcusmark

Board Regular
Joined
Nov 17, 2014
Messages
98
Hi,
I need help in editing the codes I used as below:

Code:
Sub GetImportFileName2()
Dim FileNames As Variant
Dim Msg As String
Dim I As Integer
FileNames = Application.GetOpenFilename _
(MultiSelect:=True)
If IsArray(FileNames) Then
'Display full path and name of the files
'Msg = "You selected:" & vbNewLine
For I = LBound(FileNames) To UBound(FileNames)
Msg = Msg & FileNames(I) & vbNewLine
Range("A1") = Msg
Next I
MsgBox Msg
Else
'Cancel button clicked
MsgBox "No files were selected."
End If
End Sub

Its a code for GETOPENFILENAME with mutiple selection.
My problem is that I want the filenames only, not the whole path, just the name of the files and it is not only to be copied to a single range. Its like if I choose, 3 files, then 1st file would be in Cell A1, 2nd file in Cell A2 and so on.

Thanks in advance!
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
This will load files W/O dir name...

Code:
   'import all files in folder
'------------------
Public Sub ImportSomeFileNamesInDir()
'------------------
Dim vFil, vTargT, vDir, vPath
Dim i As Integer
Dim sTbl As String, sSql As String
Dim colFiles As FileDialogSelectedItems
Dim itm


On Error GoTo errImp


Set colFiles = UserPickFiles("*.xls", "")
If colFiles.Count > 0 Then
   vPath = colFiles(1)
   getDirName vPath, vDir, vFil
    
       'post the folder name
   'Range("A1").Select
   'ActiveCell.Value = vDir
   'ActiveCell.Offset(1, 0).Select
    
    
       'post the filenames w/o folder name
    i = Len(vDir)
    Range("A1").Select
    For Each itm In colFiles
            vFil = Mid(itm, i + 1) 'filename sans directory
            ActiveCell.Value = vFil
            
            ActiveCell.Offset(1, 0).Select
    Next
End If


Set colFiles = Nothing
Exit Sub

errImp:
MsgBox Err.Description, vbCritical, "ImportAllXLFilesAndSheets()" & Err
Exit Sub
Resume
End Sub


'------------------
Private Function UserPickFiles(ByVal pvFilter, Optional pvStartPath) As FileDialogSelectedItems
'------------------
Dim strTable As String
Dim strFilePath As String
Dim sDialog As String, sDecr  As String, sExt As String


With Application.FileDialog(msoFileDialogFilePicker)   'MUST ADD REFERENCE : Microsoft Office xx.0 Object Library
    .InitialFileName = pvStartPath
    .Title = "Locate a files to Import"
    .ButtonName = "Import"
    .Filters.Clear
    .Filters.Add "Filter", pvFilter
    .Filters.Add "All Files", "*.*"
    .InitialFileName = "c:\"
    .InitialView = msoFileDialogViewList    'msoFileDialogViewThumbnail
    .AllowMultiSelect = True
    
        If .Show = 0 Then
           'MsgBox "Cancelled", vbCritical, "UserPickFile()"
           Exit Function
        End If
    
    'Save the first file selected
    Set UserPickFiles = .SelectedItems
End With
End Function


'------------------
Private Sub getDirName(ByVal psFilePath, ByRef prvDir, Optional ByRef prvFile)
'------------------
    'psFilePath: full file path given
    'prvDir : directory name output
    'prvFile: filename only output
Dim i As Integer, sDir As String


i = InStrRev(psFilePath, "\")          'not available in '97
If i > 0 Then
  prvDir = Left$(psFilePath, i)
  prvFile = Mid$(psFilePath, i + 1)
  If Asc(Mid(prvFile, Len(prvFile), 1)) = 0 Then prvFile = Left(prvFile, Len(prvFile) - 1)
End If
End Sub
 
Upvote 0
wow.this is what I really need though the codes are quite of different level (not that user-friendly to me. Hahaha) but will just study each code.thanks a lot! Super!
 
Upvote 0

Forum statistics

Threads
1,215,008
Messages
6,122,672
Members
449,091
Latest member
peppernaut

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