
Originally Posted by
shg
Set wkbCsv = Application.Workbooks.Open(Filename:=strFOLD & strFile)
Yes. It works. Thank you.
There is another problem.
My original recorded macro can do the work completely for one file, but I can not put Step 5 in the loop in polished version of macro (next code).
'Step5: Inserting FileName as "ID" in ColA until the last row of data
Range("A2").Select
ActiveWorkbook.Names.Add Name:="ID", RefersToR1C1:="=GET.DOCUMENT(88)"
ActiveWorkbook.Names("ID").Comment = ""
ActiveCell.FormulaR1C1 = "=SUBSTITUTE(ID, "".csv"", """")"
Range("A2").Select
Dim Lr As Long
Lr = Cells.Find("*", , , , 1, 2).Row
If Selection(1).Row < Lr Then
Selection.AutoFill Destination:=Selection.Resize(Lr - Selection.Row + 1)
End If
This one is the polished one. The step 5 is not included in this code.
Sub Macro_data_L3()
' Set folder containing CSV files
Const strFOLD As String = "C:\Users\test\New folder\"
' Set formula to be used
Const strFORMULA As String = "GET.DOCUMENT(88)"
Dim strFile As String
Dim wkbCsv As Workbook
Dim lngLastRow As Long
strFile = Dir$(strFOLD & "*.csv", vbNormal)
Do While strFile <> vbNullString
Set wkbCsv = Application.Workbooks.Open(Filename:=strFOLD & strFile)
With wkbCsv.Worksheets(1) 'step1: Deleting 1st row
.Rows("1").Delete
With .Columns("B")
.Clear
.Insert
End With
'Step2: TXT2Col
.Columns("A").TextToColumns _
Destination:=.Range("A1"), _
Other:=True, _
OtherChar:=Chr(47), _
FieldInfo:=Array( _
Array(1, xlGeneralFormat), _
Array(2, xlGeneralFormat), _
Array(3, xlGeneralFormat))
'Step3: Changing cell format to DMY
.Columns("A:C").ClearFormats
.Range("A1:C1").Value = Array("Day", "Month", "Year")
'Ste4: Swapping Columns
.Columns("C").Cut
.Columns("A").Insert
.Columns("B").Cut
.Columns("D").Insert
.Columns("A").Insert
' Step 5
.Range("A1").Value = "ID"
lngLastRow = .Range("B" & .Rows.Count).End(xlUp).Row
.Range("A2:A" & lngLastRow).Formula = _
"=SUBSTITUTE(" & strFORMULA & ", "".CSV"", """")"
' This part is incomplete !
strFile = Dir$()
End With
Loop
End Sub
Bookmarks