Laura
Laura

Reputation: 103

VBA: Addition in cells

I'm sorting through data and need it to be formatted the same way. The input might have cells with 6+18
12
3
5+14
20
And I want to make them all integers. I've written this so far:

If InStr(data.Cells(x, 8), "+") > 0 Then
    'Still trying to figure out this part
Else
    calc.Cells((lrcalc + 1), (col + s)).Value = data.Cells(x, 8)
End If

How would I add the value on the left of the "+" sign to the value on the right?

Upvotes: 4

Views: 104

Answers (1)

Siddharth Rout
Siddharth Rout

Reputation: 149305

Well there is no need to check for +. Simply add an = sign in each cell and let excel calculate it ;)

Sub Sample()
    Dim lRow As Long, i As Long
    Dim ws As Worksheet

    '~~> Change this to the relevant sheet
    Set ws = ActiveSheet

    With ws
        '~~> Find last row
        lRow = .Range("A" & .Rows.Count).End(xlUp).Row

        For i = 1 To lRow
            With .Range("A" & i)
                .Formula = "=" & .Value
                .Value = .Value
            End With
        Next i
    End With
End Sub

Screenshot

enter image description here

Upvotes: 5

Related Questions