VBA to paste a formula

Russk68

Well-known Member
Joined
May 1, 2006
Messages
589
Office Version
  1. 365
Platform
  1. MacOS
Hi all
I am using the code below to replace the formula in a cell with a BeforeDoubleClick event but I am getting a syntax error. (Ex: If a cell is missing the formula, I just double click it to replace the formula)

This is the formula that I am trying to paste:
=IF(C4="",F3,IFERROR(INDEX(Instruments!Z$3:Z$68,MATCH(CI4,Instruments!AA$3:AA$68,0))+F3,""))

This formula is in the first cell and it needs to be relative when it is pasted in any cell in the column that I double click.


Code:
If Not Intersect(Target, Range("F:F")) Is Nothing Then 'change this column to suit
    Target.Formula = "=IF(C" & ActiveCell.Row & ="",F" & ActiveCell.Row & ,IFERROR(INDEX(Instruments!Z$3:Z$68,MATCH(CI" & ActiveCell.Row & ,Instruments!AA$3:AA$68,0))+F3,""))"
    Target.Offset(1, 0).Select
End If

I would like to be able to do it this way but I could probably just have code that just copies the formula from the cell above from where I double click.

Thank you in advance!
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
Code:
Target.Formula = "=IF(C" & ActiveCell.Row & ="",F" & ActiveCell.Row & ,IFERROR(INDEX(Instruments!Z$3:Z$68,MATCH(CI" & ActiveCell.Row & ,Instruments!AA$3:AA$68,0))+F3,""))"
Copying from above is probably easier but if you want to persist, you are missing quite a few quote marks. It should be more like this, but there may also be an issue with some of the row numbers.
Code:
Target.Formula = "=IF(C" & ActiveCell.Row & "="""",F" & ActiveCell.Row & ",IFERROR(INDEX(Instruments!Z$3:Z$68,MATCH(CI" & ActiveCell.Row & ",Instruments!AA$3:AA$68,0))+F3,""""))"
 
Last edited:
Upvote 0
Hi Peter!

It's very close to what I am looking for. The 4th formula down is the one that was pasted. Is it possible to make it all relative so that it fits in properly?

=IF(C7="",F6,IFERROR(INDEX(Instruments!Z$3:Z$68,MATCH(CI7,Instruments!AA$3:AA$68,0))+F6,""))
=IF(C8="",F7,IFERROR(INDEX(Instruments!Z$3:Z$68,MATCH(CI8,Instruments!AA$3:AA$68,0))+F7,""))
=IF(C9="",F8,IFERROR(INDEX(Instruments!Z$3:Z$68,MATCH(CI9,Instruments!AA$3:AA$68,0))+F8,""))
=IF(C10="",F10,IFERROR(INDEX(Instruments!Z$3:Z$68,MATCH(CI10,Instruments!AA$3:AA$68,0))+F3,""))

Thank you!
 
Upvote 0
Try
Code:
Target.Formula = "=IF(C" & ActiveCell.Row & "="""",F" & ActiveCell.Row - 1 & ",IFERROR(INDEX(Instruments!Z$3:Z$68,MATCH(CI" & ActiveCell.Row & ",Instruments!AA$3:AA$68,0))+F" & ActiveCell.Row - 1 & ",""""))"
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,906
Members
452,366
Latest member
TePunaBloke

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