If Cell Left of Selected Cell is Blank, then Sum Offset of (,-3)+(,-2) else (,-1)

MikeL

Active Member
Joined
Mar 17, 2002
Messages
488
Office Version
  1. 365
Platform
  1. Windows
Hello,
I have selected last nonblank cell in Row 3.
'Range End Method last blank cell in Row 3
Cells(3, Columns.Count).End(xlToLeft).Offset(, 1).Select

Would like If Then with logic like:
If Offset(,-1) is 0 Then Sum (offset(,-3)+ (,-2) else just Offset(,-1)

How to write this in VBA?
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
VBA Code:
Sub LastBlankCellToLeft()
    Dim c As Range
    'Range End Method last blank cell in Row 3
    Set c = Cells(3, Columns.Count).End(xlToLeft).Offset(0, 1)
    
    If c.Offset(0, -1).Value = 0 Then
        c.Formula2R1C1 = "=R[0]C[-3]+R[0]C[-2]"
    Else
        c.Formula2R1C1 = "=R[0]C[-1]"
    End If
End Sub
 
Upvote 0
VBA Code:
Sub v()
With Cells(3, Columns.Count).End(xlToLeft).Offset(, 1)
    If .Offset(, -1) <> 0 Then .Value = .Offset(, -1) Else .Value = .Offset(, -3) + .Offset(, -2)
End With
End Sub
 
Upvote 0
VBA Code:
Sub LastBlankCellToLeft()
    Dim c As Range
    'Range End Method last blank cell in Row 3
    Set c = Cells(3, Columns.Count).End(xlToLeft).Offset(0, 1)
   
    If c.Offset(0, -1).Value = 0 Then
        c.Formula2R1C1 = "=R[0]C[-3]+R[0]C[-2]"
    Else
        c.Formula2R1C1 = "=R[0]C[-1]"
    End If
End Sub
Thanks I got error 438 on this line c.Formula2R1C1 = "=R[0]C[-1]"
 
Upvote 0
VBA Code:
Sub v()
With Cells(3, Columns.Count).End(xlToLeft).Offset(, 1)
    If .Offset(, -1) <> 0 Then .Value = .Offset(, -1) Else .Value = .Offset(, -3) + .Offset(, -2)
End With
End Sub
Thanks - this works. How do I autofill down?
 
Upvote 0
VBA Code:
Sub v()
Dim col&, lr&, rng As Range
col = Cells.Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1
lr = Cells.Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Set rng = Range(Cells(3, col), Cells(lr, col))
rng.FormulaR1C1 = "=IF(RC[-1]=0,RC[-3]+RC[-2],RC[-1])"
rng = rng.Value 'Omit this line if you want to keep the formula
End Sub
 
Upvote 0
Solution
VBA Code:
Sub v()
Dim col&, lr&, rng As Range
col = Cells.Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1
lr = Cells.Find(What:="*", LookIn:=xlValues, SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
Set rng = Range(Cells(3, col), Cells(lr, col))
rng.FormulaR1C1 = "=IF(RC[-1]=0,RC[-3]+RC[-2],RC[-1])"
rng = rng.Value 'Omit this line if you want to keep the formula
End Sub
Thanks
 
Upvote 0

Forum statistics

Threads
1,214,896
Messages
6,122,132
Members
449,066
Latest member
Andyg666

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