macro to extract Fle Names into Col J when Imported

howard

Well-known Member
Joined
Jun 26, 2006
Messages
6,561
Office Version
  1. 2021
Platform
  1. Windows
I have the following macro below.

I would like to extract the file names in Col J for each of the rows pertaining to the files imported that is applicable for each file

It would be appreciated if someone could amend my code to show the file names in col J

See sample data where I have manually shown the File names in Col J


MatchDebits and Credits Based similar text in Narrative.xlsx
ABCDEFGHIJ
1ReferenceSourceRunA/CDateDebitCreditBalanceNarrativeBranch
21983Purchase Ordering535517/06/20213900390983 fuel refundsBR1 Sales June 2021.csv
3792Purchase Ordering535217/06/2021212.210212.21978 fuel-FuelBR1 Sales June 2021.csv
4077Purchase Ordering535317/06/2021203.780203.78834 fuel -FuelBR1 Sales June 2021.csv
5272Purchase Ordering480104/06/2021200.920200.92226 Fuel UP -FuelBR1 Sales June 2021.csv
6484Nom480318/06/2021805.99-805.99Fuel BR2 Sales June 2021.csv
Sheet1
Cell Formulas
RangeFormula
H2:H6H2=+F2-G2




Code:
 Sub Open_MultipleFiles()

Application.DisplayAlerts = False

ChDir "C:\Extract"

Dim LR As Long
With Sheets("Reconciling Items")

LR = .Cells(.Rows.Count, "A").End(xlUp).Row

.Range("A1:J" & LR).ClearContents


Dim fDialog As Object, varFile As Variant

Dim nb As Workbook, tw As Workbook, ts As Worksheet

With Application

.ScreenUpdating = False

.Calculation = xlCalculationManual

.CutCopyMode = False

End With

Set tw = ThisWorkbook

Set ts = tw.ActiveSheet

Set fDialog = Application.FileDialog(3)

ChDir "C:\extract"

With fDialog

.Filters.Clear

.Filters.Add "Excel files", "*.csv*"

.Show



For Each varFile In .SelectedItems

Set nb = Workbooks.Open(Filename:=varFile, local:=True)



With Sheets(1)

.Range("A1:I500").Copy

ThisWorkbook.Sheets("Reconciling Items").Range("A" & Rows.Count).End(xlUp).Offset(1).PasteSpecial xlPasteValues

With ThisWorkbook.Sheets("Reconciling Items").Range("A" & Rows.Count).End(xlUp).Offset(1)

.PasteSpecial xlPasteFormats


End With

End With

nb.Close False

Next

End With

With Sheets("Reconciling Items")

.Range("A1").EntireRow.Delete

.Range("A:J").EntireColumn.AutoFit

End With
With Application

.ScreenUpdating = True

.Calculation = xlCalculationAutomatic

.CutCopyMode = True

End With


End With

ChDir "C:\my documents"

Application.DisplayAlerts = True
End Sub



Your assistance is most appreciated
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Justo inform you that I have managed to sort this out by adding this code below



Code:
 .Range("j1").Value = varFile


After

[/code] With ThisWorkbook.Sheets("Reconciling Items").Range("A" & Rows.Count).End(xlUp).Offset(1)
.PasteSpecial xlPasteFormats [/code]
 
Upvote 0

Forum statistics

Threads
1,214,792
Messages
6,121,612
Members
449,039
Latest member
Mbone Mathonsi

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