How to use Macro in my case

Closed
Ann - Oct 28, 2011 at 01:13 AM
 Ann - Nov 4, 2011 at 12:56 AM
Hello,

Would appreciate if anyone could help me in detail how to use macros in the below case:
I have Expected closure date Column A1, and escalated days Column B1- which needs to be find out.
Ex: Column A includes- 22-Oct-11, 21-Oct-11, 24-Oct-11, 27-Oct-11, 28-Oct-11,31-Oct-11
In Column B Answer is coming as on 28th Oct 2011, B1= (-6), B2= (-7), B3=(-4), B4=(-1), B5=(0) & B6=(3) . Because I have applied formula as =(A1-today()).

However the purpose of this sheet is to identify on daily basis that how many days are in hand to work on particular case as the case needs to get closed by the mentioned date on expected closure date column or how many days passed without actioning on particular case. The formula which i have used in B column is not helping me to find out the correct escalated days.

Because, 23rd Oct 2011 is Sunday and 26th Oct 2011 is Public Holiday, as these two days will not be working hence these days should be automatically excluded and the correct answer should come in B1 as (-4) instead of (-6). Likewise correct Answers should be as mentioned below:

B1(-4), B2= (-5), B3=(-3), B4=(-1), B5=(0), B6(2).

Need an early closure to this case as i need to start this sheet ASAP. Pls Help at the earliest.

Regards,

Ann.



16 responses

venkat1926 Posts 1863 Registration date Sunday June 14, 2009 Status Contributor Last seen August 7, 2021 811
Nov 1, 2011 at 05:23 AM
It is not B1 it is B2 at least in my sheet
now see the data in columns C and d
column C gives the number of days upto today
(the formula in B2
=today()-A2
which is copied down
column D contains holidays as per G1 to G12

date wkdays today-A holidays working days
10/24/2011 -5 8 26,27,30 25,28,29,31,1
10/27/2011 -3 5 27,30 start date is 28(27hday) so 29,31,1
10/28/2011 -3 4 30 29,31,1
10/31/2011 -1 1 nil
11/4/2011 3 -3 nil

the workdays has to be defined by you . here it takes into account as interval
example from yesterday today it is one day (not 2 days). if one of these (yesterday or/and today) are holidays it is zero days.

see the table above table
say the row no. 2 . the elalpsed days from start date is 8 taking into account both holidays and non holidays. there are three holidays in column D (3). so it is 8 minus 3=5 how do you get 6. so your definition is different from normal definition. If you WANT to take include the start date in all case then add(or sutract) 1

some points I am intrigued
1. how can start date be on a holiday. if the start date is holiday normally the next working day is taken as start date
2. what is the logic and meaning of elapsed days for a future date(11/4/11)
3. elapsed days from start date to today is only positive but according to your logic it is negative.

if you have a different definition you adjust column B
if it is completely logical equally for all the days you can put a condition.

I hope above table is self explanatory.

Still I did not catch your negatives and positives.
elapsed days is always from the start date to today and that will be positive.


Of course your logic depends upon your problem and need not agree with my logic.

I suggest you sit down for half an hour and think about your problem again and make a decision and modify the macro according to you.
1
Dear Venkat,

I will explain you that on daily basis I am receiving 150 request all are having different TAT - The turnaround Time, to complete each requests. Hence one column will be expected closure date.
Two scenarios for expected closure date:
1) one if request received on 10/18/2011 and the TAT is for 5 working days , 23rd being sunday the expected closure day will come on 10/24/2011
2) If request received on 10/28/2011 and the TAT is for 6 working days, 30 being sunday the expected closure day will come on 11/4/2011.

Now according to 1st scenerio, as on today (i.e 1st NOv 2011) escalated days should come as "-5" days. (counted- 25,28,29,31,1= 5 days)Because i was supposed to complete the particular request by 10/24/2011though i could not complete the request/case it got escalated, but as on today the request has escalated by 5 days hence it should come in negative.

According to second scenerio, the case has to complete by 11/4/2011, which means i have 3 days(counted 2,3,4 = 3 days) in my hand to complete this particular request, hence it should come in Positive.

I hope this clarifies your doubt.

And now as per my case i am getting the correct answers for Expected closure date however I am stuck to get the correct escalated days. as per your solution =networkdays(today(),expected closure date,"holiday list").

Somewhere answers are coming wrong which i showed you once again with example :
the networkdays formula if applied to 1st scenario correct answer is coming as "-5", which exactly i want. counting - 25,28,29,31,1 = 5 days (counting from next day that is 25 and including today's day also that 1st Nov). * Consider the same holiday List*

In second scenario when i applied networdays formula,incorrect answer is coming as "4"(counting - 1,2,3 &4), however correct answer shuld be "3". It should count - 2,3 &4 = 3 days. However it is not counting from next day.

I hope the full case is cleared to you now.

Its an humble request to you that please provide me the correct solution as now i have only one day left in my hand. as i cannot trace my request manually as data is huge, hence asking for some shorcuts/ formula in EXCEL.

Thanks in anticipation.
Regards,
Ann
0
venkat1926 Posts 1863 Registration date Sunday June 14, 2009 Status Contributor Last seen August 7, 2021 811
Nov 1, 2011 at 01:19 PM
ARE WE NOT LOOKING AT CROSS PURPOSES
MY VALUES IN COLUMN B WAS calculated based on the macro



so every day you have to AGAIN RUN the macro test.

when I ran the macro on 1 nov 2011 the data and result (col A and B are

date "number of
working days"
10/24/2011 -5
10/27/2011 -3
10/28/2011 -3
10/31/2011 -1
11/4/2011 3
is this not correct particular with respect to 11/4/11


formula networkdays(startdate,enddate,holidays) gives problem I think somehow you have to use the macro I have given
I have already told you how to use the macro.
1
venkat1926 Posts 1863 Registration date Sunday June 14, 2009 Status Contributor Last seen August 7, 2021 811
Oct 28, 2011 at 07:04 AM
enter dates as excel dates for e.g
10/24/11
10/28/11
etc
you can format it as you like

see the configuration of dates(following above rules)

10/26/2011(this is entered in C1)
the following entered in rows 3 and down in columns A and B
22-Oct-11 10/28/2011 (these two are in A3 and B3)
20-Oct-11 10/28/2011
24-Oct-11 10/28/2011
27-Oct-11 10/28/2011
28-Oct-11 10/28/2011
31-Oct-11 10/28/2011

now in C3 enter this formula
=NETWORKDAYS(A3,B3,$C$1)
note dollar signs for C1
copy C3 down.
0
Thanks Venkat as u have given your valuable time to solve my problem. However i didn't understand why u have taken 28th Oct 2011 in B column. Also would like to inform you that my purpose of this sheet is that every day when i will open this sheet the escalated days column should automatically changed considering to the sunday and other public holidays.
For eg: If i have three more days in hand to close a particular case/request then when i will open the same next day it should show 2 working days left in my hand.

I need an urgent help to solve this issue. I would be helpful if you could help me out for this.

Thanks & Regards,
Ann
0
venkat1926 Posts 1863 Registration date Sunday June 14, 2009 Status Contributor Last seen August 7, 2021 811
Oct 29, 2011 at 03:29 AM
in b column the ACTUAL entry is
=today()
so that today"s date will be automatically entered in B column and the elapsed days will be available.

if you do like this automatically the actual elapsed days will be shown in column C
0
Hi Venkat, Sorry to bother you but in some cases incorrcet days are coming.
Like in A3- 10/22/2011, I applied formula in B column as (Holidays list is in Sheet 2) =NETWORKDAYS(TODAY(),A3,Holidays!$A$3:$A$13).
However as on today that is 29th Oct 2011, result is coming (-4), where two holidays are coming 23rd Oct -Sunday & 26th oct public Holiday. The answer should come (-5). Would appreciate if you could help me little more as we are near to correct answer.
I am not able to understand why the correct answer is not coming.
Kindly help me i would be very very grateful to you.
0

Didn't find the answer you are looking for?

Ask a question
venkat1926 Posts 1863 Registration date Sunday June 14, 2009 Status Contributor Last seen August 7, 2021 811
Oct 29, 2011 at 06:40 AM
see help under neworkdays

Returns the number of whole working days between start_date and end_date. Working days exclude weekends and any dates identified in holidays. Use NETWORKDAYS to calculate employee benefits that accrue based on the number of days worked during a specific term.

weekends mean Saturdays also holidays.

today 29th
start date 22nd
omitted 22(sat),23(sun),26(holiday),29(sat)
workdays 24,25,27,28 (start days not taken) as this is interval.
0
I understood thanks for your help however for the below dates either it is including today's date also that is 31-oct -2011(end date ) or the start dates.
10/24/2011 -5
10/27/2011 -3
10/28/2011 -2
10/31/2011 1
11/4/2011 5

And for 10/22/2011 -5, (counted 24,25,27,28,29)
10/20/2011 -7(Counted 21,22,24,25,27,28,&29).
Kindly help me that why the same funda is not applied to the above Six dates. Why it is difffering.
Would be very helpful to me if this is resolved at the earliest as my work is getting pending.
Thanks & Regards,
Ann
0
venkat1926 Posts 1863 Registration date Sunday June 14, 2009 Status Contributor Last seen August 7, 2021 811
Oct 31, 2011 at 02:23 AM
normally for an intersval start date is left and only today's date is taken.

2. what is your holiday's list. is there a saturday or sunday in that list.
0
In my holiday list all public holidays and sundays i have added. Saturdays we are working hence it should not count saturdays as holiday.... We have 6 working days in a week.
Holiday list for this FY post 20th Oct:
10/23/2011
10/26/2011
10/27/2011
10/30/2011
11/6/2011
11/7/2011
11/10/2011
11/13/2011
11/20/2011
11/27/2011
12/6/2011
12/25/2011

Would be very grateful if this is resolved today itself as my work is getting pending.
Thanks in advance
Regards
sebi
0
venkat1926 Posts 1863 Registration date Sunday June 14, 2009 Status Contributor Last seen August 7, 2021 811
Oct 31, 2011 at 06:03 AM
do this experiment
enter data as follows in column A from A1

date
10/24/2011
10/27/2011
10/28/2011
10/31/2011
11/4/2011

In B1 enter number of working days

holidays list enter from G1 to G12

no run this macro and test

Sub test()
Dim r As Range, c As Range, interval As Long, j As Long, holidays As Range, choliday As Range
Set r = Range(Range("A2"), Range("a2").End(xlDown))
For Each c In r
interval = CDate(Date) - CDate(c)
'MsgBox interval
j = 0
Set holidays = Range("G1:G12")
For Each choliday In holidays
choliday.Select
If CDate(choliday) >= WorksheetFunction.Min(CDate(Date), CDate(c)) _
And CDate(choliday) <= WorksheetFunction.Max(CDate(Date), CDate(c)) Then j = j + 1
'MsgBox j
Next choliday
'MsgBox j
interval = interval - j
c.Offset(0, 1) = interval
c.Offset(0, 1).NumberFormat = "0"
j = 0
Next c
End Sub


the result is in B2 to B6

for retesting clear B2 to B6 and again run the macro

give feedback please
0
venkat1926 Posts 1863 Registration date Sunday June 14, 2009 Status Contributor Last seen August 7, 2021 811
Oct 31, 2011 at 06:08 AM
I am giving you the colums A and B after running macro

date number of working days
10/24/2011 4
10/27/2011 2
10/28/2011 2
10/31/2011 0
11/4/2011 -4

sorry for a few spelling mistakes as I was hasty in forwarding the solution immediately as today is your target date.
0
I am very thank ful to you but sorry to say i do not how to use macro. You have mentioned some programming, but i do not know where to put that and how to start. pls guide me ASAP. Also in B column why for date 10/28/2011 & 10/27/2011 result is coming same as "2". I feel for 27th answer should come "-3", and for 28th answer should come as "-2". all negative should come in positive and all positive should come in negative.
Pls guide me on urgent basis. Would be very helpful to me.
Thanks 7 Regards,

Sebi
0
venkat1926 Posts 1863 Registration date Sunday June 14, 2009 Status Contributor Last seen August 7, 2021 811
Oct 31, 2011 at 07:56 AM
not clear what is your version of excel 2003(and earlier) or 2007
I assume it is 2003 or earlier

open a new excel file
select sheet1
enter the data as I have told you( I am repeating her for easy reference)
quote
enter data as follows in column A from A1

date
10/24/2011
10/27/2011
10/28/2011
10/31/2011
11/4/2011

In B1 enter number of working days

holidays list enter from G1 to G12
unquote

open vb editor (click alt+F11)
click control+R
you will see all the open excel files
brig the cursor to the new file(it may look like Book1 or Book 2 etc)
now go to vb editor and click insert(menu) -module in the sub menu
a module page opens (perhaps module 1 and the file name )
there copy paste the macro
save the file with some name in some folder
now go to sheet1
click tools(Menu)-macro-macros
in the macro window go down to "macros in"
click the arrow at the right end and choose "this workbook"
in the top part you will see the "test"
highlight "test" and click Run on the right side

you will see in column B the result
save the file

the question of posties as negatives and vice versa is only semantic. still if you are particular I have modified the macro slightly

Sub test()
Dim r As Range, c As Range, interval As Long, j As Long, holidays As Range, choliday As Range
Set r = Range(Range("A2"), Range("a2").End(xlDown))
For Each c In r
interval = CDate(Date) - CDate(c)
'MsgBox interval
j = 0
Set holidays = Range("G1:G12")
For Each choliday In holidays
choliday.Select
If CDate(choliday) >= WorksheetFunction.Min(CDate(Date), CDate(c)) _
And CDate(choliday) <= WorksheetFunction.Max(CDate(Date), CDate(c)) Then j = j + 1
'MsgBox j
Next choliday
'MsgBox j
interval = interval - j
c.Offset(0, 1) = -interval
c.Offset(0, 1).NumberFormat = "0"
j = 0
Next c
End Sub


It is this macro which you have to park not the old one

you yourself said in the first message
"how to use macros in the below case:"
I thought you know about the macros and you want a macro

is this clear. please give me feedback
0
venkat1926 Posts 1863 Registration date Sunday June 14, 2009 Status Contributor Last seen August 7, 2021 811
Nov 1, 2011 at 02:30 AM
did you receive my last message how to run a macro in excel???????
0
Hi Venkat,

I really appreciate your efforts but please explain me :
1) In B1 i inserted "-6" - My logic behind that is since 24 till 1st Nov 6 working days are coming in between (Inclusing Nov 1st). if my logic is incorrect then what do you mean by working days/ or to calculate the same.
2) why same figures are reflecting for 27th & 28th Oct 2011 that is "-3".
I feel for 28th Oct answer should come as "-3" however for 27th Oct answer should be "-4" (as on Today i.e 1st Nov 2011).

I have taken extention of two more days to complete this task. Would be very grateful if you could provide me the correct solution.

Thanks & Regards,

Ann.
0
venkat1926 Posts 1863 Registration date Sunday June 14, 2009 Status Contributor Last seen August 7, 2021 811
Nov 1, 2011 at 01:40 PM
There are lot of discussions on networkingdays formula see
http://dailydoseofexcel.com/archives/2004/07/19/networkdays/
see particualarly the comments. The formula gives problem if the end date month is not the as the month of end date (going to the next month).
Besides networkdays formula is already taking Saturday and Sundays as holidays. the third argument (in your case g1 TO g12 should contain only holidays other than Sat/sun.

particularly one of the comments is

"The real issue is the function does not do what the function definition claims and apparently has been that way since day one. No mention is made on the knowledge base of the problem. It will be interesting to see if I get a response from Microsoft."

remember these comments were far back in 2004

so better use macro than this formula (or function)
0
Hi Venkat,

Thanks a ton, Venkat, for your outstanding timely support. What i came to a conclusion that with the help of Networkdays formula, am getting the correct escalated days. However i noticed that if any holiday is coming just next to the expected closure day that time it is giving one day extra in the escalated column. so with the help of the link which you have provided me, i understood that i can use +1 in the same formula. But these things i need to do some manual work.

Further am using Excel 2007.

With regards to usage of macros, am not clear on that part. As you said i need to run that on daily basis. And do i need to run the below mentioned macro:

Sub test()
Dim r As Range, c As Range, interval As Long, j As Long, holidays As Range, choliday As Range
Set r = Range(Range("A2"), Range("a2").End(xlDown))
For Each c In r
interval = CDate(Date) - CDate(c)
'MsgBox interval
j = 0
Set holidays = Range("G1:G12")
For Each choliday In holidays
choliday.Select
If CDate(choliday) >= WorksheetFunction.Min(CDate(Date), CDate(c)) _
And CDate(choliday) <= WorksheetFunction.Max(CDate(Date), CDate(c)) Then j = j + 1
'MsgBox j
Next choliday
'MsgBox j
interval = interval - j
c.Offset(0, 1) = -interval
c.Offset(0, 1).NumberFormat = "0"
j = 0
Next c
End Sub



On daily basis if i need to run the macro then if you could help me with the detailed steps which i need to follow in Excel 2007, i would be very grateful to you.

Thanks & Regards,

Ann
0
Already i have one saved Macro enable Excel file where i have my base data. And i will have to find the escalated days in that same file. Hence would request you to kindly provide me the detailed step to run the macro on daily basis on the same file.

Please ref my previous comment also.

Thanks & Regards,

Ann
0
venkat1926 Posts 1863 Registration date Sunday June 14, 2009 Status Contributor Last seen August 7, 2021 811
Nov 2, 2011 at 05:39 AM
I have uploaded the file named "ann.xlsm"
you can download this file from this webpage

http://hotfile.com/dl/134006529/809ee4f/ann.xlsm.html

download and save the file
in the file you can add if you want some more dates in column A below the existing dates without leaving any row in column A blank.

This is a file in excel 2007 that too macro enabled file(the m at the end of xlsm is that - if the file does not have macro or not enable macro then the file name is .xlsx.

sometimes there may be a pop up message at the top to enable or disable macros.there will be a "options" button and if you click the two choices will come up. click "enable macros"

now I have already parked the macro in the module. If you want to see click Developer ribbon and click "macros" on extreme left side below the main menu.
at the bottom "macros in" you click the arrow and choose "this workbook"
you will see the names of the macro(s) in this file
highlight the name of the macro, in this case "test". you can click Run on the right.
macro test will be run.
But see below this is not necessary.

Now I have already made a provision that whenever you open the file the macro test will automatically run. You need not do anything. you want how it is done the steps are

click Developer ribbon
click vb editor on the extreme left below the ribbon
vb editor opens
click control+R
the list of the open excel files names are seen
bring the cursor to this file(in this case "ann.xlsm") and right click the item under this file name "this workbook". right click "this workbook" and click view code
you will see the following


Private Sub Workbook_Open()
test
End Sub


This means that whenever you open this file the macro "test" in the module will automatically run.
so you need not do anything. when you open the file in the morning the macro will be automatically run. you can save the file. even if you close the file in the same day and open the macro will be run but because today is the same day there will be no change in the result(in this case column B).
only when you open next day the result will change .

I hope I made myself clear. You are at liberty to ask any doubt or question.
I wish there is some provision to know the email addresses of both of us but I realize that this is not allowed by the forum due to some valid reasons.
0
venkat1926 Posts 1863 Registration date Sunday June 14, 2009 Status Contributor Last seen August 7, 2021 811
Nov 2, 2011 at 05:47 AM
when you click the webpage for downloading choose regular download (this is free).
the other one is paid.
0
Thanks a ton. I highly appreciate the way you help me out. I feel i will go with networkdays formula itself as am not used to with Macros fundamentals.

Once again thanks, Thanks a ton.
Bye Take care

Regards,

Ann
0
venkat1926 Posts 1863 Registration date Sunday June 14, 2009 Status Contributor Last seen August 7, 2021 811
Nov 3, 2011 at 07:57 AM
in whichever solution you are comfortable with is the best solution for you.

But if you are going to do lot with excel it is better to learn about macros. there are tutorials in the web and also good books. get them for the version you are using.

best compliments and bye
0
Thanks for your advice. Nice to talk to you.

Bye n take care
0