PDA

View Full Version : VBA - Macro - EXCell getting it to insert a formula (offset)



msussams
01-31-2021, 02:29 PM
Hi All,

I don't know how to do this - and was wondering if any of you will know how...
I want to get the macro to insert a formula into an empty cell

So when I type in one box it will (in addition to adding other text to other cells) input an =if(...) formula with "text" as part of the formula..
currently vba is not reading it correctly... as it has too many " for the text I want to input....

Is there a way of doing this
So I would input a date in one cell
and the macro will put in text/dates and then this formula into a different cell?

rollis13
01-31-2021, 04:31 PM
This is a how you can insert a formula with text in a cell via macro. You need to double the double quotation marks:
Range("C" & x).Formula = "=IF(B1>B2,""greater"",""smaller"")"

msussams
02-01-2021, 03:08 AM
This is a how you can insert a formula with text in a cell via macro. You need to double the double quotation marks:
Range("C" & x).Formula = "=IF(B1>B2,""greater"",""smaller"")"

is there a way I can use the offset command vice a specific field?

rollis13
02-01-2021, 03:45 AM
Oh yeah, you can do almost everything as long as you double the double quotation marks for test output.

msussams
02-01-2021, 03:54 AM
Oh yeah, you can do almost everything as long as you double the double quotation marks for test output.

Thanks for the reply

I'm trying to put this:
Target.Offset(0, 8).formula = ""=if(I29="TEFA","HAHA!","it worked!")""

But it doesn't recognise the expression
I've tried it with this too.

Target.Offset(0, 8).formula = "=if(I29="TEFA","HAHA!","it worked!")"

In addtion to this, how, if not done automatically, can I make the I29 (eg) a relative reference eg when i type in the next row - it will replace with I30 etc.....
(this formula is a long ish one with a vlookup in it - I just typed that to test it.)

Is there anything I am doing wrong?
M

rollis13
02-01-2021, 04:39 AM
Leading and ending double quotes are single because these are for the formula. It's only for the text inside the formula that you have to use double double quotation.

snb
02-01-2021, 05:08 AM
In addtion to this, how, if not done automatically, can I make the I29 (eg) a relative reference eg when i type in the next row - it will replace with I30 etc.....

Why don't you do this manually ?