Austin
Austin

Reputation: 680

How can I enter a large string into an Excel cell with VBA?

I'm using Excel VBA. I have a string that I want write to a cell. I'm not sure what the actual character limitation is, but with really long ones I get a run-time error. Here's the line that the program breaks on:

Cells(i, 7).Formula = region_opp_amount & ")"

The string "region_opp_amount" in this scenario has a length of 1691. It's basically a large SUM formula. Here's what the string contains (a large list of cells to sum): "=sum(G2057,G2066,G2069,G2072,G2076,G2079,G2082,....."

Any way to overcome this limitation?

Here's the actual loop that is creating this string:

Do While i <= lastRow
    If Cells(i, 2).value <> "" Or i = lastRow Then
        If i <> lastRow Then
            region = Left(Cells(i, 2).value, InStr(Cells(i, 2).value, " - ") - 1)
            rep = Right(Cells(i, 2).value, Len(Cells(i, 2).value) - InStr(Cells(i, 2).value, " - ") - 2)
        Else
            region = ""
            rep = ""
            i = i + 1
        End If
        If rep <> last_rep And i <> 2 Then
            Rows(i).Insert
            With Rows(i)
                .ClearFormats
                .Font.Bold = True
                On Error Resume Next
                .Ungroup
                On Error GoTo 0
            End With
            lastRow = lastRow + 1
            Cells(i, 2).value = last_rep & " Subtotal:"
            Cells(i, 7).Formula = rep_opp_amount & ")"
            Cells(i, 9).Formula = replace(rep_opp_amount, "G", "I") & ")"
            Cells(i, 12).Formula = "=sum(L" & CStr(rep_start) & ":L" & CStr(i - 1) & ")"
            Cells(i, 13).Formula = "=sum(M" & CStr(rep_start) & ":M" & CStr(i - 1) & ")"
            Cells(i, 14).Formula = "=sum(N" & CStr(rep_start) & ":N" & CStr(i - 1) & ")"
            Cells(i, 15).Formula = "=sum(O" & CStr(rep_start) & ":O" & CStr(i - 1) & ")"
            Range("B" & CStr(i) & ":O" & CStr(i)).Interior.Color = 10092543
            'Rows(CStr(rep_start) & ":" & CStr(i - 1)).Group
            ReDim Preserve rep_group_array(y)
            rep_group_array(y) = CStr(rep_start) & ":" & CStr(i - 1)
            y = y + 1
            i = i + 1
            rep_opp_amount = "=sum("
            rep_start = i
        End If
        If region <> last_region And i <> 2 Then
            Rows(i).Insert
            With Rows(i)
                .ClearFormats
                .Font.Bold = True
                On Error Resume Next
                .Ungroup
                On Error GoTo 0
            End With
            lastRow = lastRow + 1
            rep_start = rep_start + 1
            Cells(i, 2).value = last_region & " Subtotal:"
            Cells(i, 7).Formula = region_opp_amount & ")"
            Cells(i, 9).Formula = replace(region_opp_amount, "G", "I") & ")"
            Cells(i, 12).Formula = replace(region_opp_amount, "G", "L") & ")"
            Cells(i, 13).Formula = replace(region_opp_amount, "G", "M") & ")"
            Cells(i, 14).Formula = replace(region_opp_amount, "G", "N") & ")"
            Cells(i, 15).Formula = replace(region_opp_amount, "G", "O") & ")"
            total_opp_amount = total_opp_amount & "G" & CStr(i) & ","
            Range("B" & CStr(i) & ":O" & CStr(i)).Interior.Color = 5296274
            'Rows(CStr(region_start) & ":" & CStr(i - 1)).Group
            ReDim Preserve group_array(index)
            group_array(index) = CStr(region_start) & ":" & CStr(i - 1)
            index = index + 1
            i = i + 1
            region_opp_amount = "=sum("
            region_start = i
        End If
        rep_opp_amount = rep_opp_amount & "G" & CStr(i) & ","
        region_opp_amount = region_opp_amount & "G" & CStr(i) & ","
        last_region = region
        last_rep = rep
    End If
    i = i + 1
Loop

The string contains this when it errors out: =sum(G2057,G2066,G2069,G2072,G2075,G2079,G2082,G2085,G2089,G2092,G2097,G2100,G2103,G2106,G2109,G2112,G2118,G2122,G2125,G2128,G2133,G2134,G2137,G2142,G2145,G2152,G2155,G2159,G2162,G2165,G2170,G2173,G2176,G2182,G2188,G2191,G2195,G2200,G2203,G2207,G2210,G2213,G2220,G2224,G2227,G2232,G2236,G2239,G2243,G2248,G2254,G2257,G2260,G2266,G2272,G2275,G2280,G2283,G2287,G2291,G2295,G2298,G2302,G2308,G2311,G2315,G2318,G2321,G2324,G2330,G2333,G2337,G2340,G2345,G2348,G2353,G2356,G2360,G2363,G2390,G2393,G2397,G2404,G2407,G2410,G2413,G2422,G2425,G2433,G2437,G2441,G2448,G2449,G2453,G2456,G2466,G2470,G2476,G2480,G2484,G2485,G2499,G2505,G2508,G2513,G2516,G2519,G2526,G2530,G2533,G2537,G2543,G2547,G2550,G2553,G2556,G2557,G2560,G2563,G2566,G2580,G2581,G2586,G2589,G2601,G2605,G2611,G2614,G2619,G2625,G2628,G2633,G2638,G2646,G2649,G2653,G2659,G2670,G2673,G2677,G2680,G2683,G2686,G2698,G2701,G2704,G2705,G2708,G2711,G2714,G2718,G2721,G2725,G2729,G2733,G2742,G2745,G2748,G2754,G2757,G2762,G2766,G2769,G2773,G2776,G2779,G2784,G2789,G2792,G2795,G2798,G2801,G2804,G2808,G2811,G2814,G2818,G2834,G2837,G2840,G2845,G2848,G2851,G2854,G2864,G2868,G2869,G2872,G2876,G2879,G2882,G2885,G2888,G2892,G2896,G2900,G2901,G2904,G2909,G2912,G2915,G2919,G2923,G2926,G2930,G2933,G2936,G2946,G2952,G2956,G2959,G2964,G2967,G2971,G2974,G2977,G2980,G2983,G2984,G2989,G2992,G2996,G3000,G3003,G3009,G3014,G3024,G3029,G3032,G3035,G3038,G3041,G3044,G3045,G3048,G3052,G3055,G3056,G3059,G3062,G3065,G3069,G3073,G3077,G3081,G3084,G3088,G3093,G3096,G3099,G3100,G3103,G3107,G3110,G3113,G3119,G3124,G3127,G3130,G3135,G3138,G3141,G3142,G3143,G3146,G3147,G3150,G3151,G3152,G3155,G3156,G3157,G3160,G3174,G3176,G3181,G3188,G3191,G3198,G3203,G3206,G3210,

Upvotes: 2

Views: 4962

Answers (1)

Siddharth Rout
Siddharth Rout

Reputation: 149305

Ok here is an example. In this example I have directly taken the string which is mentioned in the question.

LOGIC:

  1. Split the cell addresses and store them in an array
  2. Remake the string, ensuring the length is not more than 240 (I have used 230. I am considering cells like XFD1048576)
  3. Once that limit is reached create a Named Range. I have used names like MyRange1,MyRange2,MyRange3.... One can use ever shorter names instead like Rng1,Rng2...
  4. Repeat steps 2 and 3 till all named ranges are made.
  5. Simply use the named ranges in a formula

CODE:

Option Explicit

Sub Sample()
    Dim MyAr() As String
    Dim strRng As String, strFormula As String, strTmp As String
    Dim i As Long, nmRngCount As Long
    Dim ws As Worksheet

    Set ws = ThisWorkbook.Sheets("Sheet1")

    '~~> Sample Cells
    strRng = "G2057,G2066,G2069,G2072,G2075,G2079,G2082,G2085,G2089,G2092,G2097,G2100,G2103,G2106,G2109,G2112,G2118,G2122,G2125,G2128,G2133,G2134,G2137,G2142,"
    strRng = strRng & "G2145,G2152,G2155,G2159,G2162,G2165,G2170,G2173,G2176,G2182,G2188,G2191,G2195,G2200,G2203,G2207,G2210,G2213,G2220,G2224,G2227,G2232,G2236,"
    strRng = strRng & "G2239,G2243,G2248,G2254,G2257,G2260,G2266,G2272,G2275,G2280,G2283,G2287,G2291,G2295,G2298,G2302,G2308,G2311,G2315,G2318,G2321,G2324,G2330,"
    strRng = strRng & "G2333,G2337,G2340,G2345,G2348,G2353,G2356,G2360,G2363,G2390,G2393,G2397,G2404,G2407,G2410,G2413,G2422,G2425,G2433,G2437,G2441,G2448,G2449,"
    strRng = strRng & "G2453,G2456,G2466,G2470,G2476,G2480,G2484,G2485,G2499,G2505,G2508,G2513,G2516,G2519,G2526,G2530,G2533,G2537,G2543,G2547,G2550,G2553,G2556,"
    strRng = strRng & "G2557,G2560,G2563,G2566,G2580,G2581,G2586,G2589,G2601,G2605,G2611,G2614,G2619,G2625,G2628,G2633,G2638,G2646,G2649,G2653,G2659,G2670,G2673,"
    strRng = strRng & "G2677,G2680,G2683,G2686,G2698,G2701,G2704,G2801,G2804,G2808,G2811,G2814,G2818,G2834,G2837,G2840,G2845,G2848,G2851,G2854,G2864,G2868,G2869,"
    strRng = strRng & "G2882,G2885,G2888,G2892,G2896,G2900,G2901,G2904,G2909,G2912,G2915,G2919,G2923,G2926,G2930,G2933,G2936,G2946,G2952,G2956,G2959,G2964,G2967,"
    strRng = strRng & "G2983,G2984,G2989,G2992,G2996,G3000,G3003,G3009,G3014,G3024,G3029,G3032,G3035,G3038,G3041,G3044,G3045,G3048,G3052,G3055,G3056,G3059,G3062,"
    strRng = strRng & "G3081,G3084,G3088,G3093,G3096,G3099,G3100,G3103,G3107,G3110,G3113,G3119,G3124,G3127,G3130,G3135,G3138,G3141,G3142,G3143,G3146,G3147,G3150,"
    strRng = strRng & "G3157,G3160,G3174,G3176,G3181,G3188,G3191,G3198,G3203,G3206,G3210"

    '~~> Split each cell and store it's address in an array
    MyAr = Split(strRng, ",")

    nmRngCount = 1

    '~~> Loop through the array and rejoin the cell addresses
    For i = LBound(MyAr) To UBound(MyAr)
        strTmp = strTmp & "," & MyAr(i)

        '~~> 230 seems a pretty safe number even if I consider cells like XFD1048576)
        If Len(strTmp) > 230 Then
            '~~> Ignore the 1st Comma
            strTmp = Mid(strTmp, 2)

            '~~> Creat Names for the range
            ws.Range(strTmp).Name = "MyRange" & nmRngCount

            nmRngCount = nmRngCount + 1
            strTmp = ""
        End If
    Next i

    '~~> Join the Names so that we get something like
    '~~> MyRange1,MyRange2,MyRange3,....
    For i = 1 To (nmRngCount - 1)
        strFormula = strFormula & "," & "MyRange" & i
    Next

    '~~> Ignore the 1st Comma
    strFormula = Mid(strFormula, 2)

    '~~> Insert the final formula
    ws.Range("A1").Formula = "=SUM(" & strFormula & ")"
End Sub

SCREENSHOT

enter image description here

So these 6 Named Ranges have some 240 cells covered.

Upvotes: 3

Related Questions