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

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
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
A
B
C
1
TextValueFormula in B2 copy down
2
ABC_123123=SplitText(A2,TRUE)
3
XYZ_789789
Sheet: Sheet3

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,215,943
Messages
6,127,820
Members
449,409
Latest member
katiecolorado

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