How to call function from sub procedure

sal11235

New Member
Joined
Mar 15, 2011
Messages
13
Hello boys,

I created a VBA function to count instances of a string across sheets. Here it is

Code:
Private Function FindString(c01)
For Each sht In ActiveWorkbook.Sheets
If Not sht.Name = "LIST" Then
x = WorksheetFunction.CountIf(sht.UsedRange, c01)
xcount = x + xcount
End If
Next
FindString = xcount
End Function

This is all fine, but I now need this to be turned into a subroutine where I can execute the procedure and it will fill all the cells I want with the result of the FindString function above. I just can't figure out how to bring the function into a procedure. This is how far I have got...

(I have a list of values in a column which I select, then all the values adjacent to the column are filled with the result of the FindString procedure)

Code:
Public Sub FindString()

FirstRow = Range(Left(ActiveCell.Address(RowAbsolute, ColumnAbsolute), 1) & "1").Offset(1, 0)
LastRow = Range(Left(ActiveCell.Address(RowAbsolute, ColumnAbsolute), 1) & Rows.Count).End(xlUp).Row

For i = FirstRow To LastRow

Cells(i, ActiveCell.Column).Offset(0, 1) = FindString

Next

End Sub

which is obviously wrong as I'm just writing out code the way I want to it to perform in my head!

Any ideas???.... will be very appreciated with big cyber hugs!

Thank yoouuuu
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Welcome to MrExcel.

Your function has an argument so you need to pass it when calling it. Try:

Rich (BB code):
Cells(i, ActiveCell.Column).Offset(0, 1).Value = FindString(Cells(i, ActiveCell.Column).Value)
 
Upvote 0
Hi there,

I don't believe you'll want to call the function for each cell, as this slows processing. Maybe:
Rich (BB code):
Public Sub FindStrings()
Dim FirstRow As Long, LastRow As Long, i As Long, lRet As Long
    
    lRet = FindString
    
    FirstRow = ActiveCell.Offset(1).Row
    LastRow = Cells(Rows.Count, ActiveCell.Column).End(xlUp).Row
    
    For i = FirstRow To LastRow
        Cells(i, ActiveCell.Column).Value = lRet
    Next
End Sub

Hope that helps,

Mark
 
Upvote 0
Ha! Thank you for the solution Andrew that has made my day :=)))))))

And thank you GTO for simplifying my code, really helpful for my learning

Two massive hugs from London :=)
 
Upvote 0
One last thing.. (of course, its always one last thing isnt it?)

seriously, the last thing ;=p..since I have this wealth of MVP power at my disposal and I could do with the training!

just from what you said GTO, about having an argument, I went over it and took the argument out but this doesn't seem to work.

Code:
Public Sub FindStrings()
Dim FirstRow As Long, LastRow As Long, i As Long
        
    FirstRow = 2
    LastRow = Cells(Rows.Count, ActiveCell.Column).End(xlUp).Row
        For i = FirstRow To LastRow
        Cells(i, ActiveCell.Column).Offset(0, 1) = FindString
        
    Next
End Sub

Code:
Private Function FindString()
Dim c01 As Range
For Each sht In ActiveWorkbook.Sheets
If Not sht.Name = "LIST" Then
x = WorksheetFunction.CountIf(sht.UsedRange, c01)
xcount = x + xcount
End If
Next
FindString = xcount
End Function

I thought it might work as I'm just declaring the c01 as a range rather than an argument..But the code stops at the countif function. Is it telling me that I ALWAYS have to have c01 declared as an argument in the function?

If either of you could clear this up that would be cool...
 
Upvote 0
You haven't assigned anything to c01. It's always better to pass everything a function needs as an argument, so stick with what you've got.
 
Upvote 0
really appreciate that, the textbooks that I've read on excel VBA don't have this kind of "best practice" advice so thank you, will take on board for the future :=)
 
Upvote 0
It's also best practice to give your variables/arguments/functions/subs meaningful names rather than 'c01' which tells you nothing at all. ;)
 
Upvote 0

Forum statistics

Threads
1,224,582
Messages
6,179,670
Members
452,936
Latest member
anamikabhargaw

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