Re: Add a column with file name while importing .csv file in vba
hello,
Maybe just write straight to the worksheet
myDir = "C:\Users\ASUS\Documents\Basket Stocks"
delim = vbTab
fn = Dir(myDir & "\*.csv")
Do While fn <> ""
ff = FreeFile
Open myDir & "\" & fn For Input As #ff
Do While Not EOF(ff)
Line Input #ff, txt
x = Split(txt, delim)
If Not flg Then
n = n + 1
Cells(n, 1) = fn
flg = True
End If
If UBound(x) > 0 Then
n = n + 1
Cells(n, 2) = x(2)
Cells(n, 3) = x(1)
End If
Loop
Close #ff
fn = Dir()
flg = False
Loop
Display More
Hi, When running this code on my Scenario, I am getting "Sub or Function not defined" for the 'Cells' function (line 14). I'm assuming I need to specify the file. (I tried 'fn.Cells', 'x.Cells',) but still no joy. Quite new to VBA, can you help? Thank you!