Custom VBA Function

kland1

New Member
Joined
Mar 18, 2009
Messages
12
I created a custom function in vba but it does not update automatically like sum or other functions. The function gets numbers from another worksheet in the same workbook but when the numbers on the other worksheet change the cell with the function does not update automatically. Is there any special code that I need to have it update when the number changes on the other worksheet?
 

Excel Facts

Will the fill handle fill 1, 2, 3?
Yes! Type 1 in a cell. Hold down Ctrl while you drag the fill handle.

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
Welcome to the Board.

You could try:

Application.Volatile

at the beginning of your function.
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Hello and welcome to MrExcel.

Try adding this near the top of your function

Code:
Application.Volatile
 

kland1

New Member
Joined
Mar 18, 2009
Messages
12

ADVERTISEMENT

Hey guys,

That line of code works good but its doing something different now. The cells update automatically now but since i just the same function in multiple workbooks, it seems that only one workbook will show the values and the other ones will say #VALUE!. I can only see the values of the other workbook when i click on a cell and hit enter. This is when the other workbook will change to #VALUE!. Is there a way I can fix this?
 

kland1

New Member
Joined
Mar 18, 2009
Messages
12

ADVERTISEMENT

'*********************************************************************************************
' Retrieves the number of copper ports
' checkRange - the range to check for "Copper"
' numberCol - the number of columns right of checkRange to retrieve the data
Function CountCopper(checkRange As range, numberCol) As Integer
Application.Volatile
total = 0
Col = checkRange.Column + numberCol

For Each cell In checkRange
If (LCase(cell.Value) = "copper") Then
Row = cell.Row
total = total + Sheets(checkRange.Worksheet.Name).Cells(Row, Col).Value
End If
Next cell

CountCopper = total
End Function
 

Andrew Poulsom

MrExcel MVP
Joined
Jul 21, 2002
Messages
73,092
Instead of:

total = total + Sheets(checkRange.Worksheet.Name).Cells(Row, Col).Value

try:

total = total + checkRange.Parent.Cells(Row, Col).Value
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,164,282
Messages
5,836,401
Members
430,425
Latest member
xlsee

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