Consulting

Results 1 to 5 of 5

Thread: Check if Excel cell is not number

  1. #1

    Check if Excel cell is not number

    Hi!
    I'm trying to check if cell in excel isn't a number.
    But it gives me error.
    What seems to be issue?
    Sub isNotNr()
        Dim input As Single
        Dim WS As Worksheet
        Set WS = ThisWorkbook.Worksheets("Sheet1")
        input = WS.Range("B18").Value
        Range("B18").Select
        If input = 0 Or input < 0 Or IsNumeric(input) = False Then
          MsgBox "Check input"
          Exit Sub
         End If
    End Sub
    Thanks!

  2. #2
    VBAX Expert
    Joined
    Aug 2004
    Posts
    810
    Location
    perhaps you are getting error is because "input" is a keyword

  3. #3
    Moderator VBAX Guru Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    4,997
    Location
    To get the best help you should include what the error is that you are getting.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  4. #4
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,711
    Location
    Just taking a WAG here since you didn't say what the error was or what input value caused it

    Option Explicit
    
    
    Sub isNotNr()
        Dim N As Variant
        Dim WS As Worksheet
        
        Set WS = ThisWorkbook.Worksheets("Sheet1")
        N = WS.Range("B18").Value
        
        If IsNumeric(N) Then
            If N <= 0 Then
                MsgBox "Check input"
                Exit Sub
            End If
        Else
            MsgBox "Check input"
        End If
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    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

  5. #5
    Quote Originally Posted by Paul_Hossler View Post
    Just taking a WAG here since you didn't say what the error was or what input value caused it

    Option Explicit
    
    
    
    Sub isNotNr()
        Dim N As Variant
        Dim WS As Worksheet
        
        Set WS = ThisWorkbook.Worksheets("Sheet1")
        N = WS.Range("B18").Value
        
        If IsNumeric(N) Then
            If N <= 0 Then
                MsgBox "Check input"
                Exit Sub
            End If
        Else
            MsgBox "Check input"
        End If
    End Sub
    It gave me "type mismatch" error. Then i realized from your response, that the variable data Type must be "Variant"
    Muchos grazies

Posting Permissions

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