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.
 
I'm using a button with this but for some reason it isn't using the formulas. it will run with no exceptions, but nothing happens. I copied and pasted your code. any suggestions?
 
Upvote 0

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Can you post your code, exactly as you have it now?
And then can you post an example of your data? You can do that using the tool mentioned here: XL2BB - Excel Range to BBCode
 
Upvote 0
Private Sub CommandButton1_Click()

Dim lr As Long
Dim sr As Long
Dim r As Long

Application.ScreenUpdating = False

' Find last row with data in column D
lr = Cells(Rows.Count, "D").End(xlUp).Row

' Find last populated cell in column E and start on row below that
sr = Cells(Rows.Count, "E").End(xlUp).Row + 1

' Populate columns E and F with formulas, from row 2 to last row
Range("E" & sr & ":E" & lr).FormulaR1C1 = "=IF(R[]C[-1]<>"""",R[]C[-1]+21,"""")"
Range("F" & sr & ":F" & lr).FormulaR1C1 = "=IF(R[]C[-1]<>"""",R[]C[-1]-TODAY(),"""")"
Columns("E:E").NumberFormat = "mm/dd/yy"

' Convert formulas to hard-coded values
Range("E" & sr & ":E" & lr).Value = Range("E" & sr & ":E" & lr).Value
Range("F" & sr & ":F" & lr).Value = Range("F" & sr & ":F" & lr).Value

Application.ScreenUpdating = True

' clear contents if comment is made


For r = sr To lr
If Cells(r, "G") <> "" Then Cells(r, "F").ClearContents

Next r



End Sub
 
Upvote 0
sorry i'm currently using my excel at work. it takes forever to get IT to approve add ins.
hope this will help
1592585239437.png
 
Upvote 0
It is because of this, I think, that we talked about in post 4 (its tough to tell because I cannot see all your data).
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.
So, the macro starts right after the last populated row in column D.
Did that condition change, regarding which rows this code should be affecting?
 
Upvote 0
so D is manually entered and based on the date entered in D all the calculations start there. So install date is entered the due date is the first formula where it adds 21 days. days left is based on the due date minus today.
I also checked to see if macros were disabled they look like they are good to go. if a comment is added that means the job was completed and we no longer want to see days left. so that was the last code you sent.
 
Upvote 0
Since you only sent a small excerpt your data, I cannot test it out for your situation.
If you can upload the file to a file sharing site and provided a link to it, I can download your file and test it out.
If you do that, just be sure to remove any sensitive information from the file.
 
Upvote 0
ok that is good. I will have to do that monday. Let me know where to send it.
 
Upvote 0
I will have to do that monday. Let me know where to send it.
Just upload it to a file sharing site like DropBox, or if you have a Google Drive account or something like that.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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