View Poll Results: Did you find this posting useful?

Voters
0. This poll is closed
  • Yes

    0 0%
  • No

    0 0%
Multiple Choice Poll.
+ Reply to Thread
Results 1 to 7 of 7

Need to Convert Formula R1C1 into A1-style but the Formula String exceeds 255 characters

Hybrid View

  1. #1
    Registered User
    Join Date
    04-02-2012
    Location
    Sunnyvale, California
    MS-Off Ver
    Excel 2010
    Posts
    5

    Need to Convert Formula R1C1 into A1-style but the Formula String exceeds 255 characters

    Hi - I have run into a problem where I am converting a formula in R1C1 format to A1-style. I am using a formula conversion function that works fine as long as the length of the original formula string is less than 256 characters.
    For example,
    Original formula: "=R4C5*R7C5+R4C6*R7C6+R4C6*R7C7...." (Formula Length < 256)
    Converted formula "=$E$4*$E$7+$F$4*$F$7+$G$4*$G$7...." (Formula conversion works fine)

    But when the code gets to Row 10 and above, the formula string length exceeds 256 characters and the resulting value in my cell is "#Value". Here is a module of code where I have replaced most of the For-Next variable counters with constants. Is there a better way to generate the formula where I do not have to convert from R1C1-style to A1-style?

    Sub CreateFormula()
    
        Dim i, j As Integer
        Dim RN As Integer ' Row Number ID
        Dim CN As Integer ' Col Number ID
        Dim WFR, WFC As Integer  'Weighting Factor Row and Weighting Factor Column ID
        Dim MyFormula, NewFormula As Variant
    'Add Formulas
        Range("AD7").Select
        RN = 7 'starting row number
        CN = 5 'Starting col number
        
        WFR = 4  'weighting factor row
        WFC = 5  'weighting factor column
        Dim BP_Formula_Str As String
        
        For j = 1 To 15  'row counter
            
            BP_Formula_Str = "=R" & WFR & "C" & CN & "*R" & RN & "C" & CN
            
            
                For i = 1 To 20  'column counter
                
                    CN = CN + 1
                    BP_Formula_Str = BP_Formula_Str + "+R" & WFR & "C" & CN & "*R" & RN & "C" & CN
                   
                
                Next i
            
            'Convert Formula
               MyFormula = BP_Formula_Str
               Call FormulaConvert(MyFormula, NewFormula)
               'Debug.Print Len(MyFormula) & " - " & Len(NewFormula)
                  
            'Paste Formula
                ActiveCell.formula = NewFormula             'now in A1-style formula
                ActiveCell.Font.Size = 9
                ActiveCell.Font.Bold = True
                
            'Reset Variables
                BP_Formula_Str = ""
                ActiveCell.Offset(1, 0).Select
                CN = WFC
                RN = RN + 1
        Next j
        
       'Format column width
            With Selection
            .EntireColumn.AutoFit
            End With
    End Sub
    
    Function FormulaConvert(MyFormula As Variant, NewFormula As Variant)
        Dim inputFormula As Variant
        inputFormula = ""
        inputFormula = MyFormula   'original formula in R1C1 format
        
        'NewFormula contains the A1-style version
        Debug.Print Len(inputFormula)
            
            NewFormula = Application.ConvertFormula( _
            formula:=inputFormula, _
            fromReferenceStyle:=xlR1C1, _
            toReferenceStyle:=xlA1, ToAbsolute:=xlAbsRowRelColumn)
        
        'Debug.Print Len(NewFormula)
        
    End Function

  2. #2
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: Need to Convert Formula R1C1 into A1-style but the Formula String exceeds 255 characte

    Perhaps the code could create an equivalent SUMPRODUCT() formula instead?
    Gary's Student

  3. #3
    Registered User
    Join Date
    04-02-2012
    Location
    Sunnyvale, California
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Need to Convert Formula R1C1 into A1-style but the Formula String exceeds 255 characte

    Hi JakobShavn,
    Thanks for your suggestion. I will look up the SUMPRODUCT() formula and see if this will be an alternate way to generate this long formula. - VBA_Gary

  4. #4
    Registered User
    Join Date
    04-02-2012
    Location
    Sunnyvale, California
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Need to Convert Formula R1C1 into A1-style but the Formula String exceeds 255 characte

    A fix has been found.
    The problem was that I started using the R1C1-style format rather than A1-style format for building my formula. The easier way to do it was to use the ADDRESS object as shown below. Since my formula used both relative and absolute addressing, the Evaluate(ADDRESS) method allowed me to create the formula using the 4 different options.

    Thanks to those that added their comments.

    
        Dim RN As Integer ' Row Number ID
        Dim CN As Integer ' Col Number ID
        Dim WFR, WFC As Integer  'Weighting Factor Row and Weighting Factor Column ID
        Dim Formula_Pt1, Formula_Pt2 As String  'Two parts to the formula
        Dim Ce1, Ce2, Ce3, Ce4 As Variant  'Ce1 means Cell 1, Ce2 means Cell 2 , etc.
        
        
        ActiveCell.Offset(1, 0).Select
        RN = ActiveCell.Row      'Get the row number for the current active cell
        CN = Range("E:E").Column 'Get the column number for column E
        
        WFR = 4      'Weighting Factor Row is 4
        WFC = 5      'Weighting Factor Col is 5 ("E")
    
        Dim BP_Formula_Str As String
    
            'sample "E$4"  AbsoluteRowRelativeColumn addressing, option 2
               Ce1 = Evaluate("Address(" & WFR & "," & CN & ", 2, 1)")
            'sample "E7"   Relative addressing, option 4
               Ce2 = Evaluate("Address(" & RN & "," & CN & ", 4, 1)")  
    
            
            Formula_Pt1 = Ce1 & "*" & Ce2        ' Sample "E$4*E7"
            BP_Formula_Str = "=" & Formula_Pt1
            
            For i = 1 To 21
            
                CN = CN + 1
    
                'AbsoluteRowRelativeColumn, option 2
                Ce3 = Evaluate("Address(" & WFR & "," & CN & ", 2, 1)")             
                Ce4 = Evaluate("Address(" & RN & "," & CN & ", 4, 1)")  'Relative, option 4
                
                Formula_Pt2 = Ce3 & "*" & Ce4    
                BP_Formula_Str = BP_Formula_Str & "+" & Formula_Pt2
            
            Next i
    
            'Paste Formula
            ActiveCell.formula = BP_Formula_Str    'now formula is in A1-style 
            ActiveCell.Font.Size = 9
            ActiveCell.Font.Bold = True
            
            'Reset Variables
            
            BP_Formula_Str = ""
            ActiveCell.Copy
    
            'Now paste the formula down the column
            Range(ActiveCell, ActiveCell.Offset(FilerCount - 1, 0)).PasteSpecial
              
        With Selection
          .EntireColumn.AutoFit
        End With

  5. #5
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Need to Convert Formula R1C1 into A1-style but the Formula String exceeds 255 characte

    Do you need a poll for this question? I have closed it considering that you are new to this forum.
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  6. #6
    Forum Expert Cutter's Avatar
    Join Date
    05-24-2004
    Location
    Ontario,Canada
    MS-Off Ver
    Excel 2010
    Posts
    6,451

    Re: Need to Convert Formula R1C1 into A1-style but the Formula String exceeds 255 characte

    @ VBA_Gary

    Based on your last post it seems that you solved your issue but you haven't marked your thread as SOLVED. I'll do that for you now but please keep in mind for your future threads that Rule #9 requires you to do that yourself. If your problem has not been solved you can use Thread Tools (located above your first post) and choose "Mark this thread as unsolved".
    Thanks.

  7. #7
    Registered User
    Join Date
    04-02-2012
    Location
    Sunnyvale, California
    MS-Off Ver
    Excel 2010
    Posts
    5

    Re: Need to Convert Formula R1C1 into A1-style but the Formula String exceeds 255 characte

    Thanks Cutter for marking this as SOLVED. I looked around the website for the way to mark it as solved but did not find it. I now see it under the Thread Tools tab. Thanks for your help. - VBA_Gary

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1