macro help

ganeshpoojary05

Board Regular
Joined
Apr 26, 2011
Messages
105
hi everyone, i need a help to create a macro.

for example:
i have a leave tracker report where i have 400 employees. so i want to create a macro in which only 23 employees can take a leave on a day, and agent should not excced 21 leaves in a year.
1.if the leave count exceeds 23 than it should prompt with a message "Daily Leave quota exhausted" and it should not allow me enter more leaves.
2. if the leave count exceeds 21 in a year than it should prompt a message as "Agent has exhausted his leave balance"
3.if the above 2 conditions are ok than it should prompt a message as "Leave Validated".

Please note that the Leave calender is for 12 months (365 days).
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Please provide some cell references or a picture of your workbook so we have a better idea on what is going on. The more specific you can be the better.
 
Upvote 0
Like this?
Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim tColumn As Long, tRow As Long, dailyCount As Long, empCount As Long
Dim Msg As String
 
If Not Intersect(Target, Range("G4:NG403")) Is Nothing Then
    tColumn = Target.Column
    tRow = Target.Row
    dailyCount = Application.WorksheetFunction.Sum(Range(Cells(4, tColumn), Cells(403, tColumn)))
    empCount = Application.WorksheetFunction.Sum(Range(Cells(tRow, 7), Cells(tRow, 371)))
        If dailyCount > 23 Then
            Msg = MsgBox("Daily Leave quota exhausted", vbCritical, "Error")
        ElseIf empCount > 21 Then
            Msg = MsgBox("Agent has exhausted his leave balance", vbCritical, "Error")
        ElseIf dailyCount < 24 And empCount < 22 Then
            Msg = MsgBox("Leave Validated", vbOKOnly, "Success")
        End If
End If
 
End Sub

Put in Worksheet Change
 
Upvote 0
If you only want it controlled by the button then you can create a button and copy/paste all but the first and last lines of the code stnkynts posted.

Or if you want it controlled by button or by worksheet change, you can post the macro that stnkyts posted into a module with some name (ie Sub Leave). Then create a form button (not activeX) and assign it to that macro and also add
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Application.Run "Leave"
End Sub
to the worksheet
 
Upvote 0
Do you want to run the macro ONLY by the command button or do you want it to run automatically whenever a change is made in one of those cells as well as by a command button?
 
Upvote 0
hi,

i need to run the macro by command button, but when as per the above condition if the leaves for the day exceeds 23 then it should not allow me to add more leaves in the tracker. thanks!!!
 
Upvote 0
Ok, so it sounds like you need it in both.
So first, open one of the modules in VBA and copy/paste this code:
Code:
Option Explicit
Sub Leave()
Dim tColumn As Long, tRow As Long, dailyCount As Long, empCount As Long
Dim Msg As String
 
If Not Intersect(Target, Range("G4:NG403")) Is Nothing Then
    tColumn = Target.Column
    tRow = Target.Row
    dailyCount = Application.WorksheetFunction.Sum(Range(Cells(4, tColumn), Cells(403, tColumn)))
    empCount = Application.WorksheetFunction.Sum(Range(Cells(tRow, 7), Cells(tRow, 371)))
        If dailyCount > 23 Then
            Msg = MsgBox("Daily Leave quota exhausted", vbCritical, "Error")
        ElseIf empCount > 21 Then
            Msg = MsgBox("Agent has exhausted his leave balance", vbCritical, "Error")
        ElseIf dailyCount < 24 And empCount < 22 Then
            Msg = MsgBox("Leave Validated", vbOKOnly, "Success")
        End If
End If
 
End Sub

Then under the specific worksheet VBA page type this code:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
     Application.run "Leave"
End Sub

And finally, create a command button, double click on the button and enter
Code:
Application.run "Leave"
As the command button code.

That should do what you want. If not, then its beyond my limited scope and someone else may have to chime in.
 
Upvote 0

Forum statistics

Threads
1,224,583
Messages
6,179,678
Members
452,937
Latest member
Bhg1984

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