Referencing a function within a sub

LactoseO.D.'d

Board Regular
Joined
Feb 22, 2010
Messages
52
Right now I have a function I am using to count the number of "." 's within a cell.

I found it online:
Code:
Function countchar(Rng As Range, TextToFind As String) As Double
     'Function purpose:  To count number of instances of a string of
     'characters in a range
     
    Dim cl As Range, x As Integer
     
    For Each cl In Rng
        For x = 1 To Len(cl)
            If Mid(cl, x, Len(TextToFind)) = TextToFind Then
                countchar = countchar + 1
            End If
        Next x
    Next cl
     
End Function

I then use that number of "." 's to perform operations on other cells.

Currently I am populating a dummy column to get the function values, and performing my operations based off those values, then deleting them. This seems redundant and slow. Is there any way I can reference the output of this function within my Sub and skip the whole dummy column?
 

Some videos you may like

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type

pgc01

MrExcel MVP
Joined
Apr 25, 2006
Messages
19,877
Hi

If r is a range object referring to a cell, the number of "."'s in that cell are:

Code:
MsgBox Len(r.Value) - Len(Replace(r.Value, ".", ""))
 

GTO

MrExcel MVP
Joined
Dec 9, 2008
Messages
6,154
...
Currently I am populating a dummy column to get the function values, and performing my operations based off those values, then deleting them. This seems redundant and slow. Is there any way I can reference the output of this function within my Sub and skip the whole dummy column?

What are you populating the dummy column with?
 

LactoseO.D.'d

Board Regular
Joined
Feb 22, 2010
Messages
52
Hi

If r is a range object referring to a cell, the number of "."'s in that cell are:

Code:
MsgBox Len(r.Value) - Len(Replace(r.Value, ".", ""))

I tried
Code:
Sub yo()
Dim r As Range
r = ActiveCell
MsgBox Len(r.Value) - Len(Replace(r.Value, ".", ""))
End Sub

Run Time Error 91: Object Variable or With block variable not set

What are you populating the dummy column with?

The dummy column is the values of countchar for the cells to the left of the dummy column. I then use these values to perform an operation on the cells to the right of the dummy column. Then I delete the dummy column. It seems like I'm using an extra step. If there was a way to reference the countchar UDF and have its value, I could skip populating the column.
 

LactoseO.D.'d

Board Regular
Joined
Feb 22, 2010
Messages
52

ADVERTISEMENT

So now I feel a little sheepish.

The UDF can be referred to as any other function would be.

Rewriting pcG01's original code:
Code:
MsgBox countchar(ActiveCell, ".")
 

mikerickson

MrExcel MVP
Joined
Jan 15, 2007
Messages
23,899
When assigning object variables, the Set keyword must be used
Code:
Sub yo()
    Dim r As Range
    Set r = ActiveCell
    MsgBox Len(r.Value) - Len(Replace(r.Value, ".", ""))
End Sub
 

LactoseO.D.'d

Board Regular
Joined
Feb 22, 2010
Messages
52
When assigning object variables, the Set keyword must be used
Code:
Sub yo()
    Dim r As Range
    Set r = ActiveCell
    MsgBox Len(r.Value) - Len(Replace(r.Value, ".", ""))
End Sub

Thank you. Little things to help my syntax along really help.
 

Watch MrExcel Video

Forum statistics

Threads
1,122,193
Messages
5,594,774
Members
413,934
Latest member
austinb

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