Dynamic sheet references

Razzy

Board Regular
Joined
Jul 24, 2020
Messages
106
Office Version
  1. 365
Platform
  1. Windows
Hi!

I got this reference to another sheet:
VBA Code:
=kanin_faste!$N$15

I've written an UDF:
VBA Code:
Function ARK_B() As String
    Application.Volatile
    ARK_B = Worksheets(2).Name & "!"
End Function

Witchs returns: kanin_faste!

How do I use the UDF inside the formula like:
VBA Code:
=ARK_B()$N$15

I saw that ARK_B() returns "kanin_faste!" when I press F9.

hm....


The reason I need this is because I need the sheet references dynamic.

Thank you in advance!
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
I suppose you could use INDIRECT.

=INDIRECT(ARK_B()&"$N$15")
 
Upvote 0
I suppose you could use INDIRECT.

=INDIRECT(ARK_B()&"$N$15")


Works m8!

Maybe it would be better to have an UDF like: "ARK_B(N15)"

When I input "N15", I get N15 in ARK_B.

Can you help me with this UDF? I will try meanwhile offcurse.

Edit: Maybe skip INDIRECT function all together!

indirect-excels-most-evil-function/
 
Last edited:
Upvote 0
VBA Code:
Function ARK_C(X As Variant) As String
    Application.Volatile
    ARK_C = Worksheets(3).Name & "!" & X
  
End Function

hm...
 
Upvote 0
If you really wanted I suppose you could have something like this.
VBA Code:
Function ARK_B(strAddress As String, Optional boolRowAbsolute As Boolean = False, _
                                     Optional boolColAbsolute As Boolean = False, _
                                     Optional lngRefStyle As XlReferenceStyle = xlA1) As String
    Application.Volatile
    
    ARK_B = "'" & Worksheets(2).Name & "'!" & Range(strAddress).Address(boolRowAbsolute, boolColAbsolute, lngRefStyle)
    
End Function

P.S. Why do you need this? How is the sheet reference dynamic?
 
Upvote 0
I have a template workbook that with a custom built report.

When I run a VBA-code I get new Sheets in it, and it would be nice that that the Sheet references where dynamic.

I can live with the indirect formula, can we solve it with that?

I am sorry but I did not get your code to work. :unsure: But thx very much!

Cell Formulas
RangeFormula
D8,F8D8=(kanin_sammenstilling!$J$11+kanin_sammenstilling!$J$25)/kanin_sammenstilling!$G$27
E8E8=(hest_sammenstilling!$J$11+hest_sammenstilling!$J$25)/hest_sammenstilling!$G$27
D9,F9D9=(kanin_sammenstilling!$J$12+kanin_sammenstilling!$J$26)/kanin_sammenstilling!$G$27
E9E9=(hest_sammenstilling!$J$12+hest_sammenstilling!$J$26)/hest_sammenstilling!$G$27
D10,F10D10=(kanin_sammenstilling!$J$13+kanin_sammenstilling!$J$27)/kanin_sammenstilling!$G$27
E10E10=(hest_sammenstilling!$J$13+hest_sammenstilling!$J$27)/hest_sammenstilling!$G$27
D11,F11D11=(kanin_sammenstilling!$J$14+kanin_sammenstilling!$J$28+kanin_sammenstilling!$J$29)/kanin_sammenstilling!$G$27
E11E11=(hest_sammenstilling!$J$14+hest_sammenstilling!$J$28+hest_sammenstilling!$J$29)/hest_sammenstilling!$G$27
D12,F12D12=(kanin_sammenstilling!$J$15+kanin_sammenstilling!$J$31)/kanin_sammenstilling!$G$27
E12E12=(hest_sammenstilling!$J$15+hest_sammenstilling!$J$31)/hest_sammenstilling!$G$27
D13,F13D13=(kanin_sammenstilling!$J$16+kanin_sammenstilling!$J$30)/kanin_sammenstilling!$G$27
E13E13=(hest_sammenstilling!$J$16+hest_sammenstilling!$J$30)/hest_sammenstilling!$G$27
D14,F14D14=(kanin_sammenstilling!$J$21)/kanin_sammenstilling!$G$27
E14E14=(hest_sammenstilling!$J$21)/hest_sammenstilling!$G$27
D15,F15D15=(kanin_sammenstilling!$J$33)/kanin_sammenstilling!$G$27
E15E15=(hest_sammenstilling!$J$33)/hest_sammenstilling!$G$27
D16,F16D16=(kanin_sammenstilling!$J$34)/kanin_sammenstilling!$G$27
E16E16=(hest_sammenstilling!$J$34)/hest_sammenstilling!$G$27
D17,F17D17=(kanin_sammenstilling!$J$32)/kanin_sammenstilling!$G$27
E17E17=(hest_sammenstilling!$J$32)/hest_sammenstilling!$G$27
 
Upvote 0
How did you try the code I posted and how did it not work?

How are you populating the formulas in the sheets you are creating?
 
Upvote 0
VBA Code:
Function ARK_C(X As String) As String
    Application.Volatile
    ARK_C = "=" & Worksheets(3).Name & "!" & X
End Function

If I input "N15" I get the =kanin_sammenstilling!N15, but I need to recalculate the cell to get it to work.

I will try your code again now.

The formulas are preset in the template

Edit:

Maybe I need to add an "run" something:

VBA Code:
Public Function MyUDF(Value As Variant) As Variant
    MyUDF = Application.Run("ABC", Value, "Value_Name")
End Function
 
Last edited:
Upvote 0
VBA Code:
Function Norie(strAddress As String, Optional boolRowAbsolute As Boolean = False, _
                                     Optional boolColAbsolute As Boolean = False, _
                                     Optional lngRefStyle As XlReferenceStyle = xlA1) As String
    Application.Volatile
   
    Norie = "'" & Worksheets(3).Name & "'!" & Range(strAddress).Address(boolRowAbsolute, boolColAbsolute, lngRefStyle)
   
End Function

Eksempel.xlsm
J
28#VERDI!
29#VERDI!
Oversikt
Cell Formulas
RangeFormula
J28J28=Norie()
J29J29=Norie(N15)
 
Upvote 0

Forum statistics

Threads
1,216,124
Messages
6,128,987
Members
449,480
Latest member
yesitisasport

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