So I'm still re-writing some code froma macro we use and I'm trying to make it more adaptable to future changes in the workbook.
I have the following code:
It creates the following formula:
This code works fine and puts the correct formula in the correct cell, but I'm a little concerned that if the columns ever move/change this formula goes awry.
If in my VBA variables I've setup the following:
column K is known as DS_ActStatus_Col
column R is known as DS_YTDActuals_Col
column H is known as DS_WPC_Col
column L is known as DS_IntMed_Col
and row 5 is known as myDSStartRow
Is it possible to write a code that will mimic what my above code does (create the formula correctly) using the variables in my VBA (listed above). I've never tried to set cell formulas through VBA using VBA variables before, so I'm not sure what to do.
For those who can't follow/understand my example, if you have your own examples those would be appreciated; I can usually figure out the code of others
I have the following code:
Code:
ActiveCell.Offset(0, 2).Value = "=IF(OR(RC[-2]=""Unplanned"",RC[-2]=""Cancelled""),0,IF(OR(RC[-2]=""Completed"", RC[-2]=""Terminated""), RC[5],VLOOKUP(RC[-5],'PCD Work Packages'!C[-5]:C[-1],5,0)))"
It creates the following formula:
Code:
'NOTE: this is the formula that results from the above code, it is not VBA
=IF(OR(K5="Unplanned",K5="Cancelled"),0,IF(OR(K5="Completed", K5="Terminated"), R5,VLOOKUP(H5,H:L,5,0)))
This code works fine and puts the correct formula in the correct cell, but I'm a little concerned that if the columns ever move/change this formula goes awry.
If in my VBA variables I've setup the following:
column K is known as DS_ActStatus_Col
column R is known as DS_YTDActuals_Col
column H is known as DS_WPC_Col
column L is known as DS_IntMed_Col
and row 5 is known as myDSStartRow
Is it possible to write a code that will mimic what my above code does (create the formula correctly) using the variables in my VBA (listed above). I've never tried to set cell formulas through VBA using VBA variables before, so I'm not sure what to do.
For those who can't follow/understand my example, if you have your own examples those would be appreciated; I can usually figure out the code of others