Pass Cell Address into Formula

nalij_bond

New Member
Joined
Jan 27, 2023
Messages
8
Office Version
  1. 2013
Platform
  1. Windows
Thanks all in advance for your time and help.

My Issue: I am trying to define a variable and use that value to append to a formula within a cell

Current Code:
VBA Code:
Sub test3()
'In the PoC Doc Template, use the address function to find the cell reference column in row 10.
'for the term "VHI PMO/GTM". Then in the Sheet 'Key Stakeholders' in cell H6, use the cell
'reference found and append to the formula'

    Dim VHI_Ref
    
    VHI_Ref = "ADDRESS(12,MATCH(""VHI PMO/GTM"",'PoC Doc Template'!R10,0))"

    Sheets("Key Stakeholders").Range("H6").Formula = "='PoC Doc Template'!" & VHI_Ref

End Sub

Expected Result: What I would like to happen is in the 'Key Stakeholders' worksheet, in cell H6 - a formula like "='PoC Doc Template'!L12" is created.

Current Result: As of now, I keep receiving a Debug Error on the Sheets("Key Stakeholders").Range("H6").Formula = "='PoC Doc Template'!" & VHI_Ref line of code.

Any help would be greatly appreciated!
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
I think it's the single quotes and the missing double quote at the end. Try
Sheets("Key Stakeholders").Range("H6").Formula = "=PoC Doc Template!" & VHI_Ref & """"
 
Upvote 0
I think it's the single quotes and the missing double quote at the end. Try
Sheets("Key Stakeholders").Range("H6").Formula = "=PoC Doc Template!" & VHI_Ref & """"

Thank you for taking the time to reply.

Unfortunately, that generated a Run-Time Error '1004' on your suggested line.

I've tried a few variants like,
Sheets("Key Stakeholders").Range("H6").Formula = "='PoC Doc Template'!" & Sheets("Key Stakeholders").Address(12,MATCH(""VHI PMO/GTM"",'PoC Doc Template'!R10,0))

But that gives me a "Compile Error: Syntax Error"
 
Upvote 0
OK. Getting somewhere....

With the following code

VBA Code:
    Dim VHI_Ref
    
    VHI_Ref = "ADDRESS(12,MATCH(""VHI PMO/GTM"",'PoC Doc Template'!R10,0))"

    Sheets("Key Stakeholders").Range("H6").Formula = "=""PoC Doc Template!"" & VHI_Ref"

In Worksheet("Key Stakeholder"), cell H6... it does add in a formula but the formula listed is "="PoC Doc Template!" &@ VHI_Ref"

So, it's not passing the Address that it should have found....and appending to the text portion to make the desired output of "='PoC Doc Template'!L12"
 
Upvote 0
1004 is the most useless error number given that there must be at least 6 messages that go with it. The message matters more in that case.
You got a different error with that then you had before, so you made progress.
You get VHI_Ref in the formula because it's within the beginning and ending quote - & VHI_Ref" Excel is adding @ because it's a new thing for range formulas.
 
Upvote 0
1004 is the most useless error number given that there must be at least 6 messages that go with it. The message matters more in that case.
You got a different error with that then you had before, so you made progress.
You get VHI_Ref in the formula because it's within the beginning and ending quote - & VHI_Ref" Excel is adding @ because it's a new thing for range formulas.

My apologies for not being explicit on precisely what the error dialogue is.

When I run your suggested code modification,

VBA Code:
    Dim VHI_Ref
    
    VHI_Ref = "ADDRESS(12,MATCH(""VHI PMO/GTM"",'PoC Doc Template'!R10,0))"

    Sheets("Key Stakeholders").Range("H6").Formula = "=PoC Doc Template!" & VHI_Ref & """"

Here is the error received
1674878203971.png


The debug takes me to the last line Sheets("Key Stakeholders").Range("H6").Formula = "=PoC Doc Template!" & VHI_Ref & """"

Again, I appreciate your assistance in helping to resolve this. I've spent far too much time trying to get this to work.
 
Upvote 0
@Micron

I also attempted an alternative approach...

With the following code,
Code:
    Sheets("Key Stakeholders").Range("T6").FormulaR1C1 = _
        "=ADDRESS(12,MATCH(""VHI PMO/GTM"",'PoC Doc Template'!R10,0))"

I can successfully see the correct cell reference in Sheets("Key Stakeholders").Range("T6") of $L$12

The part I get stuck on is how to use the referenced cell value in T6 and append it to the "=PoC Doc Template!" & portion of the formula.

So the end result in Worksheet("Key Stakeholder"), cell H6 becomes "='PoC Doc Template'!L12"
 
Upvote 0
So, it's not passing the Address that it should have found.
I think that your problem is that an address has not been found. Can you explain in words exactly what you think the following line should be doing?
VBA Code:
VHI_Ref = "ADDRESS(12,MATCH(""VHI PMO/GTM"",'PoC Doc Template'!R10,0))"
 
Upvote 0
I think that your problem is that an address has not been found. Can you explain in words exactly what you think the following line should be doing?
VBA Code:
VHI_Ref = "ADDRESS(12,MATCH(""VHI PMO/GTM"",'PoC Doc Template'!R10,0))"

I need to search in row 10 of the worksheet 'PoC Doc Template' to find which column contains the value "VHI PMO/GTM". Once the column is identified, my row will always be 12.

So I am trying to pass that reference (correct answer of $L$12) into a cell formula on a different sheet ('Key Stakeholders').

Ultimately, creating a formula in the 'Key Stakeholders' worksheet, in cell H6 of "='PoC Doc Template'!$L$12". This formula reference would return the final value I need.

Thanks for your help!
 
Upvote 0
Thanks. Try making this change

Rich (BB code):
VHI_Ref = "ADDRESS(12,MATCH(""VHI PMO/GTM"",'PoC Doc Template'!R10,0))"
VHI_Ref = Evaluate("ADDRESS(12,MATCH(""VHI PMO/GTM"",'PoC Doc Template'!10:10,0))")
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,265
Members
452,627
Latest member
KitkatToby

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