Results 1 to 3 of 3

Thread: validate range of cells

  1. #1

    Thread Starter
    Frenzied Member Jmacp's Avatar
    Join Date
    Jul 2003
    Location
    UK
    Posts
    1,959

    validate range of cells

    how do i , in excel, validate that a range of cells are not empty?

    like,

    for each cells in cells(A4:c5)
    if not cells(i) = "" then

    ?

  2. #2
    Frenzied Member DKenny's Avatar
    Join Date
    Sep 2005
    Location
    on the good ship oblivion..
    Posts
    1,171

    Re: validate range of cells

    Here's a function that will perform this check, it returns TRUE if all cells in the range have a value.
    VB Code:
    1. Function ValidateRange(CheckCells As Range) As Boolean
    2. Dim rngCell As Range
    3.    
    4.     'Assume all cells have values
    5.     ValidateRange = True
    6.    
    7.     For Each rngCell In CheckCells
    8.        
    9.         'if a blank is found...
    10.         If rngCell.Value = "" Then
    11.            
    12.             '...return FALSE
    13.             ValidateRange = False
    14.             '...and discontinue checking
    15.             Exit For
    16.            
    17.         End If
    18.        
    19.     Next rngCell
    20.    
    21. End Function
    Last edited by DKenny; May 25th, 2006 at 09:13 AM.
    Declan

    Don't forget to mark your Thread as resolved.
    Take a moment to rate posts that you think are helpful

  3. #3
    Frenzied Member
    Join Date
    May 2004
    Location
    Carlisle, PA
    Posts
    1,045

    Re: validate range of cells

    Here is an alternative using the Worksheet "COUNTA" function:

    COUNTA

    Counts the number of cells that are not empty and the values within the list of arguments. Use COUNTA to count the number of cells that contain data in a range or array.
    Code:
    Option Explicit
    Sub CheckRangeForEmptyCells()
        'This routine ignores any and all cell formatting.
        Dim aRange As Range
    
        'Set the range here however you want.
        'I'm just using "Selection" as a test example
        Set aRange = Selection
        
        'Here is a prototype test ... you can also just use the CountA function
        If Application.CountA(aRange) = aRange.Cells.Count Then
            'There are NO empty cells here ... all cells are occupied
            MsgBox "All Selected cells are occupied!"
        Else
            'There is at least 1 empty cell in the defined range
            MsgBox "There are some empty cells in the selected range."
        End If
    
        'Clean House
        Set aRange = Nothing
    
    End Sub
    Good Luck and Good Programming!
    Blessings in abundance,
    All the Best,
    & ENJOY!

    Art . . . . Carlisle, PA . . USA

Posting Permissions

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



Click Here to Expand Forum to Full Width