Macro Button

davor

New Member
Joined
Jul 31, 2019
Messages
2
Hi

I am new at macros and VBA editing.
I wanted to split txt form numbers in cells so I putted this code in module:

Public Function SplitText(pWorkRng As Range, pIsNumber As Boolean) As String
'Updateby20150306
Dim xLen As Long
Dim xStr As String


xLen = VBA.Len(pWorkRng.Value)


For i = 1 To xLen
xStr = VBA.Mid(pWorkRng.Value, i, 1)
If ((VBA.IsNumeric(xStr) And pIsNumber) Or (Not (VBA.IsNumeric(xStr)) And Not (pIsNumber))) Then
SplitText = SplitText + xStr
End If
Next
End Function

The code worked but I wanted to have a button that would be on my ribbon. When I want to add it I can't see this macro in the macro list. All others are there.

What am I doing wrong???
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Thats because its a function not a sub. It would have to be rewritten for use with a button.
 
Upvote 0
Functions do not appear in list of macros
Use like any other formula like this

Excel 2016 (Windows) 32 bit
[Table="width:, class:head"][tr=bgcolor:#E0E0F0][th] [/th][th]
A
[/th][th]
B
[/th][th]
C
[/th][/tr]
[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
1
[/td][td]Text[/td][td]Value[/td][td]Formula in B2 copy down[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
2
[/td][td]ABC_123[/td][td]123[/td][td]=SplitText(A2,TRUE)[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
3
[/td][td]XYZ_789[/td][td]789[/td][td][/td][/tr]
[/table]
[Table="width:, class:grid"][tr][td]Sheet: Sheet3[/td][/tr][/table]

Or call from a procedure like this
Code:
Sub davor()
    Dim Cell As Range, aStr As String, x As Double
    For Each Cell In Range("A2:A3")
        [COLOR=#ff0000]x = SplitText(Cell, True)[/COLOR]
        aStr = aStr & Cell & " : " & x & vbCr
    Next Cell
    MsgBox aStr, , "Numbers Only"
End Sub

With values above, message box returns:
Numbers Only
ABC_123 : 123
XYZ_789 : 789
 
Upvote 0
or call from a button like this to get numbers in active cell
Code:
Private Sub CommandButton1_Click()
    MsgBox SplitText(ActiveCell, True)
End Sub

If none of the above gives you what you want, explain exactly how you want the button to work
(- what is the range ? where are returned values placed ? )
 
Upvote 0
or call from a button like this to get numbers in active cell
Code:
Private Sub CommandButton1_Click()
    MsgBox SplitText(ActiveCell, True)
End Sub

If none of the above gives you what you want, explain exactly how you want the button to work
(- what is the range ? where are returned values placed ? )


You solved it. Thank you very much :)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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