Excel VBA to add file name to column C in worksheet

JENWPS

New Member
Joined
Nov 9, 2023
Messages
10
Office Version
  1. 365
Platform
  1. Windows
I am trying to find the correct VBA to add the file name to column C of several worksheets in a folder. The file name should only be added to column C for rows that have info in columns A and/or B and it can overwrite anything that is currently in column C. The VBA I am currently using works but sometimes skips row 1 or adds the file name to several rows below where there is no info in columns A & B (that I can see). This is what I am currently using -

Sub STEP_2_InsertsFileName()
Dim MyFolder As String 'Path collected from the folder picker dialog
Dim myfile As String 'Filename obtained by DIR function
Dim wbk As Workbook 'Used to loop through each workbook
On Error Resume Next
Application.ScreenUpdating = False
'Opens the folder picker dialog to allow user selection
With Application.FileDialog(msoFileDialogFolderPicker)
.Title = "Please select a folder"
.Show
.AllowMultiSelect = False
If .SelectedItems.Count = 0 Then 'If no folder is selected, abort
MsgBox "You did not select a folder"
Exit Sub
End If
MyFolder = .SelectedItems(1) & "\" 'Assign selected folder to MyFolder
End With
myfile = Dir(MyFolder) 'DIR gets the first file of the folder
'Loop through all files in a folder until DIR cannot find anymore
Do While myfile <> ""
'Opens the file and assigns to the wbk variable for future use
Set wbk = Workbooks.Open(Filename:=MyFolder & myfile)
'Replace the line below with the statements you would want your macro to perform
Dim i As Long

For i = 2 To Sheets(1).UsedRange.Rows.Count

Sheets(1).Range("C" & i).Value = myfile

Next i
wbk.Close savechanges:=True
myfile = Dir 'DIR gets the next file in the folder
Loop
Application.ScreenUpdating = False
MsgBox "Done - move on to STEP 3"
End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Try:
VBA Code:
For i = 2 To Sheets(1).UsedRange.Rows.Count
    If WorksheetFunction.CountA(Range("A" & i).Resize(, 2)) >= 1 Then
        Sheets(1).Range("C" & i).Value = myfile
    End If
Next i
 
Upvote 0
Solution

Forum statistics

Threads
1,223,885
Messages
6,175,187
Members
452,616
Latest member
intern444

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