Convert a portion of a formula from absolute to relative

mjl78

New Member
Joined
May 5, 2011
Messages
3
I'm writing a macro and am copying a formula, that is absolute, from a cell one worksheet, to another cell on another worksheet, The formula is as follows
=IF(AND(Input!$C$3="",Input!$D$3=""),"",IF($F$3="cash","cash",IF(Input!$C$3>0,Input!$C$2,Input!$D$2)))

Once pasted I need to convert the formula to the following
=IF(AND(Input!c3="",Input!D3=""),"",IF(F3="cash","cash",IF(Input!C3>0,Input!$C$2,Input!$D$2)))

Basically I need to convert characters C3, D3, and F3 to relative, while leaving characters $C$2 and $D$2 as absolute. Does anyone know the vba code to accomplish this (using excel 2007)

Thanks so much

Marty
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
The following macro copies the formula from one cell to another, then it converts all references to a relative reference, and then it converts references to C2 and D2 to absolute references...

Code:
[font=Verdana][color=darkblue]Option[/color] [color=darkblue]Explicit[/color]

[color=darkblue]Sub[/color] test()

    [color=darkblue]Dim[/color] SourceCell [color=darkblue]As[/color] Range
    [color=darkblue]Dim[/color] DestCell [color=darkblue]As[/color] Range
    
    [color=darkblue]Set[/color] SourceCell = Sheets("Sheet1").Range("A1")
    [color=darkblue]Set[/color] DestCell = Sheets("Sheet2").Range("A1")

    SourceCell.Copy Destination:=DestCell
    
    DestCell.Formula = Application.ConvertFormula(DestCell.Formula, xlA1, , xlRelative)
    
    DestCell.Formula = Replace(Replace(DestCell.Formula, "C2", "$C$2"), "D2", "$D$2")
    
[color=darkblue]End[/color] [color=darkblue]Sub[/color]
[/font]
 
Upvote 0

Forum statistics

Threads
1,224,525
Messages
6,179,319
Members
452,905
Latest member
deadwings

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