Excel formula to return the formula from a table using the table header as a reference

trishcollins

Board Regular
Joined
Jan 7, 2006
Messages
71
I have successfully used this formula to get the exact address of a header in a table, which I can then use in a Hyperlink formula, to go directly to that header. What I am trying to do, is get the formula in the first row of the column under the same header, and display it in text form. The reason for this, is that allows me to document the formulas with definitions on another worksheet. The thing to remember is that I am checking first to see if the initial enter text is referring to a Table or a Named Range, as I treat Named Ranges differently. The portion in bold/italics is what I am concerned about. What do I change to get the contents of the next row, which is a formula, to return in text format.

=IF([@[Table or Named Range]]="Table",CELL("address",(INDIRECT([@[Table or Range Names]]&"[[#Headers], [" & [@[Header or Range Name]]&"]]"))),CELL("address",INDIRECT([@[Table or Range Names]])))

The result of this formula is: '[DND-SCN-Client Application Onboarding to SCN Tool-WIPv1.43.xlsm]Business Needs Matrix'!$K$1

I want the result of the new formula to display the formula in the first row below ($K$2) as text.

Trish ;)
 
The version of GetDVFormula above is asking for a range. You are providing it the Formula inside the cell. I changed the UDF to read the string for the cell Address and allow you to specify the offset

Use it like:
=GetDVFormula([@[Table/Range Cell Reference]],1,0)
It will return the Data Validation Formula in the row directly below the range specified in [@[Table/Range Cell Reference]]


VBA Code:
Function GetDVFormula(Adr As String, Optional R As Long, Optional C As Long) As String
  Dim SheetName As String
  Dim Exclamation As Long
  Dim Sht As Worksheet
  Dim Rng As Range
  
  Exclamation = InStr(Adr, "!")
  If Exclamation > 0 Then
    SheetName = Left(Adr, Exclamation - 1)
    If InStr(SheetName, "'") > 0 Then
      SheetName = Replace(SheetName, "'", "")
    End If
    Set Sht = Sheets(SheetName)
    Set Rng = Sht.Range(Mid(Adr, Exclamation + 1, 100)).Offset(R, C)
  Else
    Set Sht = ActiveSheet
    Set Rng = Sht.Range(Adr).Offset(R, C)
  End If
  
  GetDVFormula = ""
  On Error Resume Next
  GetDVFormula = Rng.Validation.Formula1
  On Error GoTo 0
End Function
 
Upvote 0

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Can you please show me what results or values you have in the column [@[Table/Range Cell Reference]]
 
Upvote 0
This is how I am using it. I changed the UDF again to accommodate the filename when using =CELL("Address",[Reference])

Book2
ABCD
1OneTwoTable/Range Cell ReferenceDV Formula
212[Book2]Sheet2!$I$2=IF(TRUE=TRUE,1,2)
323[Book2]Sheet2!$J$2=IF(LEN("Jeff")=4,4,0)
Sheet1
Cell Formulas
RangeFormula
C2C2=CELL("address",Sheet2!I2)
D2:D3D2=GetDVFormula([@[Table/Range Cell Reference]],1,0)
C3C3=CELL("address",Sheet2!J2)



VBA Code:
Function GetDVFormula(Adr As String, Optional R As Long, Optional C As Long) As String
  Dim SheetName As String
  Dim Exclamation As Long
  Dim Sht As Worksheet
  Dim Rng As Range
  Dim Bracket As Long
  Dim aStr As String
  
  Bracket = InStr(Adr, "]")
  If Bracket > 0 Then
    aStr = Mid(Adr, Bracket + 1, 100)
  Else
    aStr = Adr
  End If
  Exclamation = InStr(aStr, "!")
  If Exclamation > 0 Then
    SheetName = Left(aStr, Exclamation - 1)
    If InStr(SheetName, "'") > 0 Then
      SheetName = Replace(SheetName, "'", "")
    End If
    Set Sht = Sheets(SheetName)
    Set Rng = Sht.Range(Mid(aStr, Exclamation + 1, 100)).Offset(R, C)
  Else
    Set Sht = ActiveSheet
    Set Rng = Sht.Range(aStr).Offset(R, C)
  End If
  
  GetDVFormula = ""
  On Error Resume Next
  GetDVFormula = Rng.Validation.Formula1
  On Error GoTo 0
End Function
 
Upvote 0
Solution
Can you please show me what results or values you have in the column [@[Table/Range Cell Reference]]
There is a formula in that cell which if it is a identified as a table in another column, looks up the header name from a different column to return the address: =IF([@[Table or Named Range]]="Table",CELL("address",(INDIRECT([@[Table or Range Names]]&"[[#Headers], [" & [@[Header or Range Name]]&"]]"))),CELL("address",INDIRECT([@[Table or Range Names]])))

The value in that cell is: '[DND-SCN-Client Application Onboarding to SCN Tool-WIPv1.43.xlsm]Business Needs Matrix'!$K$1

DND-SCN-Client Application Onboarding to SCN Tool
Table or Range NamesTable or Named RangeSheetSheet NameHeader or Range NameTable/Range Cell Reference
Business_Needs_MatrixTableC:\Users\collins.p\Documents\Work\DND\D4 Cloud Enablement\Secure Cloud Networking (SCN)\SCN Onboarding Tool\[DND-SCN-Client Application Onboarding to SCN Tool-WIPv1.43.xlsm]Business Needs MatrixBusiness Needs MatrixMapped to Preferred Network Connectivity'[DND-SCN-Client Application Onboarding to SCN Tool-WIPv1.43.xlsm]Business Needs Matrix'!$K$1

Trish ;)
 
Upvote 0
Are any of the references in the column "Table/Range Cell Reference" referring to other workbooks?

And if they are, are they closed or open. If they are closed, then the code is not going to work
 
Upvote 0
Are any of the references in the column "Table/Range Cell Reference" referring to other workbooks?

And if they are, are they closed or open. If they are closed, then the code is not going to work
That makes sense. At least the ones that are the data validation formulas are open. I will simply put the logic in place, and add "offset" to my table/range list, so that I can then use "offset" to determine if I need to run the code or not. Just happened the first one I tested, was on a closed spreadsheet, hidden from users :)
 
Upvote 0

Forum statistics

Threads
1,220,965
Messages
6,157,119
Members
451,398
Latest member
rjsteward

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