+ Reply to Thread
Results 1 to 4 of 4

Trying to pick up an NA value in a cell when looping - can't get to work!

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    04-13-2011
    Location
    Havant, Hants, England
    MS-Off Ver
    Excel 2010
    Posts
    116

    Trying to pick up an NA value in a cell when looping - can't get to work!

    Dear all,

    I'm trying to loop across row 2 (in my test file attached) and identify when the cell value is NA (as in cell J2). I want to identify it to be able to delete the column but am having trouble writing the correct code to do this (my latest attempts isattached as text and in the file). Can anybody point me in the right direction?

    thanks,

    neil

    Dim WSD As Worksheet    'Object variables available throughout this module
    Dim FinalCol As Single
    
    Sub Macro3()
    Dim StrTitle As String
    
        FinalCol = ActiveSheet.Cells(3, Application.Columns.Count).End(xlToLeft).Column
    
        For i = 1 To FinalCol
            Cells(2, i).Select
            StrTitle = ActiveCell.Value
           ' MsgBox StrTitle
            ActiveCell.Value = "= IF(ISNA(StrTitle),""blank"", StrTitle)"
           
        Next i
    End Sub
    Test File.xlsm
    Last edited by Leith Ross; 12-18-2012 at 01:07 PM. Reason: Moved link out of code block

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Trying to pick up an NA value in a cell when looping - can't get to work!

    Hello highlystrung,

    It is easier and faster to use the Find method when searching for a cell value. Here is an example. This will change "#N/A" when found to "blank".
    Sub Macro4()
    
        Dim Cell As Range
        Dim Rng As Range
        
            Set Rng = Cells(2, "A").EntireRow
            
            Set Cell = Rng.Find("#N/A", , xlValues, xlWhole, xlByColumns, xlNext, False)
            If Not Cell Is Nothing Then Cell.Value = "blank"
            
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Forum Expert contaminated's Avatar
    Join Date
    05-07-2009
    Location
    Baku, Azerbaijan
    MS-Off Ver
    Excel 2013
    Posts
    1,430

    Re: Trying to pick up an NA value in a cell when looping - can't get to work!

    Hello

    Maybe this one

    Sub Test()
        Dim UsedColumns As Integer
        
        UsedColumns = Range("A1").CurrentRegion.Columns.Count
        
        For x = 1 To UsedColumns
            If WorksheetFunction.IsNA(Cells(2, x)) Or Cells(2, x) = "#N/A" Then Cells(2, x).Value = "Blank"
        Next x
    End Sub
    PS: didn't see Leith's code )))
    Last edited by contaminated; 12-18-2012 at 01:33 PM.
    Люди, питающие благие намерения, как раз и становятся чудовищами.

    Regards, ?Born in USSR?
    Vusal M Dadashev

    Baku, Azerbaijan

  4. #4
    Forum Contributor
    Join Date
    04-13-2011
    Location
    Havant, Hants, England
    MS-Off Ver
    Excel 2010
    Posts
    116

    Re: Trying to pick up an NA value in a cell when looping - can't get to work!

    Thanks guys, I'll try both, Neil

+ 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