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.
attachment.php


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
banghead.gif
please help me out
pray2.gif


(P.S. Also posted on
http://www.vbaexpress.com/forum/sho...nges-(without-range-address)-inside-a-Formula ​)
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
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
 
Upvote 0
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)
 
Upvote 0
Try
Code:
rng.Resize(, [COLOR=#ff0000]5[/COLOR]).Formula
change the value in red to match the number of columns in table 2
 
Upvote 0
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:
Upvote 0
How is table 2 being created?
 
Upvote 0
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)
 
Upvote 0
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?
 
Upvote 0
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).
 
Upvote 0
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.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,213
Members
452,618
Latest member
Tam84

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