So I am completely new to VBA (like just started yesterday and have no programming experience) and was trying to create a macro that took a text file and converts it into an excel worksheet. With the aid of the internet, I was able to piece together the following code get it to work after spending an afternoon looking things over. However, I feel like there might be a more efficient way of handling the row deletion as the worksheet hangs for a bit and eventually gives me the "Error Running Macro" message. Does anyone have any suggestions or see anything that is unnecessary? Appreciate the help!
Sub CustomerStatements()
Dim TestRow As Range
Dim LR As Long, i As Long
DestFile = Application.GetOpenFilename 'Opens Popup that allows me to choose a text file that will use the fixed width parameters below.
If DestFile = False Then
msg = MsgBox("No file was selected")
Exit Sub
End If
On Error GoTo ErrHandler
Workbooks.OpenText Filename:=DestFile, Origin:=437, StartRow:=1, DataType:=xlFixedWidth, FieldInfo:=Array( _
Array(0, 1), Array(7, 1), Array(21, 1), Array(33, 1), Array(59, 1), Array(75, 1), Array(80, _
1)), TrailingMinusNumbers:=True 'Creates columns from text file based on the above Fixed width settings
On Error GoTo 0
Cells.Select
Selection.Columns.AutoFit
ActiveWindow.Zoom = 85
Rows("1:25").Delete 'Deletes First 25 Rows
Range("A1").CurrentRegion.Select
LR = Range("A" & Rows.Count).End(xlUp).Row 'Deletes any row that that doesn't have a date in the first column
For i = LR To 1 Step -1
If Not IsDate(Range("A" & i).Value) Then Rows(i).Delete
Next i 'I feel like there might be a more efficient coding as I get the error messagebox below every time I run this
ErrHandler:
msg = MsgBox("Error Running Macro")
End Sub
Bookmarks