I have an CVS file that have amount of data over hundred thousands.
Because the data file have many space not regular, I used filter "space" to filter one by one columns. After filter, I copy this column and paste to another sheet. I do those steps until the column data end.
My file have many columns and hundreds thousand rows, but after filter "space" that about 100 thousands.
But now I had a problem, I had wait too long about 5 minutes to finish this wrok.
How could I run faster?
I try to use Selection.SpecialCells(xlCellTypeVisible).Copy
, took more time.
Thanks!
Below is my excel VBA
filter space and copy paste code
Sub FilterData()
On Error GoTo ErrorHandler
Dim AddSheetName As String
Dim CSVNoExtensionName As String
Dim LastColumn As Long
Dim FinalRow As Variant
Dim idxDataCol, idxPasteCol As Integer
Dim sDelayTime As String
sDelayTime = "02"
AddSheetName = "sheet1"
Dim Time0#
Time0 = Timer
Workbooks(CSVDataFileName).Activate
If InStr(CSVDataFileName, ".") > 0 Then
CSVNoExtensionName = Left(CSVDataFileName, InStr(CSVDataFileName, ".") - 1)
End If
Sheets.Add(After:=ActiveSheet).Name = AddSheetName
Worksheets(CSVNoExtensionName).Activate
LastColumn = Cells(1, Columns.Count).End(xlToLeft).Column
FinalRow = Range("A1").End(xlDown).Row
idxPasteCol = 1
For idxDataCol = 2 To LastColumn Step 1
Cells(1, idxDataCol).Select
Selection.AutoFilter
ActiveSheet.Range(Cells(1, 1), Cells(FinalRow, LastColumn)).AutoFilter Field:=idxDataCol, Criteria1:="<>"
Dim rng1, rng2 As Range
Set rnge2 = Range(Cells(1, idxDataCol), Cells(FinalRow, idxDataCol))
Set rng1 = Union(Range("A1:A" & FinalRow), rnge2)
rng1.Select
Selection.Copy
Application.Wait (Now + TimeValue("0:00:" & sDelayTime))
Sheets(AddSheetName).Select
ActiveSheet.Cells(1, idxPasteCol).Select
ActiveSheet.Paste
Columns(idxPasteCol).Font.ColorIndex = 41
Sheets(CSVNoExtensionName).Select
Application.CutCopyMode = False
Selection.AutoFilter
idxPasteCol = idxPasteCol + 2
Next idxDataCol
ActiveSheet.Cells(1, 1).Select
Application.DisplayAlerts = False
ActiveWorkbook.SaveAs FileName:=CSVNoExtensionName & ".xlsx", FileFormat:=xlOpenXMLStrictWorkbook, CreateBackup:=False
Exit Sub
End Sub