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!
 
Maybe it is better if I build in a "Search and replace" macro...
 
Upvote 0

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Have you considered using simple find/replace code to change the sheet names in the template?

That would be straightforward and it would mean you wouldn't need to rely upon INDIRECT.
 
Upvote 0
Have you considered using simple find/replace code to change the sheet names in the template?

That would be straightforward and it would mean you wouldn't need to rely upon INDIRECT.

Thats the key I thing.

Thank you Norie for your help! I can record that macro myself I think! :giggle: (y) (y) (y)
 
Upvote 0
VBA Code:
Sub Makro3()
    Range("D9").Select
    ActiveCell.Replace What:="'2'", Replacement:=Worksheets(4).Name, LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
    Selection.Find(What:=Worksheets(4).Name, After:=ActiveCell, LookIn:=xlFormulas2, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate
End Sub


YES! Thanks again Norie
 
Upvote 0
How many formulas are you dealing with?
 
Upvote 0
How many formulas are you dealing with?


VBA Code:
    Cells.Select
    ActiveCell.Replace What:="'1'", Replacement:=Worksheets(2).Name, LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
    Selection.Find(What:=Worksheets(2).Name, After:=ActiveCell, LookIn:=xlFormulas2, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate
        
    ActiveCell.Replace What:="'2'", Replacement:=Worksheets(3).Name, LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
    Selection.Find(What:=Worksheets(3).Name, After:=ActiveCell, LookIn:=xlFormulas2, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate
        
    ActiveCell.Replace What:="'3'", Replacement:=Worksheets(4).Name, LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
    Selection.Find(What:=Worksheets(4).Name, After:=ActiveCell, LookIn:=xlFormulas2, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate
        
    ActiveCell.Replace What:="'4'", Replacement:=Worksheets(5).Name, LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
    Selection.Find(What:=Worksheets(5).Name, After:=ActiveCell, LookIn:=xlFormulas2, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate
        
    ActiveCell.Replace What:="'5'", Replacement:=Worksheets(6).Name, LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
    Selection.Find(What:=Worksheets(6).Name, After:=ActiveCell, LookIn:=xlFormulas2, _
        LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate
    Range("A1").Select

About 5
 
Upvote 0
The reason I'm asking is because rather than using find/replace across the sheet you could simply loop through all the cells with formulas and do a find/replace at that level.

Obviously how efficient that would be depends on the number of formulas you have.
 
Upvote 0

Forum statistics

Threads
1,215,643
Messages
6,125,990
Members
449,277
Latest member
Fanamos298

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