Consulting

Results 1 to 3 of 3

Thread: Detect listbox item is numeric problem

  1. #1
    VBAX Regular
    Joined
    May 2016
    Posts
    17
    Location

    Detect listbox item is numeric problem

    I have a listbox in a userform where all the column header from A:AZ listed, I used a name range to capture it. There is no fix data or column header and it could be change from time to time(depend on the user), what I need to to determine is if the vba code detects that the line item is numeric it could format amount into "0,00.00", but date has to be formatted "mm/dd/yyyy". I have also a textbox where the items on listbox will be listed through double clicking.

    Is there a way or function inside vba to detect numeric value? Thanks

  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Dim myVar As Variant
    Dim Cel as Range
    
    For each Cel in MyNamedRange
    If IsNumeric(Cel) then 
    myVar = Format(Cel, "#,##0.00")
    
    ElseIf IsDate(Cel) then 
    myVar = Format(myVar, "dd mmm, yyyy")
    
    Else: myVar = Cel
    End If
    
    ListBox1.AddItem myVar
    Next Cel
    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

  3. #3
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    Rather than test for data type, you could use the .Text property of the cell and use the formatting determined in the worksheet.

    Dim oneCell as Range
    
    For Each oneCell in Range("A1:AZ1")
        ListBox1.AddItem oneCell.Text
    Next oneCell

Posting Permissions

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