Help with For X

Shakeable_Drip

Board Regular
Joined
May 30, 2023
Messages
52
Office Version
  1. 365
Platform
  1. Windows
I'm butchering VBA I found.
I'd like to have FilePicker to select files then write those file names to range A1.

VBA Code:
Sub ChooseFiles()

Dim SourceFileApp As Application
Dim TargetFolderApp As Application
Dim vitemselecetd As Variant

Set SourceFileApp = Application
Set TargetFolderApp = Application

listdepth = Cells(Rows.Count, 1).End(xlUp).Row
With SourceFileApp.FileDialog(msoFileDialogFilePicker)  ' get file to replicate
    .AllowMultiSelect = True
    If .Show = -1 Then
        
        For Each vitemselecetd In .SelectedItems
        
        For x = 1 To listdepth
          'write .selecteditems starting at range A1
        Next x
    Next vitemselecetd
 
    
    
    Else
                    MsgBox "No Source File Chosen"
                    Exit Sub
 
    End If
End With

End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.
VBA Code:
Sub ChooseFiles()

    Dim SourceFileApp As Application
    Dim TargetFolderApp As Application
    Dim vitemselecetd As Variant
    Dim i As Long
    Dim listdepth As Long

    Set SourceFileApp = Application
    Set TargetFolderApp = Application

    listdepth = Cells(Rows.Count, 1).End(xlUp).Row
    
    With SourceFileApp.FileDialog(msoFileDialogFilePicker)  ' get file to replicate
        .AllowMultiSelect = True
        
        If .Show = -1 Then
            For Each vitemselecetd In .SelectedItems
                Cells(listdepth, "A").Offset(i).Value = vitemselecetd
                i = i + 1
            Next vitemselecetd
        Else
            MsgBox "No Source File Chosen"
            Exit Sub
        End If
    
    End With

End Sub

Artik
 
Upvote 0
Solution
VBA Code:
Sub ChooseFiles()

    Dim SourceFileApp As Application
    Dim TargetFolderApp As Application
    Dim vitemselecetd As Variant
    Dim i As Long
    Dim listdepth As Long

    Set SourceFileApp = Application
    Set TargetFolderApp = Application

    listdepth = Cells(Rows.Count, 1).End(xlUp).Row
   
    With SourceFileApp.FileDialog(msoFileDialogFilePicker)  ' get file to replicate
        .AllowMultiSelect = True
       
        If .Show = -1 Then
            For Each vitemselecetd In .SelectedItems
                Cells(listdepth, "A").Offset(i).Value = vitemselecetd
                i = i + 1
            Next vitemselecetd
        Else
            MsgBox "No Source File Chosen"
            Exit Sub
        End If
   
    End With

End Sub

Artik
That worked like a charm thanks Artik! Can you help me with one more question?
I've got this to strip away the file path, but I'm no longer using "MyFolder" and I don't know if it works with the "ForEach"
Can I also strip away the ".pdf" as well?
VBA Code:
vitemselecetd, MyFolder & IIf(InStr(1, ".", Cells(x, 1)) > 0, Cells(x, 1), Cells(x, 1) & Right(vitemselecetd, Len(vitemselecetd) - InStrRev(vitemselecetd, ".", Len(vitemselecetd)) + 1))
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,173
Members
451,543
Latest member
cesymcox

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