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?
 

Some videos you may like

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.

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
 

Watch MrExcel Video

Forum statistics

Threads
1,123,481
Messages
5,601,918
Members
414,482
Latest member
morkar

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