Filter columns with header name

qwertyuiop2021

New Member
Joined
Aug 4, 2021
Messages
5
Office Version
  1. 2013
Platform
  1. 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!!

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]
 

Attachments

  • Capture.PNG
    Capture.PNG
    151.3 KB · Views: 10
  • merging.PNG
    merging.PNG
    182.1 KB · Views: 10

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.

Forum statistics

Threads
1,214,965
Messages
6,122,495
Members
449,088
Latest member
Melvetica

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