Update formula Col reference Macro

xcelnovice

Board Regular
Joined
Dec 31, 2011
Messages
81
Is there a way to use a macro to update a column a sum if or vlookup formula is pulling data from. Ex for a vlookup I have 12 mos of budget data in columns. In Feb I am pulling data from the 8th Col in the table. I'd like to run a macro that would update that number by 1 to pull Col 9 next month and Col 10 the following. It'd be similar with the sum of but increase from Col D to Col F etc. Thanks
 

Some videos you may like

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.

dave3009

Well-known Member
Joined
Jun 23, 2006
Messages
7,002
Office Version
  1. 365
  2. 2016
Hi xcelnovice,

Are you sure you really need to do this with VBA? Here are some dynamic examples that work using worksheet functions to pick column number.

<b>Excel 2007</b><table cellpadding="2.5px" rules="all" style=";background-color: #FFFFFF;border: 1px solid;border-collapse: collapse; border-color: #A6AAB6"><colgroup><col width="25px" style="background-color: #E0E0F0" /><col /><col /><col /><col /><col /><col /></colgroup><thead><tr style=" background-color: #E0E0F0;text-align: center;color: #161120"><th></th><th>A</th><th>B</th><th>C</th><th>D</th><th>E</th><th>F</th></tr></thead><tbody><tr ><td style="color: #161120;text-align: center;">1</td><td style="text-align: right;;"></td><td style="text-align: right;;">Jan</td><td style="text-align: right;;">Feb</td><td style="text-align: right;;">Mar</td><td style="text-align: right;;">Apr</td><td style="text-align: right;;">May</td></tr><tr ><td style="color: #161120;text-align: center;">2</td><td style=";">Data 1</td><td style="text-align: right;;">14181</td><td style="text-align: right;;">16800</td><td style="text-align: right;;">15578</td><td style="text-align: right;;">19393</td><td style="text-align: right;;">12412</td></tr><tr ><td style="color: #161120;text-align: center;">3</td><td style=";">Data 2</td><td style="text-align: right;;">15203</td><td style="text-align: right;;">15498</td><td style="text-align: right;;">14216</td><td style="text-align: right;;">17863</td><td style="text-align: right;;">13151</td></tr><tr ><td style="color: #161120;text-align: center;">4</td><td style=";">Data 3</td><td style="text-align: right;;">12479</td><td style="text-align: right;;">17368</td><td style="text-align: right;;">10104</td><td style="text-align: right;;">12234</td><td style="text-align: right;;">19279</td></tr><tr ><td style="color: #161120;text-align: center;">5</td><td style=";">Data 4</td><td style="text-align: right;;">19157</td><td style="text-align: right;;">17592</td><td style="text-align: right;;">15380</td><td style="text-align: right;;">12116</td><td style="text-align: right;;">16113</td></tr><tr ><td style="color: #161120;text-align: center;">6</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">7</td><td style="text-align: right;;"></td><td style="text-align: right;;">Feb</td><td style="text-align: right;;">Feb</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">8</td><td style=";">Data 1</td><td style="text-align: right;;">16800</td><td style="text-align: right;;">16800</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">9</td><td style=";">Data 2</td><td style="text-align: right;;">15498</td><td style="text-align: right;;">15498</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">10</td><td style=";">Data 3</td><td style="text-align: right;;">17368</td><td style="text-align: right;;">17368</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr><tr ><td style="color: #161120;text-align: center;">11</td><td style=";">Data 4</td><td style="text-align: right;;">17592</td><td style="text-align: right;;">17592</td><td style="text-align: right;;"></td><td style="text-align: right;;"></td><td style="text-align: right;;"></td></tr></tbody></table><p style="width:4.2em;font-weight:bold;margin:0;padding:0.2em 0.6em 0.2em 0.5em;border: 1px solid #A6AAB6;border-top:none;text-align: center;background-color: #E0E0F0;color: #161120">Sheet3 </p><br /><br /><table width="85%" cellpadding="2.5px" rules="all" style=";border: 2px solid black;border-collapse:collapse;padding: 0.4em;background-color: #FFFFFF" ><tr><td style="padding:6px" ><b>Worksheet Formulas</b><table cellpadding="2.5px" width="100%" rules="all" style="border: 1px solid;text-align:center;background-color: #FFFFFF;border-collapse: collapse; border-color: #A6AAB6"><thead><tr style=" background-color: #E0E0F0;color: #161120"><th width="10px">Cell</th><th style="text-align:left;padding-left:5px;">Formula</th></tr></thead><tbody><tr><th width="10px" style=" background-color: #E0E0F0;color: #161120">C7</th><td style="text-align:left">=B7</td></tr><tr><th width="10px" style=" background-color: #E0E0F0;color: #161120">B8</th><td style="text-align:left">=INDEX(<font color="Blue">$A$1:$F$5, MATCH(<font color="Red">$A8, $A$1:$A$5, 0</font>), MATCH(<font color="Red">B$7, $A$1:$F$1, 0</font>)</font>)</td></tr><tr><th width="10px" style=" background-color: #E0E0F0;color: #161120">C8</th><td style="text-align:left">=VLOOKUP(<font color="Blue">A8, $A$2:$F$5, MONTH(<font color="Red">$C$7</font>)+1, 0</font>)</td></tr></tbody></table></td></tr></table><br />
 

xcelnovice

Board Regular
Joined
Dec 31, 2011
Messages
81
Awesom the index match worked great. Is there a more dynamic sum if? Thanks for the help!
 

dave3009

Well-known Member
Joined
Jun 23, 2006
Messages
7,002
Office Version
  1. 365
  2. 2016
Using the same values and ranges here are two options

<b>Excel 2007</b><table cellpadding="2.5px" rules="all" style=";background-color: #FFFFFF;border: 1px solid;border-collapse: collapse; border-color: #A6AAB6"><colgroup><col width="25px" style="background-color: #E0E0F0" /><col /></colgroup><thead><tr style=" background-color: #E0E0F0;text-align: center;color: #161120"><th></th><th>D</th></tr></thead><tbody><tr ><td style="color: #161120;text-align: center;">7</td><td style="text-align: right;;">Mar</td></tr><tr ><td style="color: #161120;text-align: center;">8</td><td style="text-align: right;;">55278</td></tr><tr ><td style="color: #161120;text-align: center;">9</td><td style="text-align: right;;">55278</td></tr></tbody></table><p style="width:4.2em;font-weight:bold;margin:0;padding:0.2em 0.6em 0.2em 0.5em;border: 1px solid #A6AAB6;border-top:none;text-align: center;background-color: #E0E0F0;color: #161120">Sheet3 </p><br /><br /><table width="85%" cellpadding="2.5px" rules="all" style=";border: 2px solid black;border-collapse:collapse;padding: 0.4em;background-color: #FFFFFF" ><tr><td style="padding:6px" ><b>Worksheet Formulas</b><table cellpadding="2.5px" width="100%" rules="all" style="border: 1px solid;text-align:center;background-color: #FFFFFF;border-collapse: collapse; border-color: #A6AAB6"><thead><tr style=" background-color: #E0E0F0;color: #161120"><th width="10px">Cell</th><th style="text-align:left;padding-left:5px;">Formula</th></tr></thead><tbody><tr><th width="10px" style=" background-color: #E0E0F0;color: #161120">D7</th><td style="text-align:left">=C7</td></tr><tr><th width="10px" style=" background-color: #E0E0F0;color: #161120">D8</th><td style="text-align:left">=SUM(<font color="Blue">OFFSET(<font color="Red">A1, 1, MATCH(<font color="Green">D7, $B$1:$G$1, 0</font>), 4, 1</font>)</font>)</td></tr><tr><th width="10px" style=" background-color: #E0E0F0;color: #161120">D9</th><td style="text-align:left">=SUM(<font color="Blue">INDEX(<font color="Red">$A$1:$F$5, 2, MATCH(<font color="Green">D7, $A$1:$G$1, 0</font>)</font>):INDEX(<font color="Red">$A$1:$F$5, 5, MATCH(<font color="Green">D7, $A$1:$G$1, 0</font>)</font>)</font>)</td></tr></tbody></table></td></tr></table><br />
 

Watch MrExcel Video

Forum statistics

Threads
1,123,304
Messages
5,600,865
Members
414,409
Latest member
FloordAlex

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
Top