VBA Cutsom function breaks on updating another workbook

PanzerRanger

New Member
Joined
Jan 3, 2018
Messages
20
Hi!

I have write a custom function sista raden means last row:

Public Function SISTARADEN()
Dim a, h As Integer
a = Worksheets("Stl").Cells(Rows.Count, "A").End(xlUp).Row - 1
h = Worksheets("Stl").Cells(Rows.Count, "H").End(xlUp).Row - 1
If a > h Then
SISTARADEN = a
Else: SISTARADEN = h
End If

End Function

It is then used in a formula =IF(SISTARADEN()/30<1;1;ROUNDUP(SISTARADEN()/30;0))
It is use to automatically create pages in another workbook. The problem is, that if I make a full calculations in another workbook this custom formulas, and a few others, similar to this breaks by returning #VALUE !

What have I done wrong? :eeek:
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
Hi, welcome to the forum! You probably need to qualify the workbook. Assuming the UDF is in the workbook that is calling it.

Code:
Public Function SISTARADEN()
 Dim a As Long, h As Long
 a = ThisWorkbook.Worksheets("Stl").Cells(Rows.Count, "A").End(xlUp).Row - 1
 h = ThisWorkbook.Worksheets("Stl").Cells(Rows.Count, "H").End(xlUp).Row - 1
 If a > h Then
 SISTARADEN = a
 Else: SISTARADEN = h
 End If
End Function

But, in my opinion, you should really be passing the ranges to the function, so..

Code:
Public Function SISTARADEN(Col1 As Range, Col2 As Range)
 Dim a As Long, h As Long
 a = Col1.Cells(Rows.Count, "A").End(xlUp).Row - 1
 h = Col2.Cells(Rows.Count, "H").End(xlUp).Row - 1
 If a > h Then
 SISTARADEN = a
 Else: SISTARADEN = h
 End If
End Function

Which you would call like:
=IF(SISTARADEN(stl!A:A;stl!H:H)/30<1;1;ROUNDUP(SISTARADEN(stl!A:A;stl!H:H)/30;0))

Note the changes to the variable declarations.
 
Last edited:
Upvote 0
But, in my opinion, you should really be passing the ranges to the function, so..

That second piece of code was a last minute addition to my post and should you opt for that option this would be better:

Code:
Public Function SISTARADEN(Col1 As Range, Col2 As Range)
 Dim a As Long, h As Long
 a = Col1.Cells(Col1.Rows.Count).End(xlUp).Row - 1
 h = Col2.Cells(Col2.Rows.Count).End(xlUp).Row - 1
 If a > h Then
 SISTARADEN = a
 Else: SISTARADEN = h
 End If
End Function
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,891
Messages
6,175,229
Members
452,621
Latest member
Laura_PinksBTHFT

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