Consulting

Results 1 to 10 of 10

Thread: Solved: Deleting Rows if a particular string doesnt exist in it

  1. #1

    Solved: Deleting Rows if a particular string doesnt exist in it

    I am really the newest newbie in VBA with hardly any knowledge of the terminologies.
    This is a very simple problem I have
    I have to delete the entire row if "." doesnt exist in Column B.
    Also please let me know how to activate a sheet if its name is something else than Sheet1 such that i can generalize it for all the excel sheets i have and add sheets thereafter!

    The final product should be directly pasted in sheet 2!

    [VBA]Sub Delete()
    Dim i As Integer, LastRow As Long
    Worksheets("Sheet1").Activate
    LastRow = ActiveSheet.UsedRange.Rows.Count
    Application.ScreenUpdating = False
    For i = 1 To LastRow

    If IsEmpty(Cells(i, 2)) Then
    Rows(i).Select
    Selection.Delete Shift:=xlUp
    End If
    Next i
    End Sub[/VBA]


    This is what i ran for deleting empty rows!

  2. #2
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    Well for a start don't use Activate or Select/Selection.

    If you want this code to work on all the sheets try something like this.
    [vba]
    For Each ws In Worksheets
    LastRow = ws.UsedRange.Rows.Count
    For I = LastRow To 1 Step -1
    If IsEmpty(ws.Cells(I,2)) Then ws.Cells(I,2).EntireRow.Delete
    Next I
    Next ws
    [/vba]

  3. #3
    Well ok and how abt if i want to delete all the rows whose Column B do not contain the string "." ?? what is the function for tht?

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Sub Delete()
    Dim i As Integer, LastRow As Long
    Worksheets("Sheet1").Activate
    LastRow = ActiveSheet.UsedRange.Rows.Count
    Application.ScreenUpdating = False
    For i = LastRow to 1 Step -1

    If Not Cells(i, 2).Value Like "*.*" Then
    Rows(i).Delete
    End If
    Next i
    Application.ScreenUpdating = True
    End Sub
    [/vba]
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  5. #5
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    What about this?
    [VBA]
    Rows("1:1").Insert
    Columns("B:B").AutoFilter Field:=1, Criteria1:="<>*.*"
    Cells.Delete

    [/VBA]
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

  6. #6
    Hmm how abt if "." is just part of the string and is common with majority of the cells in column B. and these are the cells i wanna retain the remaining i wanna delete...I tried doing this but just not happening
    [vba]Sub Delete()
    Dim i As Integer, LastRow As Long
    Sheets(1).Activate
    LastRow = ActiveSheet.UsedRange.Rows.Count
    Application.ScreenUpdating = False
    Const TEST_STRING As String = "."
    For i = 1 To LastRow

    If Not Right(Cells(i, "B").Value, Len(TEST_STRING)) = TEST_STRING Then
    Rows(i).Select
    Selection.Delete Shift:=xlUp
    End If
    Next i
    End Sub[/vba]


    since . is just part of a huge string.. i want to retain all those strings containing . in them and delete the rest which dont ve . in them

    pl help

  7. #7
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    LastRow = ActiveSheet.UsedRange.Rows.Count
    This will not return the correct value if you don't use Row 1
    Try entering data in rows 8 and 20 of a new sheet and run this
    [VBA]
    Sub Test()
    MsgBox ActiveSheet.UsedRange.Rows.Count
    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'

  8. #8
    But how do i delete the rows which do not contain . in their string. and since . is just part of a huge string.. i want to retain all those strings containing . in them and delete the rest which dont ve . in them

  9. #9
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Isn't that just what we gave you?
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  10. #10
    ohh lol xld my bad didnt do it correctly.thanks topic solved lol!!! i am irritatingly funny

Posting Permissions

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