I need help in working with big Excel Tables.
Description
I have an export of Data from our ERP System that has 400K Rows at least. In this report the format is quite messed up and I want to write a script that will clean up all the data.
I started to write little sub just to delete empty rows and such that have a special behavior. please see below:
Sub Main()
OptimizeVBA (True)
DeleteLastRows
OptimizeVBA (False)
End Sub
Sub DeleteLastRows()
'Achtung, diese Funktion dauert sehr lange
Dim total
total = ActiveSheet.UsedRange.Rows.Count
Dim Tim1 As Single
Tim1 = Timer
For i = total To total - 100 Step -1
If ThatSpecialLine("0", i, 1, 9) Then
'DeleteRow (i)
Rows(i).EntireRow.Delete
ElseIf EmptyRow(i, 1, 13) Then
'DeleteRow (i)
Rows(i).EntireRow.Delete
End If
Next
Tim1 = Timer - Tim1
MsgBox ("Anzahl der Zeilen nach der Bearbeitung: " & ActiveSheet.UsedRange.Rows.Count & vbNewLine & "Dafür wurde: " & Tim1 & " gebraucht")
End Sub
Function EmptyRow(ByVal Row As Long, ByVal startc As Integer, ByVal EndC As Integer) As Boolean
EmptyRow = True
Dim temp As String
For i = startc To EndC
temp = Cells(Row, i).Value
temp = Trim(temp)
If temp <> "" Then
EmptyRow = False
Exit Function
End If
Next
End Function
Function ThatSpecialLine(ByVal val As String, ByVal Row As Long, ByVal startc As Integer, ByVal EndC As Integer) As Boolean
ThatSpecialLine = False
If EmptyRow(Row, startc, EndC) Then
If Cells(Row, EndC + 1).Value = val Then
ThatSpecialLine = True
End If
End If
End Function
Sub OptimizeVBA(isOn As Boolean)
Application.Calculation = IIf(isOn, xlCalculationManual, xlCalculationAutomatic)
Application.EnableEvents = Not (isOn)
Application.ScreenUpdating = Not (isOn)
ActiveSheet.DisplayPageBreaks = Not (isOn)
End Sub
This code needs about 14 seconds to execute for just 100 lines. I am wondering why the performance is so bad. I have no experience in making an application performance optimizing so please be kind if my question is very stupid :).
Questions
- Would it be better / faster to export this .xlsx file to .txt file and process with a programm i write in Visual studio with vb.net or C#? this would be my next idea.
- How to improve my vba code?
Would it be better / faster to export this .xlsx file to .txt file and process with a programm i write in Visual studio with vb.net or C#? this would be my next idea.
Thanks in advance