VBA Problem with Formulas-to-Values in Non-Adjacent Cells

pbornemeier

Well-known Member
Joined
May 24, 2005
Messages
3,911
This code exposes what I consider to be a bug in Excel along with a workaround. It will probably not occur in most circumstances.

Code:
Option Explicit

Sub DemoProblemWhenUpdatingFormulasInNonAdjacentCells()

    'This code demonstrates a problem that I found the hard way
    '  as well as a solution.  Set VBE small enough to see
    '  worksheet as well as code.  Step through code and watch what happens.
    
    Dim rng As Range
    
    'Reset Demo Data Field
    With Range("A1:H20")
        .FormulaR1C1 = "=Row()"
        .Value = .Value
        .Interior.Color = -4142
    End With
    Stop
    'Update Values in a block of cells
    With Range("C5:F10")
        .FormulaR1C1 = "=R[-1]C*5"  'The formulas show correct answers
        .Interior.Color = vbYellow
        Stop
        .Value = .Value 'The values show the correct answer.
        Stop
    End With
    
    'Reset Demo Data Field
    With Range("A1:H20")
        .FormulaR1C1 = "=Row()"
        .Value = .Value
        .Interior.Color = -4142
    End With
    Stop
    'Update Values in a few separate randomly placed cells
    With Range("G15,D9,D18,H20,H7,B5")
        .FormulaR1C1 = "=R[-1]C*5"  'The formulas show correct answers
        .Interior.Color = vbYellow
        Stop
        .Value = .Value 'The values show the answer for
                        'the first cell in the range.  This behavior was
                        'not what I expected.  Explore further.
    End With
    
    'Reset Demo Data Field
    With Range("A1:H20")
        .FormulaR1C1 = "=Row()"
        .Value = .Value
        .Interior.Color = -4142
    End With
    Stop
    'Update Values in two separate blocks of cells, and a slightly different formula
    With Range("C3:E7,E12:F14") 'First block is larger
        .FormulaR1C1 = "=R[-1]C*(5+COLUMN())"  'The formulas show correct answers
        .Interior.Color = vbYellow
        Stop
        .Value = .Value     'The values in the second block of cells are filled
                            '(columns then rows) with the values in the first block
                            'of cells (columns then rows)
    End With
    
    'Reset Demo Data Field
    With Range("A1:H20")
        .FormulaR1C1 = "=Row()"
        .Value = .Value
        .Interior.Color = -4142
    End With
    Stop
    'Update Values in two separate blocks of cells, and a slightly different formula
    With Range("E12:F14,C3:E7") 'Second block is larger
        .FormulaR1C1 = "=R[-1]C*(5+COLUMN())"  'The formulas show correct answers
        .Interior.Color = vbYellow
        Stop
        .Value = .Value     'The values in the second block of cells are filled
                            '(columns then rows) with the values in the first block
                            'of cells (columns then rows).  If there are fewer
                            'cells in the first block, the remaining cells in the
                            'second block are now #N/A
        Stop
    End With
    
    
    'SOLUTION
    'Update formula by areas to preserve the correct answers in each area
    
    'Reset Demo Data Field
    With Range("A1:H20")
        .FormulaR1C1 = "=Row()"
        .Value = .Value
        .Interior.Color = -4142
    End With
    Stop
    'With two separate blocks of cells, and a slightly different formula
    With Range("E12:F14,C3:E7")
        For Each rng In .Areas
            With rng
                .FormulaR1C1 = "=R[-1]C*(5+COLUMN())"  'The formulas show correct answers
                .Interior.Color = vbYellow
                Stop
                .Value = .Value 'The values show correct answers
                Stop
            End With
        Next
    End With

    'The solution works in all instances that I tested.  The requirement to do this does
    '  look like an Excel bug to me since the formulas do show the correct values and
    '  errors come to view when converting them to values.
    
End Sub
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
This simplified code essentially reproduces (Test1) some of what you have observed. Working with a range that has more than one area can produce surprises that are not necessarily indicative of a bug.
Code:
Sub test1()
'Place random numbers (as constants) in several non-contiguous cells then select those cells
With Selection
.Value = .Value ' Places the value of the upper-left cell in all selected cells
End With
End Sub
Sub test2()
'Place random numbers (as constants) in several non-contiguous cells then select those cells
For Each c In Selection
    c.Value = c.Value 'Each cell in the selection gets correct value
Next c
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,847
Messages
6,121,911
Members
449,054
Latest member
luca142

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top