• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Not gettting how to use array in macro

I am trying to get a hand on using array's and not quite getting it here.

I think my problem is here: HasNumber(dat(i, 1).Text), in that "dat(i, 1)" is a Varient so that "dat(i, 1).Text" does not make sense, but I am not sure how to handle the mismatch

Thanks

Code:
Sub DoesCellHaveNumer()
Dim dat As Variant
Dim ws As Worksheet
Dim rng As Range
Dim LR As Long
Dim i As Long

Set ws = ThisWorkbook.Sheets("UnPivot")
LR = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
Set rng = ws.Range("D2:D" & LR)
dat = rng.Value

For i = LBound(dat, 1) To UBound(dat, 1)
If HasNumber(dat(i, 1).Text) Then
  MsgBox "Yes has number"
 
Else
  MsgBox "No does not"
 
End If
Next i

End Sub

Code:
Function HasNumber(strData As String) As Boolean
Dim iCnt As Integer

For iCnt = 1 To Len(strData)
    If IsNumeric(Mid(strData, iCnt, 1)) Then
        HasNumber = True
        Exit Function
    End If
Next iCnt

End Function
 
Pls check with it,

Code:
Sub DoesCellHaveNumer()
Dim ws As Worksheet
Dim rng As Range, r As Range
Dim LR As Long

Set ws = ThisWorkbook.Sheets("UnPivot")
LR = ws.Range("A" & ws.Rows.Count).End(xlUp).Row
Set rng = ws.Range("D2:D" & LR)

For Each r In rng
    If HasNumber(r.Value) = True Then
        MsgBox "Yes has number"
    Else
        MsgBox "No does not"
    End If
Next
End Sub
 
Back
Top