qwertyuiop2021
New Member
- Joined
- Aug 4, 2021
- Messages
- 5
- Office Version
- 2013
- Platform
- Windows
Hi board,
I have a project with VBA and I have never worked with VBA before. This project needs me to merge multiple csv files into one new file. After merging, I need to filter out columns with header names that I choose (which can be changed and automatically get the header names). This is the code I use to merge files and I want to filter after merging on this code too.
Thank you!!
I have a project with VBA and I have never worked with VBA before. This project needs me to merge multiple csv files into one new file. After merging, I need to filter out columns with header names that I choose (which can be changed and automatically get the header names). This is the code I use to merge files and I want to filter after merging on this code too.
Thank you!!
VBA Code:
[/
Sub MyCombineMacro()
Dim myDataFolder As String
Dim myMacroFile As Workbook
Dim myNewFile As Workbook
Dim myDataFile As Workbook
Dim myLastFile As Workbook
Dim myFileName As String
Dim cnt As Long
Dim myNewFileName As String
Application.ScreenUpdating = False
' Thu muc chua testlog
myDataFolder = "D:\CT_Test\TASK\TestLog\"
Set myMacroFile = ActiveWorkbook
Workbooks.Add
Set myNewFile = ActiveWorkbook
myFileName = Dir(myDataFolder & "*.csv")
Do While Len(myFileName) > 0
cnt = cnt + 1
' Open file testlog
Set myDataFile = Workbooks.Open(Filename:=myDataFolder & myFileName)
' Read and get data each file
If cnt = 1 Then
Range(Cells(9, 1), Cells(10, 16384)).Copy
myNewFile.Activate
Range("A1").Select
Else
Range(Cells(10, 1), Cells(10, 16384)).Copy
myNewFile.Activate
Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Select
End If
ActiveSheet.Paste
Application.CutCopyMode = False
myDataFile.Close
myFileName = Dir
Loop
' Save New File
myNewFileName = "D:\CT_Test\TASK\TestResult\" & "log" & Format(Now, "hhmmssddmmyy") & ".csv"
myLastFile.Activate
ActiveWorkbook.SaveAs Filename:=myNewFileName, FileFormat:=xlCSV, CreateBackup:=False
myLastFile.Close
Application.ScreenUpdating = True
End Sub
]
[ATTACH type="full"]44398[/ATTACH]
[ATTACH type="full"]44397[/ATTACH]