Only calculate formula if cell is not blank

MonicaAS

New Member
Joined
Jun 15, 2020
Messages
13
Office Version
  1. 2013
Platform
  1. Windows
In one column I have a date field. If there is no date I don't want the other columns to calculate the formula, using VBA.
Column c: 1/20/20 Column D: "formula is column c +21 days column E: "column d minus today.
its all based on the date field in column C.
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Welcome to the Board!

Assuming the first row with data in it is row 2, try this:
VBA Code:
Sub MyMacro()

    Dim lr As Long
    Dim r As Long

    Application.ScreenUpdating = False
   
'   Find last row with data in column C
    lr = Cells(Rows.Count, "C").End(xlUp).Row
   
'   Populate columns D and E with formulas, from row 2 to last row
    Range("D2:D" & lr).FormulaR1C1 = "=IF(RC[-1]<>"""",RC[-1]+21,"""")"
    Range("E2:E" & lr).FormulaR1C1 = "=IF(RC[-2]<>"""",RC[-1]-TODAY(),"""")"
    Columns("E:E").NumberFormat = "0"
   
'   Convert formulas to hard-coded values
    Range("D2:D" & lr).Value = Range("D2:D" & lr).Value
    Range("E2:E" & lr).Value = Range("E2:E" & lr).Value
   
    Application.ScreenUpdating = True
   
End Sub
 
Upvote 0
Thanks so much! one other question. this is potentially a large database is there a way for it to move down a line when it is looking for the formulas? so it doesn't recheck line 2-10 etc.
 
Upvote 0
Thanks so much! one other question. this is potentially a large database is there a way for it to move down a line when it is looking for the formulas? so it doesn't recheck line 2-10 etc.
What do you mean?
Are you saying that you will continuously be adding new data to your sheet, and running this macro multiple times?

If so, you can look at column D for the last populated row, and then start in the next row.
Something like this:
VBA Code:
Sub MyMacro()

    Dim lr As Long
    Dim sr As Long

    Application.ScreenUpdating = False
    
'   Find last row with data in column C
    lr = Cells(Rows.Count, "C").End(xlUp).Row
    
'   Find last populated cell in column D and start on row below that
    sr = Cells(Rows.Count, "D").End(xlUp).Row + 1
    
'   Populate columns D and E with formulas, from row 2 to last row
    Range("D" & sr & ":D" & lr).FormulaR1C1 = "=IF(RC[-1]<>"""",RC[-1]+21,"""")"
    Range("E" & sr & ":E" & lr).FormulaR1C1 = "=IF(RC[-2]<>"""",RC[-1]-TODAY(),"""")"
    Columns("E:E").NumberFormat = "0"
    
'   Convert formulas to hard-coded values
    Range("D" & sr & ":D" & lr).Value = Range("D" & sr & ":D" & lr).Value
    Range("E" & sr & ":E" & lr).Value = Range("E" & sr & ":E" & lr).Value
    
    Application.ScreenUpdating = True
    
End Sub
 
Upvote 0
One last question, if there are comments in field G I want to clear contents in column F this is what I tried
If Range("G" & sr & ":G" & lr) <> "" Then
Range("F" & sr & ":F" & lr).ClearContents

End If
 
Upvote 0
One last question, if there are comments in field G I want to clear contents in column F this is what I tried
If Range("G" & sr & ":G" & lr) <> "" Then
Range("F" & sr & ":F" & lr).ClearContents

End If
You cannot do an IF statement on a whole range of values at once like that.

When you say there are "Comments" in column G, do you mean just something typed in the cell, or a "Cell Comment" (where you have the little colored flag in the upper right corner of the cell, and if you select the cell, it will show you what the comment says, and if you move off of it, it will be hidden and all you see is the colored triangle)?
Here is a deeper explanation of Excel Comments: Insert comments and notes in Excel
 
Upvote 0
One way is to use a loop, i.e.
VBA Code:
Dim r as Long
For r = sr to lr
    If Cells(r,"G")<>"" Then Cells(r,"F").ClearContents
Next r
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,212
Members
452,618
Latest member
Tam84

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