Dynamic Ranges (without range address unavailable) inside a Formula

predzer

New Member
Joined
Jun 21, 2018
Messages
20
Hey Guys!

I immensely need your help for the below scenario.

I am trying to find a way to address range in a formula. The problem is that, the range is created dynamically. Please refer below image further.


Here I have 2 tables say Table 1 & Table 2, these tables are snippets of my actual table which has 50 columns & 40 Rows.

Table 1 is generated from a pivot table, and it is dynamic so I wont be having idea of its address (except cell A1 where table 1 starts always)

so Have used the below codes to set range of table 2

Code:
[COLOR=#333333]Set table2top = Range("A1").End(xlToRight).Offset(0, 1) 
Set table2bot = Range("A1").End(xlToRight).End(xlDown).Offset(0, 1)
[/COLOR]
Now i have to enter formula =AVERAGEIF($A$1:$I$1,K$1,$A2:$I2) in cell K2 and use autofill for rest of table 2.


I need your help to Replace cell range ($A$1:$I$1,K$1,$A2:$I2) to 'something' as i wont be knowing cell range except starting cell A1.


P.S. I tried naming date row. so i can replace $A$1:$I$1 with a name, but could not figure out for the next part of formula.

I just started using VBA to build a model & Im almost halfway through my model but stuck at this point
please help me out


(P.S. Also posted on
http://www.vbaexpress.com/forum/showthread.php?63174-Using-Dynamic-Ranges-(without-range-address)-inside-a-Formula ​)
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
32,317
Office Version
365
Platform
Windows
How about
Code:
Sub AddFormula()
Dim Rng As Range
With Range("A1").CurrentRegion
Set Rng = Range(Cells(2, .Columns.Count), Cells(.Rows.Count, .Columns.Count)).Offset(, 2)
Rng.Formula = "=averageif(" & .Rows(1).Address & "," & Rng.Offset(-1).Resize(1, 1).Address(1, 0) & "," & .Rows(2).Address(0, 1) & ")"
End With
End Sub
 

predzer

New Member
Joined
Jun 21, 2018
Messages
20
How about
Code:
Sub AddFormula()
Dim Rng As Range
With Range("A1").CurrentRegion
Set Rng = Range(Cells(2, .Columns.Count), Cells(.Rows.Count, .Columns.Count)).Offset(, 2)
Rng.Formula = "=averageif(" & .Rows(1).Address & "," & Rng.Offset(-1).Resize(1, 1).Address(1, 0) & "," & .Rows(2).Address(0, 1) & ")"
End With
End Sub
Hey Fluff! Thanks so much.

But the code works only for the first column of table 2. I can edit and make it work for 2nd and 3rd column but im looking to autofill for the entire table as my table 2 has multiple columns

In short, Im looking to enter the formula in cell K2 and use autofill for the entire table (All using VBA)
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
32,317
Office Version
365
Platform
Windows
Try
Code:
rng.Resize(, [COLOR=#ff0000]5[/COLOR]).Formula
change the value in red to match the number of columns in table 2
 

predzer

New Member
Joined
Jun 21, 2018
Messages
20
Try
Code:
rng.Resize(, [COLOR=#ff0000]5[/COLOR]).Formula
change the value in red to match the number of columns in table 2
I wont be knowing the number of columns in table 2 as it is dynamic (and btw where should i include this line in your code?)
 
Last edited:

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
32,317
Office Version
365
Platform
Windows
How is table 2 being created?
 

predzer

New Member
Joined
Jun 21, 2018
Messages
20
How is table 2 being created?
In my first post, I have mentioned that i will be using below code to place table2.

Code:
Set table2top = Range("A1").End(xlToRight).Offset(0, 1) 
Set table2bot = Range("A1").End(xlToRight).End(xlDown).Offset(0, 1)

Now i have to enter the average formula in the cell (table2value) selected using below code and autofill for the entire table.

Code:
Set table2value = Range("A1").End(xlToRight).Offset(1, 1)
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
32,317
Office Version
365
Platform
Windows
None of that code shows the size of table2, it shows the starting point & the height, but not the number of columns.
If you need the formula to populate the entire table, then the macro has to now how any columns.
How will that be determined?
 

predzer

New Member
Joined
Jun 21, 2018
Messages
20
None of that code shows the size of table2, it shows the starting point & the height, but not the number of columns.
If you need the formula to populate the entire table, then the macro has to now how any columns.
How will that be determined?
Hey fluff, I dont know the size of the table 2, that is the challenge i am facing. However i will be knowing the column header range of table 2, since i am using table 1 header (date row) to remove duplicates.
So in that case i will be knowing only (K1:M1).
 

Fluff

MrExcel MVP, Moderator
Joined
Jun 12, 2014
Messages
32,317
Office Version
365
Platform
Windows
I wont be knowing the number of columns in table 2 as it is dynamic (and btw where should i include this line in your code?)
Hey fluff, I dont know the size of the table 2, that is the challenge i am facing. However i will be knowing the column header range of table 2, since i am using table 1 header (date row) to remove duplicates.
So in that case i will be knowing only (K1:M1).
Which of these 2 statements is true? :confused:
The code I supplied can be made dynamic, but I need hard facts to do it.
 

Forum statistics

Threads
1,082,287
Messages
5,364,299
Members
400,787
Latest member
bs04c

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top