Sub ImportData()
' First it imports the file
Dim ResultStr As String
Dim FileName As String
Dim FileNum As Integer
Dim Counter As Double
'Ask User for File's Name
FileName = Application.GetOpenFilename
'Check for no entry
If FileName = "" Then End
'Get Next Available File Handle Number
FileNum = FreeFile()
'Open Text File For Input
Open FileName For Input As #FileNum
'Turn Screen Updating Off
Application.ScreenUpdating = False
'Create A New WorkBook With One Worksheet In It
Workbooks.Add Template:=xlWorksheet
'Set The Counter to 1
Counter = 1
' Loop Until the End Of File Is Reached
Do While Seek(FileNum) <= LOF(FileNum)
'Display Importing Row Number On Status Bar
Application.StatusBar = "Importing Row " & Counter & " of text file " _
& FileName
'Store One Line Of Text From File To Variable
Line Input #FileNum, ResultStr
'Store Variable Data Into Active Cell
If Left(ResultStr, 1) = "=" Then
ActiveCell.Value = "'" & ResultStr
Else
ActiveCell.Value = ResultStr
End If
If ActiveCell.Row = 65536 Then
'If On The Last Row Then Add A New Sheet
ActiveWorkbook.Sheets.Add
Else
'If Not The Last Row Then Go One Cell Down
ActiveCell.Offset(1, 0).Select
End If
'Increment the Counter By 1
Counter = Counter + 1
'Start Again At Top Of 'Do While' Statement
Loop
'Close The Open Text File
Close
'Remove Message From Status Bar
Application.StatusBar = False
Columns("A:A").Select
Selection.TextToColumns Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=False, _
Semicolon:=False, Comma:=True, Space:=False, Other:=False, FieldInfo _
:=Array(Array(1, 2), Array(2, 2), Array(3, 2), Array(4, 2)), TrailingMinusNumbers:= _
True
' Then the filtering follows
Dim rng As Range
Dim calcmode As Long
Dim myArr As Variant
Dim I As Long
With Application
calcmode = .Calculation
.Calculation = xlCalculationManual
.ScreenUpdating = False
End With
'The values for the filter (the 23 criteria, it deletes a row for each even if it shouldn't)
myArr = Array("01*", "02*", "04*", "0999*", "10*", "110*", "111*", "112*", "113*", "114*", "115*", "12*", "13*", "14*", "15*", "16*", "17*", "18*", "19*", "5*", "6*", "8*", "9*")
For I = LBound(myArr) To UBound(myArr)
'Sheet with the data, you can also use Sheets("MySheet")
With ActiveSheet
'Firstly, remove the AutoFilter
.AutoFilterMode = False
'Apply the filter
.Range("A1:A" & .Rows.Count).AutoFilter Field:=1, Criteria1:=myArr(I)
Set rng = Nothing
With .AutoFilter.Range
On Error Resume Next
Set rng = .Offset(0, 0).Resize(.Rows.Count - 1, 1) _
.SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If Not rng Is Nothing Then rng.EntireRow.Delete
End With
'Remove the AutoFilter
.AutoFilterMode = False
End With
Next I
With Application
.Calculation = calcmode
End With
' Then some copying of final data follows
Range("A:A,C:C").Select
Range("C1").Activate
Selection.Copy
Windows("Audit Tool.xls").Activate
Sheets("Sheet1").Activate
Range("A1").Select
ActiveSheet.Paste
Dim Lastrow As Long
Lastrow = Range("A" & Rows.Count).End(xlUp).Row
Sheets("Sheet2").Columns("A:B").ClearContents
Range("A1:A" & Lastrow).Copy Sheets("Sheet2").Range("A3")
Range("B1:B" & Lastrow).Copy Sheets("Sheet2").Range("B3")
Sheets("Sheet2").Activate
Range("A1").Select
End Sub
Please look into the code and help me figure out what is wrong with it.
Bookmarks