Consulting

Results 1 to 13 of 13

Thread: Solved: read from list and delete rows if cells contains expression from that list

  1. #1
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location

    Solved: read from list and delete rows if cells contains expression from that list

    [VBA]
    ub Read_text_File()

    Dim oFSO As New FileSystemObject
    Dim oFS
    Dim luka As Range
    Set oFS = oFSO.OpenTextFile("C:\list.txt")


    Do Until oFS.AtEndOfStream
    STEXT = oFS.ReadLine
    R = ActiveSheet.UsedRange.Rows.Count


    Range("A1").Select
    For i = 1 To R
    Set luka = Cells.Find(What:=STEXT, After:=ActiveCell, LookIn:=xlFormulas _
    , LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
    MatchCase:=False, SearchFormat:=False)
    If Not luka Is Nothing Then
    If STEXT = "" Then
    GoTo znova
    End If
    luka.Select
    Selection.Delete shift:=xlUp
    ActiveCell.Offset(-1, 0).Select
    'End If
    Else
    GoTo znova:
    End If
    Next i
    znova:
    'End If
    Loop
    R = ActiveSheet.UsedRange.Rows.Count


    Range("A1").Select
    'For i = 1 To R

    On Error Resume Next ' In case there are no blanks
    Columns("A:A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    ' Next i


    End Sub[/VBA]

    I have a normal txt file which software names in each line
    (actually not full software name but just partial name e.g : instead microsoft office I have just microsoft)

    So when this keyword is found in excel row whole row should get deleted, so leaving no empty rows, but for some reason it doesnt work well, I found that a lot entries are deleted even if keyword is not in txt file)

    thank you very much for your answers

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Untested but try
    [VBA]
    Option Explicit
    Sub Read_text_File()
    Dim oFSO As New FileSystemObject
    Dim oFS
    Dim luka As Range
    Dim FirstAddress As String
    Dim STEXT As String

    Set oFS = oFSO.OpenTextFile("C:\list.txt")
    Do Until oFS.AtEndOfStream
    STEXT = oFS.ReadLine
    With Worksheets(1).Cells
    Set luka = Cells.Find(What:=STEXT, After:=Range("A1"), LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, _
    MatchCase:=False, SearchFormat:=False)
    If Not luka Is Nothing Then
    FirstAddress = luka.Address
    Do
    luka.EntireRow.Delete
    Set luka = .FindNext(luka)
    Loop While Not luka Is Nothing And luka.Address <> FirstAddress
    End If
    End With
    Loop
    On Error Resume Next ' In case there are no blanks
    Columns("A:A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    End Sub
    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location
    thnank you very much will let you know if it works

  4. #4
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location
    I get an error
    Set luka = .FindNext(luka)
    "unable to get property find next..."
    error 1004

    Any ideas why

  5. #5
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    [VBA]Option Explicit
    Sub Read_text_File()
    Dim oFSO As New FileSystemObject
    Dim oFS
    Dim luka As Range, d As Range
    Dim FirstAddress As String
    Dim STEXT As String

    Set oFS = oFSO.OpenTextFile("C:\list.txt")
    Do Until oFS.AtEndOfStream
    STEXT = oFS.ReadLine
    With Worksheets(1).Cells
    Set luka = Cells.Find(What:=STEXT, After:=ActiveCell, LookIn:=xlFormulas, _
    LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, _
    MatchCase:=False, SearchFormat:=False)
    If Not luka Is Nothing Then
    FirstAddress = luka.Address
    Do
    Set d = luka
    On Error GoTo Exits
    Set luka = .FindNext(luka)
    d.EntireRow.Delete
    Loop While Not luka Is Nothing
    End If
    End With
    Exits:
    On Error Resume Next ' In case there are no blanks
    Columns("A:A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    End Sub

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  6. #6
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location
    Why this [vba]SearchDirection:=xlPrevious[/vba]

    Somehow it doesnt go through all the list

    This throws an error and becaus eof on error resume next the macro finishes with deleting row
    [VBA] On Error Goto Exits
    Set luka = .FindNext(luka)
    d.EntireRow.Delete
    Loop While Not luka Is Nothing
    End If
    End With
    Exits:
    On Error Resume Next ' In case there are no blanks
    Columns("A:A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    [/VBA]

  7. #7
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location
    the search is more like 10,000 rows than 10

  8. #8
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Can you post a small sample of the workbook and text file?
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  9. #9
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location
    I have did this like this
    what do you think
    [VBA]Option Explicit
    Sub Read_text_File()

    Dim oFSO As New FileSystemObject
    Dim oFS
    Dim luka As Range, d As Range
    Dim FirstAddress As String
    Dim STEXT As String
    Dim chosen
    Dim name
    With Application.FileDialog(msoFileDialogFilePicker)
    .AllowMultiSelect = False
    .Title = "Choose your list of software you want to exclude from list"
    .ButtonName = "Yes this is my list"
    .Filters.Add "Documents", "*.txt"
    .FilterIndex = 2
    If .Show = -1 Then
    chosen = Application.FileDialog(msoFileDialogFilePicker).SelectedItems.Item(1)
    Else
    Unload UserForm1
    Exit Sub
    End If
    End With
    Set oFS = oFSO.OpenTextFile(chosen)
    Do Until oFS.AtEndOfStream
    STEXT = oFS.ReadLine
    'With Worksheets(1).Cells
    ' Set luka = Cells.Find(What:=STEXT, After:=ActiveCell, LookIn:=xlFormulas, _
    ' LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlPrevious, _
    ' MatchCase:=False, SearchFormat:=False)
    Dim Firstrow As Long
    Dim Lastrow As Long
    Dim Lrow As Long
    Dim CalcMode As Long
    Dim ViewMode As Long
    With Application
    CalcMode = .Calculation
    .Calculation = xlCalculationManual
    ' .ScreenUpdating = False
    End With
    'We use the ActiveSheet but you can replace this with
    'Sheets("MySheet")if you want
    With ActiveSheet
    'We select the sheet so we can change the window view
    .Select
    'If you are in Page Break Preview Or Page Layout view go
    'back to normal view, we do this for speed
    ViewMode = ActiveWindow.View
    ActiveWindow.View = xlNormalView
    'Turn off Page Breaks, we do this for speed
    .DisplayPageBreaks = False
    'Set the first and last row to loop through
    Firstrow = .UsedRange.Cells(1).Row
    Lastrow = .UsedRange.Rows(.UsedRange.Rows.Count).Row
    'We loop from Lastrow to Firstrow (bottom to top)
    For Lrow = Lastrow To Firstrow Step -1
    'We check the values in the A column in this example
    With .Cells(Lrow, "A")
    If Not IsError(.Value) Then

    Set luka = Cells.Find(What:=STEXT, After:=ActiveCell, _
    LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
    SearchDirection:=xlNext, MatchCase:=False)
    If luka Is Nothing Then GoTo naprej Else luka.Select
    'This will delete each row with the Value "ron"
    'in Column A, case sensitive.
    'If luka = Then .EntireRow.Delete
    End If
    If STEXT = "" Then GoTo konec

    'End If
    Selection.EntireRow.Delete
    End With
    Next Lrow
    naprej:
    End With
    ' ActiveWindow.View = ViewMode
    'With Application
    ' .ScreenUpdating = True
    ' .Calculation = CalcMode
    'End With
    Loop
    konec:
    Columns("A:A").SpecialCells(xlCellTypeBlanks).EntireRow.Delete
    MsgBox "Clean-up complete"
    'On Error Resume Next ' In case there are no blanks

    End Sub

    [/VBA]

  10. #10
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    In this extract, you are carrying out a search of all cells once for each row in your range. You could be searching 100 times for 1 return. Using the FindNext method should loop the required numbers of times only.

    [VBA]For Lrow = Lastrow To Firstrow Step -1
    'We check the values in the A column in this example
    With .Cells(Lrow, "A")
    If Not IsError(.Value) Then

    Set luka = Cells.Find(What:=STEXT, After:=ActiveCell, _
    LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
    SearchDirection:=xlNext, MatchCase:=False)
    If luka Is Nothing Then GoTo naprej Else luka.Select
    'This will delete each row with the Value "ron"
    'in Column A, case sensitive.
    'If luka = Then .EntireRow.Delete
    End If
    If STEXT = "" Then GoTo konec

    'End If
    Selection.EntireRow.Delete
    End With
    Next Lrow[/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  11. #11
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location
    I see that way speed preformance would be way better than now but the results would be 100% identical

    Am I thinking right??

    Thanx again for all your help

  12. #12
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    That's correct.
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  13. #13
    VBAX Mentor
    Joined
    Jan 2006
    Posts
    348
    Location
    thnx man I appreciate your help very much

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •