VBA Named Range Dynamic Index-CountIf Formula

Hamartian_

New Member
Joined
Jun 5, 2023
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hello,
I'm looking for some help with the last piece of this code, as everything else works until I attempt to change Data_Validation[Layer] into a variable equivalent to the DataBodyRange column of the ActiveCell

This is the only area that breaks the code for me, where I attempt to assign a variable with
":INDEX(Data_Validation[Layer],COUNTIF(Data_Validation[Layer],""?*""))"

to

":INDEX(" & ActiveCell.ListObject.DataBodyRange.Column & ",COUNTIF(" & ActiveCell.ListObject.DataBodyRange.Column & ",""?*""))"

Looking forward to any guidance! Learned a lot to get this far, and I know there's a lot to go

Rest of the code:

VBA Code:
Sub Create_DV_NamedRange()
'creates a named range based on header name that dynamically ignores blank cells

Dim TableName As String
Dim HeaderName As String
Dim HeaderOffset As Range
Dim startingCell As String
Dim DV_Name As String
Dim DataName As String

TableName = ActiveCell.ListObject.Name
HeaderName = Intersect(ActiveCell.ListObject.HeaderRowRange, ActiveCell.EntireColumn).Value
DV_Name = "DV_" & HeaderName
'DataName = "Data_Validation[" & HeaderName & "]"
startingCell = Intersect(HeaderOffset, ActiveCell.EntireColumn).Address

Set HeaderOffset = Intersect(ActiveCell.ListObject.HeaderRowRange, ActiveCell.EntireColumn).Offset(1)

'  Can not get following to work with references of strings, variants, or ranges :INDEX(Data_Validation[Layer],COUNTIF(Data_Validation[Layer],""?*""))"

'  Works - Index and CountIf is not dynamic - Recorded from Macro and modified
'  ActiveWorkbook.Names.Add Name:=DV_Name, RefersToR1C1:= _
'      "='Data Validation'!R3C" & ActiveCell.Column & ":INDEX(Data_Validation[Layer],COUNTIF(Data_Validation[Layer],""?*""))"
'  ActiveWorkbook.Names(DV_Name).Comment = ""

 'Does Not Work
ActiveWorkbook.Names.Add Name:=DV_Name, RefersToR1C1:= _
       "='Data Validation'!R3C" & ActiveCell.Column & ":INDEX(" & ActiveCell.ListObject.DataBodyRange.Column & ",COUNTIF(" & ActiveCell.ListObject.DataBodyRange.Column & ",""?*""))"
ActiveWorkbook.Names(DV_Name).Comment = ""


'  Does Not Work
'  ActiveWorkbook.Names.Add Name:=DV_Name, RefersToR1C1:= _
'      "='Data Validation'!R" & HeaderOffset.Row & "C" & ActiveCell.Column & ":INDEX(" & DataName & ",COUNTIF(" & DataName & ",""?*""))"
' ActiveWorkbook.Names(DV_Name).Comment = ""

'  Does Not Work
'  ActiveWorkbook.Names.Add Name:=DV_Name, RefersToR1C1:= _
'      "='Data Validation'!R3C" & ActiveCell.Column & ":INDEX(Range("K3:K14"),COUNTIF(Range("K3:K14"),""?*""))"
'  ActiveWorkbook.Names(DV_Name).Comment = ""


End Sub
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Fix was changing RefersToR1C1 to RefersTo

Code got cleaned up after that a bit, but that was the culprit that stumped mw
 
Upvote 0
Solution

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

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