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?
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
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, ".", ""))
 
Upvote 0
...
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?
 
Upvote 0
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.
 
Upvote 0
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, ".")
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,213,484
Messages
6,113,924
Members
448,533
Latest member
thietbibeboiwasaco

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