Consulting

Results 1 to 8 of 8

Thread: Range Values

  1. #1

    Range Values

    Can someone please help? I am trying to detect value in a range of cells from the user input and handle it accordingly by this piece of code.. But the it keeps going into the 'Else' part and pops up MsgBox "Sheet is ready to be submitted".. all the time!



    Sub RangeDetectEntry()
    Dim DataEntry As Range
    Set DataEntry = Range("B1:B3")
    If DataEntry Is Nothing Then
    MsgBox "Enter value"
        Else
            MsgBox "Sheet is ready to be submitted"
        End If
    End Sub

    I modified the code just a bit but it keeps going into the Else part of the If loop ...



    Sub RangeDetectEntry()
        Dim DataEntry As Range, cell As Range
        Set DataEntry = Range("B1:B3")
        
        For Each cell In DataEntry
         If cell Is Nothing Then
             MsgBox "Enter value"
        Else
             MsgBox "Sheet is ready to be submitted"
        End If
        Next cell
    End Sub
    Last edited by Bishma1982; 06-27-2016 at 10:29 AM.

  2. #2
    VBAX Expert
    Joined
    May 2016
    Posts
    604
    Location
    change

    If cell Is Nothing Then
    to
    If cell.Value = "" Then

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,728
    Location
    Sub RangeDetectEntry()
    Dim DataEntry As Range, cell As Range
    Set DataEntry = Range("B1:B3")

    For Each cell In DataEntry
    If cell Is Nothing Then
    MsgBox "Enter value"
    Else
    MsgBox "Sheet is ready to be submitted"
    End If
    Next cell
    End Sub
    Empty is not the samething as Nothing

    DataEntry might have 3 cells with nothing in them, BUT the DataEntry range object has been Set to something, and is therefore not Nothing
    Last edited by Paul_Hossler; 06-27-2016 at 01:56 PM.
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    If the code is not in a worksheet code module:
    Sub RangeDetectEntry() 
        Dim DataEntry As Range 
        Set DataEntry = Sheets("Sheet1").Range("B1:B3") 'Adjust "Sheet1" as needed
        If DataEntry Is Nothing Then 
            MsgBox "Enter value" 
        Else 
            MsgBox "Sheet is ready to be submitted" 
        End If 
    End Sub
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  5. #5
    This helped a lot Offthelip! Thanks..

  6. #6
    Quote Originally Posted by offthelip View Post
    change

    If cell Is Nothing Then
    to
    If cell.Value = "" Then

    This helped a lot. Thanks

  7. #7
    Quote Originally Posted by SamT View Post
    If the code is not in a worksheet code module:
    Sub RangeDetectEntry() 
        Dim DataEntry As Range 
        Set DataEntry = Sheets("Sheet1").Range("B1:B3") 'Adjust "Sheet1" as needed
        If DataEntry Is Nothing Then 
            MsgBox "Enter value" 
        Else 
            MsgBox "Sheet is ready to be submitted" 
        End If 
    End Sub

    Yeah well my code was indeed in a module. I will give this a shot! Thanks

  8. #8
    Quote Originally Posted by Paul_Hossler View Post
    Empty is not the samething as Nothing

    DataEntry might have 3 cells with nothing in them, BUT the DataEntry range object has been Set to something, and is therefore not Nothing
    Finally, I realised my mistake.. this is fantastic. Thanks

Tags for this Thread

Posting Permissions

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