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 to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
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,214,943
Messages
6,122,369
Members
449,080
Latest member
Armadillos

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