Noen All American 31346 Posts user info edit post |
Okay so this is quite possibly really simple, but I'm stumped.
I'm doing some Excel VBA macros, and I need to create cell formulas on the fly.
Currently I fill cells using numeric Cell notation, aka
Worksheet(1).Cells(8,9).Value = "=sum(A1:A10)"
But I need to come up with a way to translate from numeric notation to the A1 style. I need the above cell to be something like this instead:
Worksheet(1).Cells(8,9).Value = "=sum("+ Worksheet(1).Cells(1,1) + ":" + Worksheet(1).Cells(10,1) + ")"
but of course that doesnt work, as the formula needs the textual equivalent (A1:A10) and not the actual object.
Can anyone tell me how I can get a string representation of the A1 notation of a cell in VBA?
[Edited on December 12, 2006 at 2:22 PM. Reason : code]12/12/2006 2:22:11 PM |
agentlion All American 13936 Posts user info edit post |
feels like the INDIRECT method should come in handy here. .... i'm not sure how, but this is the kind of thing it does in Excel. Unfortunately, there's no direct equivalent in VBA 12/12/2006 2:56:13 PM |
Noen All American 31346 Posts user info edit post |
Figured it out.
Just had to use the R1C1 notation, excel auto-converts it to A1 depending on which option is set.
Thanks Agent, you jogged my mind and got me on the right track ` 12/12/2006 3:24:02 PM |
LimpyNuts All American 16859 Posts user info edit post |
Every Excel Range class has a .Address method and an .AddressR1C1 method:
Cells(1,1).Address will give you "$A$1".
But if you want to refer to a range like: "Column 1, Row 1 through Column 2 Row 2", try this:
Sub DoSomething() Cells(1,1).Formula = "=sum(" rowcol(1,1,2,2) & ")" End Sub
Function rowcol$(StartRow&, StartCol&, EndRow&, EndCol&)
rowcol = Mid$(Columns(StartCol).Address, 2, 1) & StartRow & ":" & _ Mid$(Columns(EndCol).Address, 2, 1) & EndRow
End Function
[Edited on December 12, 2006 at 3:31 PM. Reason : ]12/12/2006 3:31:09 PM |
Noen All American 31346 Posts user info edit post |
^Nice dude, EXACTLY what I need, this actually just made all my stuff that much easier, thanks! 12/12/2006 3:42:05 PM |
|