VBA help for newbie

DAD

Board Regular
Joined
Jan 8, 2010
Messages
54
Hi Guys and Gals,

I have a project I have been working on for quite some time, but unable to find a workable solution. Up until now, I have been trying to find a solution without the use of VBA, as I am a relative newbie to VBA, but doing lots of reading and "trial and error" coding to fix that. Unfortunately the non-VBA solution is too big and too unworkable, so I am now trying it using VBA solution. I was hoping some of you may generously help get me on the right track?

So I work for a small company, and I am trying to make our system of allocation holiday/vacation dates a little more streamlined. To this end, I have come up with an algorithm to allocate the leave, but just now need to be able to code that into excel so employees can see if their desired leave dates will be approved.

If you observe the pic below, I have drastically simplified the final solution so that it may make it easier for you to help me down the right path. You will see that the employees are Smith, Jones and Chang. They can request up to 3 periods of Holidays/vacation per year, and they allocate each of the periods a priority, 1 being the most desired, 3 being the least. If there is no clash with other requested dates, it is approved, and the column titled "Approved" beside the requested period will say YES. If there is a clash of dates (with any of the dates entered in Priority 1-3), then the person that has the higher priority dates entered (1-3) will have their leave approved (YES) and the lower priority will have NO in the Approved? column. Should the dates clash, and the priority be the same, then the person with the higher seniority (that is the earlier date in column L will be approved (YES), the lower seniority with NO. Should The dates clash, the priority the same, the seniority the same, then the final step will be to allocate the leave to the oldest person (column N).

So to summarise:

1. No clash of dates = Approved
2. Clash of dates = Approved to higher Priority
3. Same dates + same Priority = Approved to higher Seniority
4. Same dates + same Priority + same Seniority = Approved to older person.

I realise this is more complicated than most questions asked here, but I would really appreciate your help. While the final solution is a lot more complicated, if I can gain an understanding of how you experts solve a smaller problem like this, I am sure I will be able to figure out the larger solution.

Many thanks for your time.

1596v9.jpg
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Hi Brian,

Yes, unfortunately what started out as something I thought would be relatively simple, turns out it is not. But thanks you for putting your effort and brain power into it.

Pete
 
Last edited:
Upvote 0
I quickly found that my initial model was incorrect so I did actually have to "flowchart" Priority 1.
To go further I had to create some data of my own. In Col N I've set the outcome which Col E should be telling me.
Those columns are in agreement but I'm still not totally confident. I also know that my algorithm is not highly efficient.

Anyway, this is what happens:
I get two items of data (dates). I retain the first and test it against others in the list. When found to be invalid against your criteria it is so flagged and then the second item becomes the focus. It (when further down the list) does have to begin at the start of the list as something earlier might have created an exclusion and this needs to take that into account.

Now, if my process so far is correct then any personnel who have a "Yes" need to have an "N/AP" registered for Pr2 and Pr3.
In addressing Pr2 dates both the "N/AP" and Pr1 dates do need to be addressed as someone like Smith for instance, may have nominated an earlier date (eg, 15 Jan. Wills having that period reserved would create a rejection).

Then Pr3 also has to backtrack on Pr1 and Pr2 whilst looking at its own dates.

This is my code at this point:
Code:
Sub overlap()
Dim day1, day2, day3, day4 As Long
Dim sen1, sen2, age1, age2 As Long
Dim lastrow, a, b, flag As Long
lastrow = Cells(Rows.Count, "C").End(xlUp).Row
For a = 3 To lastrow
day1 = Range("C" & a).Value
day2 = Range("D" & a).Value
sen1 = Range("L" & a).Value
age1 = Range("M" & a).Value

Range("B16").Value = Range("B" & a).Value
Range("C16").Value = day1
Range("L16").Value = sen1
Range("M16").Value = age1
For b = 3 To lastrow
If a <> b Then
flag = 0
day3 = Range("C" & b).Value
sen2 = Range("L" & b).Value
age2 = Range("M" & b).Value
Range("B17").Value = Range("B" & b).Value
Range("C17").Value = day3
Range("L17").Value = sen2
Range("M17").Value = age2
If day1 < day3 And day3 < day2 Then

        
   If sen1 = sen2 Then
           If age1 < age2 Then
             flag = 1
                Exit For
            End If
           
           ElseIf sen1 > sen2 Then
                flag = 1
                Exit For
        End If
   
End If
End If

Next
If flag = 1 Then
Range("E" & a).Value = "No"
Else
Range("E" & a).Value = "Yes"
End If
Next

End Sub
This is my sheet after the run. Oh, I didn't mention that I added some code to show what data was being processed as I stepped through the code (Debug F8). That appears below the table.

Excel 2013/2016
ABCDEFGHIJKLMN
1PR1App?PR2App?PR3App?SenAge
2NameStEndStEndStEndReq'd
3Jones1-Jan-1810-Jan-18No3-Jun-1735N
4Smith13-May-1825-May-18Yes2-Jun-1730Y
5Chan2-Jan-183-Jan-18Yes1-Jun-1740Y
6Wills12-Jan-1823-Jan-18Yes4-Jun-1741Y
7Thom6-Feb-1813-Feb-18Yes5-Jun-1742Y
8Ford3-Mar-1817-Mar-18No7-Jun-1744N
9Simons8-Mar-1815-Mar-18Yes6-Jun-1743Y
10Dean9-Apr-1818-Apr-18Yes8-Jun-1745Y
11George15-Apr-1828-Apr-18Yes9-Jun-1746Y
12
13
14
15
16George15/04/20189/06/201746
17Dean9/04/20188/06/201745
18

<colgroup><col style="width: 25pxpx"><col><col><col><col><col><col><col><col><col><col><col><col><col><col></colgroup><thead>
</thead><tbody>
</tbody>
Sheet1
 
Last edited:
Upvote 0
Hi Brian,

Wow, thanks so much for that. I am going to have to go through this slowly to figure out and understand it. I'll get back to you once I think I have a good grasp on how you have done it. Once again, thanks again, you have no idea of how helpful this is.

Pete
 
Upvote 0
There is an error. I submitted a post but for some reason it didn't work.

If I reverse the Seniority of Ford and Simons they both get "Yes" but that should be "Yes" and then "No"!!!!
I thought that I had that error beaten but I cannot understand where I went wrong.

There is something in this block:
Code:
If day1 < day3 And day3 < day2 Then
  If sen1 = sen2 Then            
      If age1 < age2 Then
             flag = 1
                Exit For
            End If
           
           ElseIf sen1 > sen2 Then
                flag = 1
                Exit For
        End If   
End If
End If
which I failed to address.
 
Last edited:
Upvote 0
Hi Brian,

I have tried my best to understand the code. I think I do, but fear my understanding is wrong as I struggle to make sense of the logic. I think your code for variable lastrow looks up the last row of data (dates) in column c. You then create a loop from 3 (first row of dates) to lastrow (last row of dates). But what I am struggling to understand is the variables a and b (counters I assume). It appears to me that Day 1 should be the first day of leave for Priority one, day 2 the last day of leave for priority one. But, by my understanding, it appears as though day 3 is also the first day of leave for priority one. Obviously I am incorrect, as the logic "If day1 < day3 And day3 < day2 Then" would never work. But I can't seem to grasp why there is a difference between day 1 and day 3 (given that counter variables a and b seem the same).

Pete
 
Upvote 0
I realised later that I should not have located those values at C16 and C17 as when the code is run a second time, and those rows are not cleared, then lastrow counts to row 17 which I don't want.

The variables day1 and day3 represent the range of the leave for the first employee being tested. Day2 and day4 (which I haven't used) are the start and end leave dates for the second employee being compared. If day2 is between day1 and day3 then we have an obvious clash which has to be resolved. As to the variables a and b, when a is 1 then all values associated with b= 2 to lastrow are compared with a=1, then when a=2, all values for b=1, 3 to lastrow are compared with a=2.

As to priority, and we are only talking about Pr1 at the moment, day1 is always the first day of leave. Later when we consider Pr2 and Pr3 those same variables will be used again.

I should have annotated the code before I published it.
 
Upvote 0
Ah ok, that is where my mistake is. I will go back and figure out why b=2 when a=1. That would make sense
 
Upvote 0
In a for ... next loop a would usually begins at 1 (but our data begins at row 3). The next time through the loop a increases by 1.

Now that code has a a pair of "nested" For ...Next loops
For a= 3 to lastrow
for b= 3 to lastrow
next
next

When a=3, b loops until it gets to lastrow and then a jumps to 4 and b begins fro 3 to lastrow again; it's a bit like the odometer in your car, the units keeps repeating while the tens increases by 1.
 
Upvote 0

Forum statistics

Threads
1,214,649
Messages
6,120,733
Members
448,987
Latest member
marion_davis

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