View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
OssieMac OssieMac is offline
external usenet poster
 
Posts: 2,510
Default Returning Cell Reference/link

Hi,

The following is the logic of what I believe you need to do. It takes a sum
formula in cell A1, =SUM(B1,D1,E1) and adds cell F1 to the formula,
=SUM(B1,D1,E1,F1)

There may be better ways so you might get a better answer from someone else.
As some added advice, you should not use name as a variable because it is a
reserved word.

'Add a cell address to SUM formula

Dim strFormula As String
Dim lngFormula As Long

'Assign formula to a string variable
strFormula = Cells(1, 1).Formula

'Assign number of characters to a variable
lngFormula = Len(strFormula)

'Subtract 1 from the number of characters
lngFormula = lngFormula - 1

'Assign characters without last bracket to string variable
strFormula = Left(strFormula, lngFormula)

'Concatenate the string variable with comma, the
'added address and the the last bracket
strFormula = strFormula & "," & Cells(1, 6).Address(0, 0) & ")"

'Assign formula to a cell
Cells(1, 1) = strFormula


--
Regards,

OssieMac


"Fishnerd" wrote:

This seems like it should be fairly simple, but for the life of me, I can
figure out the correct command. I'm writing a macro that loops through a
database comparing all rows and when the a statement is true, and if it is,
it adds a reference/link to the contents of Cell(y, r) to the current formula
of Cell(x, qr). This way, if Cell(y,r) changes later, Cell(x,qr)
automatically changes as well. The end result is each cell in column qr will
have formulas that are the equivelent of =SUM(B3,B12,B56).

Here is what I've got so far... "Ref" in the macro is simply what I wish the
command was, but unfortunately it doesn't exist...

Do While Cells(x, name).Value < ""
Do While Cells(y, name).Value < ""
If (Cells(x, name).Value = Cells(y, name).Value) Then
Cells(x, qr).Formula = Cells(x, qr).Formula + Cells(y, r).Ref
y = y + 1
Else
y = y + 1
End If
Loop
x = x + 1
y = 2
Loop

Thanks for any help you can offer. I hope I at least made a little sense in
my explanation... couldn't think how to better word it...