Get value by referencing a cell

hsandeep

Well-known Member
Joined
Dec 6, 2008
Messages
1,213
Office Version
  1. 2010
Platform
  1. Windows
  2. Mobile
In E2 of Worksheet “Sheet2” I am using a formula =’7’!H120 which gives me value of cell H120 of the Worksheet with tab named ‘7” as a result in cell E2.

This formula in E2 is variable…it can be =’7’!B100; or =’7’!C555 or any formula like this.
I require an Excel formula in cell E3 of Worksheet “Sheet2” which should give me value of the cell which is $G$2 row below or above (IF G2=1, then 1 row below, IF $G$2=-1, then 1 row above) of the cell address which is being used in cell E2.
G2= an integer value

How to accomplish?
Thanks in advance.



Cross posted in Get value by referencing a cell
Referencing.xlsx
EFG
2221
3999
Sheet2
Cell Formulas
RangeFormula
E2E2='7'!H120
 
Unless OP saves his workbook using: Excel 97 - 2003 Workbook (*.xls)
Yes, but that is a macro-enabled workbook.
I wasn't sure why the OP is avoiding macros, so I just raised the issue that the old GET.CELLs really are a form of macro.

I did provide a link in the 1st thread itself.
You did indeed - I just missed it and I apologise. I will remove my earlier comment.
 
Upvote 0

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
I apologise.
It is shame on me if you apologise for my comeents. Sir I really have a great respect for you since you had helped me way back (I don't remember the exact date) on this forum with a vba which removes formula from a cell & fills it back.....
 
Upvote 0
I had forgotten about that one.
That's okay..what I really need is a solution for my "THIS" Post..even now I will bear a vba solution, preferably if this new vba is merged with the old one, else a separate Worksheet vba
 
Upvote 0
even now I will bear a vba solution
In that case try this user-defined function.

VBA Code:
Function myOffset(r As Range, RowOffset As Long) As Variant
  myOffset = Sheets(Replace(Split(Mid(r.Formula, 2), "!")(0), "'", "")).Range(Split(r.Formula, "!")(1)).Offset(RowOffset).Value
End Function

hsandeep.xlsm
H
117466
118115
119913
120121
121296
122812
123578
7


hsandeep.xlsm
EFG
1-2
2121
3115
Sheet2
Cell Formulas
RangeFormula
E2E2='7'!H120
E3E3=myOffset(E2,G1)
 
Upvote 0
Solution
In that case try this user-defined function.

VBA Code:
Function myOffset(r As Range, RowOffset As Long) As Variant
  myOffset = Sheets(Replace(Split(Mid(r.Formula, 2), "!")(0), "'", "")).Range(Split(r.Formula, "!")(1)).Offset(RowOffset).Value
End Function

hsandeep.xlsm
H
117466
118115
119913
120121
121296
122812
123578
7


hsandeep.xlsm
EFG
1-2
2121
3115
Sheet2
Cell Formulas
RangeFormula
E2E2='7'!H120
E3E3=myOffset(E2,G1)
Sir, it works perfectly well.
Btw, what is being returned by 2 & (1) in the formula?
Where is cell G1 in the code?
Will you explain in detail the actions of the code, please, so that I can manoeuvre it as per my requirements in future?
 
Upvote 0
Btw, what is being returned by 2 & (1) in the formula?
Do you mean this 2 and (1)?
myOffset = Sheets(Replace(Split(Mid(r.Formula, 2), "!")(0), "'", "")).Range(Split(r.Formula, "!")(1)).Offset(RowOffset).Value

2 says to start looking at the formula from the 2nd character. That is, start looking after the "=" sign

(1) Says that after splitting the formula at the "!" sign, look at the 2nd part - The first part is index (0) and the second part is index (1)

Where is cell G1 in the code?
G1 is the second argument in the formula =myOffset(E2,G1) so it becomes the second argument in the function Function myOffset(r As Range, RowOffset As Long) As Variant
So in the function it is RowOffset and is used here
myOffset = Sheets(Replace(Split(Mid(r.Formula, 2), "!")(0), "'", "")).Range(Split(r.Formula, "!")(1)).Offset(RowOffset).Value

Will you explain in detail the actions of the code
The worksheet formula was ='7'!H120
The function looks at that formula from character 2 (as explained above) and splits it into 2 pieces at the "!" as explained above and removes the single quotes from the first part. So the function obtains
7 and H120

The function then goes to the sheet given by the first part and then to the cell given by the second part then moves up or down (negative number moves up, positive number moves down) by RowOffset which is the value from G1 as explained above and returns the value from that cell
 
Upvote 0
Do you mean this 2 and (1)?
myOffset = Sheets(Replace(Split(Mid(r.Formula, 2), "!")(0), "'", "")).Range(Split(r.Formula, "!")(1)).Offset(RowOffset).Value

2 says to start looking at the formula from the 2nd character. That is, start looking after the "=" sign

(1) Says that after splitting the formula at the "!" sign, look at the 2nd part - The first part is index (0) and the second part is index (1)


G1 is the second argument in the formula =myOffset(E2,G1) so it becomes the second argument in the function Function myOffset(r As Range, RowOffset As Long) As Variant
So in the function it is RowOffset and is used here
myOffset = Sheets(Replace(Split(Mid(r.Formula, 2), "!")(0), "'", "")).Range(Split(r.Formula, "!")(1)).Offset(RowOffset).Value


The worksheet formula was ='7'!H120
The function looks at that formula from character 2 (as explained above) and splits it into 2 pieces at the "!" as explained above and removes the single quotes from the first part. So the function obtains
7 and H120

The function then goes to the sheet given by the first part and then to the cell given by the second part then moves up or down (negative number moves up, positive number moves down) by RowOffset which is the value from G1 as explained above and returns the value from that cell
Absolutely clear. Many thanks Sir
 
Upvote 0

Forum statistics

Threads
1,214,958
Messages
6,122,475
Members
449,087
Latest member
RExcelSearch

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