+ Reply to Thread
Results 1 to 3 of 3

Counting number of non-blank cells in column......but getting run time error in vba

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Counting number of non-blank cells in column......but getting run time error in vba

    Hi,

    I have set up a range in vba as follows (see rngsourcedata):



    Dim fl_name As String
    
    Dim rngsourcedata As Range
    
    fl_name = ThisWorkbook.Name
    
    Set wrksht_data = Workbooks(fl_name).Worksheets(1)
    
    status_col = 10
    
    Set rngsourcedata = wrksht_data.Columns(status_col).SpecialCells(xlCellTypeConstants)

    The problem is that if status_col is "empty" then i get a run time error.

    I thought if a range is empty that it would evaluate to "Nothing"......not cause a runtime error.......


    any thoughts/ideas?
    Last edited by welchs101; 04-30-2012 at 01:18 PM.

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,482

    Re: Counting number of non-blank cells in column......but getting run time error in vba

    That particular function returns a valid reference or an error. It's the same as doing Goto > Special > Constants. That would return a dialog reporting no cells found.

    So you need to trap error

    Sub X()
    Dim fl_name As String
    Dim wrksht_data As Worksheet
    Dim rngsourcedata As Range
    Dim status_col As Long
    
    fl_name = ThisWorkbook.Name
    
    Set wrksht_data = Workbooks(fl_name).Worksheets(1)
    
    status_col = 10
    On Error Resume Next
    Set rngsourcedata = wrksht_data.Columns(status_col).SpecialCells(xlCellTypeConstants)
    On Error GoTo 0
    If rngsourcedata Is Nothing Then
        MsgBox "No Matching cells"
    Else
        MsgBox "Found Cells " & rngsourcedata.Address
    End If
    
    End Sub
    Cheers
    Andy
    www.andypope.info

  3. #3
    Forum Contributor
    Join Date
    12-01-2007
    Location
    USA-North Carolina
    MS-Off Ver
    MS Office 2016
    Posts
    2,712

    Re: Counting number of non-blank cells in column......but getting run time error in vba

    thanks......exactly the kind of info i was looking for........thanks. exactly what i needed.

+ 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