Consulting

Results 1 to 5 of 5

Thread: Solved: Checking to see if a cell contains a Number or Letter

  1. #1

    Solved: Checking to see if a cell contains a Number or Letter

    Alrighty-then

    Let's say that I want to write, in VBA, a code that will check Cell(i,j)
    and if it contains a letter (any letter) it will MsgBox "Letter" and if it contains a Number (any number) it will do nothing.

    How do I specify a letter in general? Is there a Chr() code or something?

    Thanks!

  2. #2
    VBAX Mentor MaximS's Avatar
    Joined
    Sep 2008
    Location
    Stoke-On-Trent
    Posts
    360
    Location
    try that:

    [vba]
    Sub Chr_Check()

    Dim x As String

    x = Asc(Cells(1, 1).Value)
    Select Case Asc(x)
    Case 65 To 90
    Err = MsgBox(Cells(1, 1).Address & " is a Letter", vbOKOnly, "Letter")
    Case 67 To 122
    Err = MsgBox(Cells(1, 1).Address & " is a Letter", vbOKOnly, "Letter")
    End Select
    End Sub

    [/vba]

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Couldn't get that to work. My adaption of Maxim's code.
    Try
    [VBA]
    Option Explicit
    Sub Chr_Check()

    Dim x As Long

    x = Asc(Cells(1, 1).Value)
    Select Case x
    Case 65 To 90
    Err = MsgBox(Cells(1, 1).Address & " is a Letter", vbOKOnly, "Letter")
    Case 67 To 122
    Err = MsgBox(Cells(1, 1).Address & " is a Letter", vbOKOnly, "Letter")
    Case Else
    If IsNumeric(Cells(1, 1)) Then
    MsgBox "Number"
    Else
    MsgBox "Symbol"
    End If
    End Select
    End Sub

    [/VBA]
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  4. #4
    VBAX Mentor MaximS's Avatar
    Joined
    Sep 2008
    Location
    Stoke-On-Trent
    Posts
    360
    Location
    ehh my mistake double ASC()

  5. #5
    Great!! Thanks guys!

Posting Permissions

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