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
 

Some videos you may like

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.

dreid1011

Well-known Member
Joined
Jun 4, 2015
Messages
2,129
Perhaps something along the lines of:

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

And put that between "Next I" and "GoExit".
 

Rick Rothstein

MrExcel MVP
Joined
Apr 18, 2011
Messages
36,679
Office Version
  1. 2010
Platform
  1. Windows
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
 

Nemo74

New Member
Joined
Mar 22, 2013
Messages
38
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!
 

Watch MrExcel Video

Forum statistics

Threads
1,122,469
Messages
5,596,313
Members
414,052
Latest member
Dual Showman

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
Top