Howto create a formula in VBA based on another formula

brianfosterblack

Active Member
Joined
Nov 1, 2011
Messages
251
I am running a macro in VBA which ends in a cell in row 1 of the worksheet. I then want to create the formula in the Activecell, in my macro to increment the row number of the Cell 2 cells to the left by 1 in the Active cell. The formula 2 columns to the left will be in the same row number 1 of the worksheet.
eg. if the formula 2 cells to the left =
Excel Formula:
='1Master'!$AA2
The formula I want to create will increment the above formula by 1 row so I end up with the activecell formula as
Excel Formula:
='1Master'!$AA3
If I run the Macro again, I will finish in the cell two columns to the right and will then create a formula again
Excel Formula:
='1Master'!$AA4
Can anyone help with this code.
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Can you walk us through a few cycles of an actual example in more detail?
Where exactly is this formula being entered (what cell)?
Is it overwriting a current formula, or adding a new formula in a blank cell?
How does it know what to use for the formula the first time through?
 
Upvote 0
Can you walk us through a few cycles of an actual example in more detail?
Where exactly is this formula being entered (what cell)?
Is it overwriting a current formula, or adding a new formula in a blank cell?
How does it know what to use for the formula the first time through?
Hi Joe, thanks for the reply.
The first formula is already inserted in the worksheet in cell C1. I currently have 2 columns in column C and D and the formula is in row 1 of column C
Every time I get a new client, I have to add 2 columns to the summary worksheet.
To do this I have a macro which copies the last 2 columns in the summary worksheet and in the next 2 available empty columns it pastes the formulae, formats and column widths for the new client.. At the top of the first column is a formula referring to the list of all the client sheets in my workbook. In Cell C1 the formula refers to
Excel Formula:
=1Master!AA2
When I copy the columns I one again end up with a formula which also =
Excel Formula:
=1Master!AA2
but as I want it to refer to the next name in the sheet list I need the formula to be changed to
Excel Formula:
=1Master!AA3
The formulae in the columns use the "Indirect" function with the the above formula to pick up the data in the relevant client sheet referred to in this formula.

Hope this makes sense.
 
Upvote 0
Still hoping more the nitty-gritty details here (without any kind of images, all we have to work off of is your explanation).

So, if I understand you correctly, you are starting off with this formula in cell C1:
Excel Formula:
=1Master!AA2
is that correct?

And then what cell exactly (cell address, please) arr you trying to copy this formula to?
And you want that to be:
Excel Formula:
=1Master!AA3
correct?

The formulae in the columns use the "Indirect" function with the the above formula to pick up the data in the relevant client sheet referred to in this formula.
I am not sure where this plays in to everything.
What exactly is the formula, and where is that located?
 
Upvote 0
Still hoping more the nitty-gritty details here (without any kind of images, all we have to work off of is your explanation).

So, if I understand you correctly, you are starting off with this formula in cell C1:
Excel Formula:
=1Master!AA2
is that correct?

And then what cell exactly (cell address, please) arr you trying to copy this formula to?
And you want that to be:
Excel Formula:
=1Master!AA3
correct?


I am not sure where this plays in to everything.
What exactly is the formula, and where is that located?
Lets take this with your first Formula in C1 which is correct is
when I copy the last 2 columns, which in this case is column C and D to the first empty columns it copies to columns E and F with the formula in C1 now being copied to Column E1 as
Excel Formula:
=Master!AA2
but the formula in Column E1 needs to be
Excel Formula:
=Master!AA3

The next time I run the Macro I will copy columns E & F to columns G and H and in this case the formula copied to cell G1 will need to be changed to
Excel Formula:
=Master!AA4
 
Upvote 0
If you put this formula in cell C1, I think it will do what you want when you copy it to E1, G1, I1, etc.
Excel Formula:
=INDIRECT("'1Master'!AA" & (COLUMN()+1)/2)
It will increase the row reference by 1 each time, even though you are jumping 2 columns.
 
Upvote 0
Solution
If you put this formula in cell C1, I think it will do what you want when you copy it to E1, G1, I1, etc.
Excel Formula:
=INDIRECT("'1Master'!AA" & (COLUMN()+1)/2)
It will increase the row reference by 1 each time, even though you are jumping 2 columns.
You are right - this works perfectly, thank you for the patience and effort.
 
Upvote 0
You are welcome!
Glad I was able to help.

Note: Here is another solution that uses OFFSET instead of INDIRECT.
I don't think there is any advantage to this one, just another option:
Excel Formula:
=OFFSET('1Master'!$AA$1,(COLUMN()-1)/2,0)
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,606
Members
449,089
Latest member
Motoracer88

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