Solved

Formula to subtract one column from another

Posted on 2013-01-07
16
863 Views
Last Modified: 2013-01-15
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
0
Comment
Question by:swjtx99
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
  • 2
16 Comments
 
LVL 16

Expert Comment

by:terencino
ID: 38753475
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

0
 
LVL 16

Expert Comment

by:terencino
ID: 38753485
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
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 38753508
Sheets("Sheet1").Range("H2:H" & Range("A2").End(xlDown).Row).FormulaR1C1 = "=today()-rc[-1]"
0
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
LVL 16

Expert Comment

by:terencino
ID: 38753514
By the way did you want workdays ie number of days excluding weekends? Or just the total number of days
0
 

Author Comment

by:swjtx99
ID: 38753544
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
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 38753572
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())"
0
 
LVL 16

Expert Comment

by:terencino
ID: 38753578
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
0
 
LVL 43

Accepted Solution

by:
Saqib Husain, Syed earned 250 total points
ID: 38753729
Sorry missed the "blanks" part. You can still do it without looping

Sheets("Sheet1").Range("H2:H" & Range("A" & Rows.Count).End(xlUp).Row).FormulaR1C1 = "=networkdays(rc[-1],today())"
Sheets("Sheet1").Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row).SpecialCells(xlCellTypeBlanks).Offset(, 7).ClearContents
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 38753737
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())"
0
 
LVL 16

Assisted Solution

by:terencino
terencino earned 250 total points
ID: 38753852
Ok ssaqibh, no looping, one line, allows for formulas too, with same criteria for blank cells:

Union(ActiveSheet.UsedRange.Columns(1).SpecialCells(xlCellTypeFormulas), ActiveSheet.UsedRange.Columns(1).SpecialCells(xlCellTypeConstants)).Offset(0, 7).Formula = "=networkdays(rc[-1] ,TODAY())"

Any ideas to shorten?
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 38753933
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.
0
 
LVL 16

Expert Comment

by:terencino
ID: 38753973
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?
0
 
LVL 43

Expert Comment

by:Saqib Husain, Syed
ID: 38754002
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
0
 

Author Comment

by:swjtx99
ID: 38756094
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
0
 
LVL 16

Expert Comment

by:terencino
ID: 38758712
Hi swjtx99 you awarded all to one solution?
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question