Avatar of swjtx99
swjtx99
 asked on

Formula to subtract one column from another

Hi,

I need to put a formula into a blank column that subtracts the date in another column from Today and returns the number of workdays. This is a line in a VBA macro.

The catch is that it need to do it for every row where column A is not "blank/null".

To restate, every row where column A is not blank, subtract the date from column G and insert the number of workdays into column H.

I think it might start something like this?

 Sheets("Sheet1").Range("H2:H" & Range("A2").End(xlDown).Row).FormulaR1C1 = "=?"

I'm just not sure what should be where the =? is. I couldn't say "=Today()-G" without specifying a row# right?

Any help would be appreciated.

swjtx99
Microsoft ExcelVisual Basic.NET

Avatar of undefined
Last Comment
terencino

8/22/2022 - Mon
terencino

Hi try
Sub add_workdays_formula()
    Dim c As Range
    For Each c In ActiveSheet.UsedRange.Columns(1).Cells
        If c <> 0 Then c.Offset(0, 7).Formula = "=networkdays(g" & c.Row & ",TODAY())"
    Next c
End Sub

Open in new window

terencino

The NETWORKDAYS function can also include holidays if you have them in a list in your worksheet, do you want to do that?
Let me know if you need more help
...Terry
Saqib Husain

Sheets("Sheet1").Range("H2:H" & Range("A2").End(xlDown).Row).FormulaR1C1 = "=today()-rc[-1]"
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
terencino

By the way did you want workdays ie number of days excluding weekends? Or just the total number of days
swjtx99

ASKER
Hi,

Needed number of days excluding weekends.

Looks like some good possibilities posted but i'll have to wait till the morning to try them.

Thanks very much,

swjtx99
Saqib Husain

To exclude networkdays you can use networkdays as suggested by terencino.

The modified statement would be

Sheets("Sheet1").Range("H2:H" & Range("A2").End(xlDown).Row).FormulaR1C1 = "=networkdays(rc[-1],today())"
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
terencino

Note that this formula
Sheets("Sheet1").Range("H2:H" & Range("A2").End(xlDown).Row).FormulaR1C1
will put a formula in every cell.
The code I sent you will only put a formula where there is a value in column A
ASKER CERTIFIED SOLUTION
Saqib Husain

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Saqib Husain

You can also use this one-liner if the entries in column A are values and the blanks are blanks cells (Not "" entries)

Sheets("Sheet1").Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row).SpecialCells(xlCellTypeConstants).Offset(, 7).FormulaR1C1 = "=networkdays(rc[-1],today())"
SOLUTION
terencino

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
Saqib Husain

Maybe not but for such a case I personally would prefer the two-liner which I have already proposed. But the key constraint here is that the blanks cells MUST be really blank and not blank by formula.
Your help has saved me hundreds of hours of internet surfing.
fblack61
terencino

So you would need three lines then if column A included formulas? And if blanks in column A were not genuine blanks then you would need to loop through as I originally proposed to test column A cells against various criteria?
Saqib Husain

So you would need three lines then if column A included formulas?
No, just the two lines posted in ID: 38753729

And if blanks in column A were not genuine blanks then you would need to loop through as I originally proposed to test column A cells against various criteria?
True
swjtx99

ASKER
Both solutions awarded worked. The report I'm working with comes off a server so blanks are really blank but nice to have the answer if/when they're not!

Thanks for your help. I sincerely appreciate it.


swjtx99
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
terencino

Hi swjtx99 you awarded all to one solution?