VBA edit to return Null

Nemo74

New Member
Joined
Mar 22, 2013
Messages
38
Hello everyone,

I learned this code set up from the interwebs, but I'm not slick enough to modify it to return the word "Null" when there are no numbers in the target... if someone could lead me to water, I'm more than happy to learn how to drink.

Thanks,

Code:
Function PullNums(target As Range)Dim MyChar As String, i As Integer, MyChar2 As String


    MyChar = ""
    If Len(target.Value) = 0 Then GoTo GoExit
    For i = 1 To Len(target.Value)
        If IsNumeric(Mid(target, i, 1)) Then MyChar = MyChar & Mid(target, i, 1)
    Next i
GoExit:
    PullNums = MyChar
End Function
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Perhaps something along the lines of:

Code:
If Len(MyChar) = 0 Then MyChar = "Null"

And put that between "Next I" and "GoExit".
 
Upvote 0
Here is the code you posted modified to do what you asked for...
Code:
[table="width: 500"]
[tr]
	[td]Function PullNums(target As Range)
    Dim MyChar As String, i As Integer, MyChar2 As String
    MyChar = ""
    If Len(target.Value) = 0 Then GoTo GoExit
    For i = 1 To Len(target.Value)
        If IsNumeric(Mid(target, i, 1)) Then MyChar = MyChar & Mid(target, i, 1)
    Next i
GoExit:
    If Len(MyChar) Then
      PullNums = MyChar
    Else
      PullNums = "Null"
    End If
End Function[/td]
[/tr]
[/table]
However, I would have coded this function a little differently (should be more efficient)...
Code:
Function PullNums(S As String)
  Dim X As Long
  For X = 1 To Len(S)
    If Mid(S, X, 1) Like "[!0-9]" Then Mid(S, X) = " "
  Next
  If S Like "*[! ]*" Then
    PullNums = Replace(S, " ", "")
  Else
    PullNums = "Null"
  End If
End Function
 
Upvote 0
Thank you much to the both of you, they all work perfectly! I love learning stuff and you have both shown me some cool things. That 2nd script Rick is very nice and short and I'll be looking to use that and implement that in my future code.

Thanks you guys!
 
Upvote 0

Forum statistics

Threads
1,214,950
Messages
6,122,436
Members
449,083
Latest member
Ava19

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top