LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 7
Default R1C1 Fix

I'm sure somebody knows a better way, but...
I have this sheet I take from messed up data and make it
useable. The problem is that the # of rows and columns varies
from one instance to the next.
So, I did the formulas by hand (fx wizard, etc) and then
switched on R1C1 and lo and behold the reference could be more
easily made dynamic by putting together a string with the
correct row and column variables, like this:

MyRange.FormulaR1C1 = "=SUM(RC(2 - lastcolumn):RC[-1]"

But, of course, the application doesn't know what the variable
"lastcolumn" is so it gets pasted literal and a #NAME# thing
comes up. But this works:

MyRange.FormulaR1C1 = "=SUM(" & RCFix(, 2 - lastcolumn) & ":"
& RCFix(, -1) & ")"

with the helper function:
Function RCFix(Optional r As Integer, Optional c As Integer)
As String
Dim rstrng As String, cstrng As String
If r = 0 Then
rstrng = "R"
ElseIf r < 0 Then
rstrng = "R[" & Format(r) & "]"
Else
rstrng = "R" & Format(r)
End If
If c = 0 Then
cstrng = "C"
ElseIf c < 0 Then
cstrng = "C[" & Format(c) & "]"
Else
cstrng = "C" & Format(c)
End If
RCFix = rstrng & cstrng
End Function

which produces, in this case:
"=SUM(RC2:RC[-1])"
or

MyRange.FormulaR1C1 = "=SUM(RC2:RC[-1])"

Which works good. I don't know if that is useful to anyone but
me but whenever I spend a few hours figuring something out I
put it here sort of as a reward.
I'm interested to see some better ways too.
--
Poseur
"That's just kooky talk." --Kramer
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
R1C1 stacia Excel Discussion (Misc queries) 0 February 16th 10 06:19 PM
help on Converting R1C1 to A1 and A1 to R1C1..tia sa2 temp Excel Discussion (Misc queries) 3 September 13th 07 08:31 AM
help on Converting R1C1 to A1 and A1 to R1C1..tia sa2 temp Excel Worksheet Functions 3 September 13th 07 08:31 AM
R1C1 Arne Hegefors Excel Discussion (Misc queries) 1 August 18th 06 10:11 AM
A:1 now R1C1 Michael E W Excel Worksheet Functions 2 December 24th 05 11:38 PM


All times are GMT +1. The time now is 11:53 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"