+ Reply to Thread
Results 1 to 10 of 10

Need a macro that will delete a line when "#n/a" appears

Hybrid View

  1. #1
    Registered User
    Join Date
    03-16-2009
    Location
    arkansas
    MS-Off Ver
    Excel 2003
    Posts
    17
    I need some help in finding a macro that will delete a line if the message #N/A appears. Right now I have a macro that will hide a row if it has a zero in it and it loops all the way till it hits "done" would like to add the ability to delete a row with #N/A in it, here is my code for the hide zero macro
    Sub Hide_Zeros()
    'Hides all rows less than zero starting at C11
    
    ActiveSheet.Range("e12").Select
    Do Until ActiveCell.Value = "END"
    ActiveCell.Offset(1, 0).Activate
    Cntr = Cntr + 1
    If ActiveCell.Value <= 0 Then
    ActiveCell.EntireRow.Hidden = "true"
    End If
    Loop
    
    End Sub
    Any help would be awesome, thanks!
    Last edited by Paul; 06-09-2009 at 04:31 PM. Reason: Merged post without code tags and last post with code tags.

  2. #2
    Forum Contributor
    Join Date
    02-23-2006
    Location
    Near London, England
    MS-Off Ver
    Office 2003
    Posts
    770

    Re: Need a macro that will delete a line when "#n/a" appears

    Firstly, you need to add code tags to your post or else you will get your knuckles rapped.
    Edit your post, then click the Advanced button, highlihgt the code, and click the [Code] button. It makes it much easier for people to read and offer you help.

    Incidently you DO realise that your code doesn't start at C11 as indicated in the comment, but at E12 don't you? Also it loops until it reaches "END" not "done", you need to make sure your blurb and your code say the same things otherwise people will get truely confused!

    Also unless you are intialising and then use the Cntr variable outside of this sub it is not actually doing anything.

    Option Explicit
    
    Sub Hide_Zeros_Delete_NA()
    'Hides all rows less than zero starting at E12 and deletes any containing "#N/A"
    
    Dim row_counter as Long
    row_counter=12
    Do While Range("E" & row_counter).value <>"End"
        ' Cntr = Cntr + 1 This is commented out as it is unclear what it is for
        If Range("E" & row_counter).Value <= 0 Then
            Range("E" & row_counter).EntireRow.Hidden = True
        ElseIf Range("E" & row_counter).Value = "#N/A" Then
            Range("E" & row_counter).EntireRow.Delete
            row_counter = row_counter - 1 'Offset it backwards as by deleteing a row you will have moved the data up, so no need to move the counter down.
        End If
        row_counter = row_counter + 1 ' Move to the next row
    Loop
    End Sub
    If you find the response helpful please click the scales in the blue bar above and rate it
    If you don't like the response, don't bother with the scales, they are not for you

  3. #3
    Registered User
    Join Date
    03-16-2009
    Location
    arkansas
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: Need a macro that will delete a line when "#n/a" appears

    thanks for the code Phil, i tried running it and it gave me an error when i hit the #n/a, I don't know if it makes a difference but i copy and paste special values the data into the cells so that the sheet I have doesn't have to recalculate, do you think that would have any affect on it not working?

  4. #4
    Forum Contributor
    Join Date
    02-23-2006
    Location
    Near London, England
    MS-Off Ver
    Office 2003
    Posts
    770

    Re: Need a macro that will delete a line when "#n/a" appears

    What was the error given?

    Is the "#N/A" the actual text in the cell, or an Excel error message, or what is displayed after some custom formatting?

    Is it "#N/A", "#n/a" or "N/a" ? It may be that the case is wrong, but I can't really help anymore without having a copy of the data, or at least knowing what the 'error' was that you got.

  5. #5
    Registered User
    Join Date
    03-16-2009
    Location
    arkansas
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: Need a macro that will delete a line when "#n/a" appears

    The error says Run Time Error '13': Type Mismatch

  6. #6
    Registered User
    Join Date
    03-16-2009
    Location
    arkansas
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: Need a macro that will delete a line when "#n/a" appears

    the cell contains #N/A as the actual text, but it isn't the error message, it is the error message copied and then pasted as a value

  7. #7
    Forum Contributor
    Join Date
    02-23-2006
    Location
    Near London, England
    MS-Off Ver
    Office 2003
    Posts
    770

    Re: Need a macro that will delete a line when "#n/a" appears

    Can you upload an example workbook worksheet of your data?

  8. #8
    Registered User
    Join Date
    03-16-2009
    Location
    arkansas
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: Need a macro that will delete a line when "#n/a" appears

    Okay, i have kind of dumbed down the data but I had to in order to post it. What I want to do is delete any row in column E after row 12 that is either a 0 or an #N/A. The workbook eventually will have about 15,000 lines of data to go through so I will also need to copy and paste the data as values before this starts so that it doesn't have to recalculate every time.
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    03-16-2009
    Location
    arkansas
    MS-Off Ver
    Excel 2003
    Posts
    17

    Re: Need a macro that will delete a line when "#n/a" appears

    any ideas?

  10. #10
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Need a macro that will delete a line when "#n/a" appears

    Perhaps something along the lines of the below ?

    Public Sub Purge()
    Dim rngData As Range, xlCalc As XlCalculation
    Set rngData = Sheets("sheet1").Range(Cells(5, "E"), Cells(Rows.Count, "E").End(xlUp))
    With Application
        xlCalc = .Calculation
        .Calculation = xlCalculationManual
        .EnableEvents = False
        .ScreenUpdating = False
    End With
    With rngData.Offset(, Columns.Count - rngData.Column)
        .FormulaR1C1 = "=IF(ISERROR(RC5),""x"",IF(RC5=0,""x"",1))"
        .Calculate
        .SpecialCells(xlCellTypeFormulas, xlTextValues).EntireRow.Delete
        .Clear
    End With
    Set rngData = Nothing
    With Application
        .Calculation = xlCalc
        .EnableEvents = True
        .ScreenUpdating = True
    End With
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1