Consulting

Results 1 to 11 of 11

Thread: Solved: Macro to delete the row

  1. #1
    VBAX Mentor
    Joined
    Sep 2007
    Posts
    405
    Location

    Solved: Macro to delete the row

    Hi All,

    I am new to VBA... can you please helpme out in resolving my issue....

    My concern is

    i have a excel sheet with numerous data.... i want to check column A and delete the entire row if any cell in colum A contains value other than numbers..

    i want this to be done using macros as data will be greater than 25000 rows....

    Thanks for your help in advance..!

    Regards,
    Sindhuja

  2. #2
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,060
    Location
    And if its an alphanumerical string?
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

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

    Public Sub DeleteAFterr()
    Const TEST_COLUMN As String = "A" '<=== change to suit
    Dim i As Long
    Dim LastRow As Long
    Dim rng As Range
    Dim sh As Worksheet

    With Application

    .ScreenUpdating = False
    .Calculation = xlCalculationManual
    End With

    Set sh = ActiveSheet

    With sh

    LastRow = .Cells(.Rows.Count, TEST_COLUMN).End(xlUp).Row


    iLastRow = .Cells(.Rows.Count, TEST_COLUMN).End(xlUp).Row
    For i = 1 To iLastRow 'iLastRow to 1 Step -1

    If IsNumeric(.Cells(i, TEST_COLUMN).Value) Then

    If rng Is Nothing Then

    Set rng = .Rows(i)
    Else

    Set rng = Union(.Rows(i), rng)
    End If
    End If
    Next i

    If Not rng Is Nothing Then rng.Delete

    End With

    With Application

    .Calculation = xlCalculationAutomatic
    .ScreenUpdating = True
    End With

    Set sh = Nothing
    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

  4. #4
    VBAX Mentor
    Joined
    Sep 2007
    Posts
    405
    Location
    Quote Originally Posted by xld
    [vba]

    Public Sub DeleteAFterr()
    Const TEST_COLUMN As String = "A" '<=== change to suit
    Dim i As Long
    Dim LastRow As Long
    Dim rng As Range
    Dim sh As Worksheet

    With Application

    .ScreenUpdating = False
    .Calculation = xlCalculationManual
    End With

    Set sh = ActiveSheet

    With sh

    LastRow = .Cells(.Rows.Count, TEST_COLUMN).End(xlUp).Row


    iLastRow = .Cells(.Rows.Count, TEST_COLUMN).End(xlUp).Row
    For i = 1 To iLastRow 'iLastRow to 1 Step -1

    If IsNumeric(.Cells(i, TEST_COLUMN).Value) Then

    If rng Is Nothing Then

    Set rng = .Rows(i)
    Else

    Set rng = Union(.Rows(i), rng)
    End If
    End If
    Next i

    If Not rng Is Nothing Then rng.Delete

    End With

    With Application

    .Calculation = xlCalculationAutomatic
    .ScreenUpdating = True
    End With

    Set sh = Nothing
    End Sub
    [/vba]

    Hi,

    Thanks for all your help.

    The code works fine in deleting the rows which has numbers as the value in column A.

    But my requirement is i want the rows to be deleted with the values as alphabets or alpha numerics in column A....


    Regards,
    Sindhuja

    p

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Back to fron.

    Change

    [vba]

    If IsNumeric(.Cells(i, TEST_COLUMN).Value) Then
    [/vba]

    to

    [vba]

    If Not IsNumeric(.Cells(i, TEST_COLUMN).Value) Then
    [/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

  6. #6
    VBAX Mentor
    Joined
    Sep 2007
    Posts
    405
    Location
    Hi Xld...

    Its perfect !!!!
    Thanks a lot for your timely help...

    Have a nice day

    Regards,
    Sindhuja

  7. #7
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,060
    Location
    Bob, 2 questions in relation to your code if I may. In the initial section where you declare the variables, I noticed iLastRow is not declared. Is this because "Option Explicit" was not used or is it not necessary?

    Quote Originally Posted by xld
    [vba]
    Dim i As Long
    Dim LastRow As Long
    Dim rng As Range
    Dim sh As Worksheet

    [/vba]
    Later on, within the Set sh = Active Sheet section you indicate that LastRow and iLastRow are the same thing. Could we get away with not indicating the Lastrow bit?

    Quote Originally Posted by xld
    [vba]
    Set sh = ActiveSheet

    With sh

    LastRow = .Cells(.Rows.Count, TEST_COLUMN).End(xlUp).Row

    iLastRow = .Cells(.Rows.Count, TEST_COLUMN).End(xlUp).Row

    [/vba]
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  8. #8
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Ted,

    That was a typo on my part. They should both be LastRow, I was meaning use Lastrow throughout. I changed my template code today and messed up by the looks. Sindhuja probably got away with it because he doesn't use Option Explicit.

    Thanks for the heads up, I will correct my template.
    ____________________________________________
    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

  9. #9
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,060
    Location
    Grasshopper very pleased with his effort Master.

    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  10. #10
    VBAX Mentor
    Joined
    Sep 2007
    Posts
    405
    Location
    Hi All,

    Thanks for the informations !!

    It really helped me out and learnt a lot...

    Regards,
    Sindhuja

  11. #11
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,060
    Location
    G'day sindhuja,

    If you are happy with the response then please use the Thread Tools options at the top of the page to mark this thread "solved".

    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Posting Permissions

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