# Thread: Excel Formula in Macro (Excel 2002)

1. ## Excel Formula in Macro (Excel 2002)

Hi,
I have the following code in a macro attached to an Excel spreadsheet:
lngRow = 1
Do
If Range("A" & lngRow) <> "" Then
Range("Q" & lngRow).Select
ActiveCell.FormulaR1C1 = "=RC[-9]&RC[-8]&RC[-7]"
End If
lngRow = lngRow + 1
Loop Until Range("A" & lngRow) = ""

It works great, however instead of having the actual formula show up in the cell, can I tweak the code some how to generate just the value of the formula... for example, the formula that shows up due to my code above is as follows =A1&B1&C1..... if cell A1was 75 and B1 was M and C1 was CORP, then the result would be 75MCORP... I don't want the formula to be there... I'd just like the answer. I suppose I could do a copy & paste values as part of the code, but I'm guessing there's a more direct way of doing it?!?!
Thanks!!
Lana

2. ## Re: Excel Formula in Macro (Excel 2002)

Hi Hans,
Thanks for the info! Sorry if I complicated it by having my example refer to A1, B1, & C1, when in reality it was H1, I1, & J1... I was trying to make it easier for example purposes... oops! Anyway, I took part of your code and put it in (see below) and it worked perfect... I knew there had to be better way then my original approach (which of course was derived partially from a recorded macro and partially from some VBA code you had provided in to me in the past)!
Thanks as usual Hans!
Lana
'Concatenate columns to create unit
lngRow = 1
Do
If Range("A" & lngRow) <> "" Then
Range("Q" & lngRow) = Range("H" & lngRow) & Range("I" & lngRow) & Range("J" & lngRow)
End If
lngRow = lngRow + 1
Loop Until Range("A" & lngRow) = ""

3. ## Re: Excel Formula in Macro (Excel 2002)

Edited by HansV to correct mistake in original reply

Since your formula acts on column Q, the formula refers to columns H, I and J, not to columns A, B and C. <img src=/S/scratch.gif border=0 alt=scratch width=25 height=29>

If you want to set the value instead of the formula, this is the code:

lngRow = 1
Do While Range("A" & lngRow) <> ""
Range("Q" & lngRow) = Range("H" & lngRow) & Range("I" & lngRow) & Range("J" & lngRow)
lngRow = lngRow + 1
Loop

4. ## Re: Excel Formula in Macro (Excel 2002)

Around here, you usually get what you ask for, not necessarily what you want! <img src=/S/yep.gif border=0 alt=yep width=15 height=15> <img src=/S/grin.gif border=0 alt=grin width=15 height=15>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•