Employee serial Number based on hiring date - vba

Please assist amending the code below. what I want to do is creating unique serial number for each employee based on their hiring date eg. on was hired on 1/13/2016 the serial number comes with the last two digital number of (year,month,day+00) that means (16011300) for the one who hired in the same day and (year,month,day+01) that means (16011301)for the second one who hired in the same day. and do the same thing for those hired on different days. see pic below first what the code do but the second what I want it to be. thanks in advance for assistance
the used code:
Dim myDate As Date, i As Long, dayPart As String
Application.EnableEvents = False
For i = 2 To Rows.Count
If Cells(i, 5).Value > 1 And Not IsEmpty(Cells(i, 5).Value) Then
myDate = Cells(i, 5)
dayPart = Format(Year(myDate), "00") - 2000 & _
Format(Month(myDate), "00") & _
Format(Day(myDate), "00") & 1
Cells(i, 2) = dayPart
End If
Next i
Application.EnableEvents = True

If your list stays sorted by Hiring Date you can just put the following formula in B2 and draw it down:
=IF(E2=E1,B1+1,VALUE(TEXT(E2,"YYMMDD")&"00"))
EDIT:
In case of unsorted lists use the following formula in B2 and draw it down:
=TEXT(E2,"YYMMDD")&TEXT(COUNTIF($E$1:E1,E2),"00")
or (if you want it as a number instead of text):
=VALUE(TEXT(E2,"YYMMDD")&TEXT(COUNTIF($E$1:E1,E2),"00"))

You cannot apply a format mask (see Number Format Codes) that included both date/time characters as well as a regular integer character, but if you convert the integer to the number of hours then your Emp no can be created in one shot.
Dim i As Long, seq As Long, dayPart As String
With Worksheets("employees")
For i = 2 To .Cells(Rows.Count, "E").End(xlUp).Row
seq = Application.CountIf(.Columns(5).Resize(i - 1, 1), .Cells(i, "E").Value)
dayPart = Format(.Cells(i, "E").Value2 + TimeSerial(seq, 0, 0), _
"yymmddhh")
.Cells(i, "B") = dayPart
Next i
End With
After collecting the number of previous matching dates with a COUNTIF function, the returned integer is converted to hours with TimeSerial and added to the hiring date. A format mask of yymmddhh generated the entire Emp no.
Granted, this is limited to 24 new employee hires per day.
        

Related

VBA: If statement to replace week number with text

In my workbook "isum", I have the week number figured out by a WEEKNUM formula (right now is week 27) listed on column X under the label Week#. The worksheet is called "Orders" with data to see what orders are late. I am struggling to create an if/then statement that makes it so that all of the week numbers on column X (starting at X2) that are < 27 (the current week number out of 52) are labeled as "Late". I am not sure how to change this value to the text, but the hard part is making sure that each week changes until it gets to 52. Otherwise nothing will change that is "Late". If this does not make sense let me know, but this is what I have so far:
isum.Sheets("Orders").Activate
Range("X2").Formula = "=WEEKNUM(RC[-9])"
Range("X2", "X" & Cells(Rows.Count, 1).End(xlUp).Row).FillDown
'Change statement to say "Late" and account for changing week numbers after every week
If cell.Value < 27 Then cell.Value = "Late"
Try looping through the range
Dim col As Range: Set col = Worksheets("Orders").Range("X2:X" & <current week num>)
Dim i As Integer
For i = 1 To col.Rows.Count
col.Cells(RowIndex:=i, ColumnIndex:="X").Value = "Late"
Next
(http://codevba.com/excel/for_each_cell_in_range.htm)
I would suggest a custom number format that displays Late for weeknums less than 27 but retains the underlying numerical weeknum value for use in future calculations. This can be applied through a conditional formatting rule that checks the weeknum formula's result against the current weeknum for dynamic results week to week.
With isum.workSheets("Orders")
With .Range(.Cells(2, "X"), .Cells(.Rows.Count, "O").End(xlUp).Offset(0, 9))
.Formula = "=weeknum(o2)"
.NumberFormat = "0_)"
.FormatConditions.Delete
With .FormatConditions.Add(Type:=xlExpression, Formula1:="=x2<weeknum(today())")
.NumberFormat = "L\at\e_)"
'optionally apply a red fill color
'.interior.color = vbred
End With
End With
End With

How to set reminders in excel by calculating dates in header columns

Good day!
I am using one of the tutorials: https://www.youtube.com/watch?v=x2_y0_KDaN0 and this is what I need with a few changes.
Here, The clients are listed in column A and the subsequent columns have dates and what interaction has been done with the client. If client xyz is in cell A2, cell b1 and onwards have dates of the months and cells b2, c2, d2 etc have either informed or pending entered. I want excel to set a reminder if the client contact is overdue beyond one week. The cells input chosen as "informed" indicates a contact that is made. I need to pick up the last date that the contact happened and calculate the dates. If the result is greater than or equal to 7, then a reminder should show. I would like to insert a column after the client name which the cell highlights and indicates a text as a reminder.
I have used the following code:
Sub dates()
End Sub
Dim x As Long
For x = 2 To informedendcolumn
Dim informedendcolumn As Integer
Dim mydate1 As Date
Dim mydate2 As Long
Dim date1 As Date
Dim date2 As Long
informedendcolumn = Range("2:2").Find(what:="informed", after:=Range("A1"), searchdirection:=xlPrevious).Column
mydate1 = Cells(informedendcolumn.ListObject.Range.Row, informedendcolumn.Column).Value
mydate2 = mydate1
date1 = Date
date2 = date1
If date2 - mydate2 = 7 Then
Cells(x, 2) = "Yes"
Cells(x, 2).Interior.ColorIndex = 3
Cells(x, 2).Font.ColorIndex = 2
Cells(x, 2).Font.Bold = True
End If
Next
End Sub
Cant really get where in going wrong. There are no errors but the code doesnt run.
Thanks in advance!

VBA To subtract today's date from a date in another column and return a number

I hope you can help. I have a small piece of code below. The issue I am having is that I am trying to subtract today's date from the date in Column C (see Pic 1) and then return a numerical result in Column D and then continue this formula down Column D until there is no values left in column C to subtract against.
So today's date is 09/03/2017 I want to subtract this date from the date in C2 03/07/2017 giving me 2 in D2 and then continue this through column D until C has a blank cell.
The piece of code that is bugging is Range("D2").Formula = DateDiff(C2, Date, "d")
The error I get is run time error 13 type mismatch.
The larger piece of code it belongs to is
Public Sub Activate_Sheet()
Worksheets("In Progress").Activate
Columns.AutoFit
Range("N:N").EntireColumn.Delete
Range("D1").Value = "# days open"
Range("D2").Formula = DateDiff(C2, Date, "d")
End Sub
As always any and all help is greatly appreciated.
Pic 1
Instead of
Range("D2").Formula = DateDiff(C2, Date, "d")
use
Range("D2").Formula = "=DAYS(TODAY(),C7)"
.Formula has to be a formula as you write it into a cell (for english Excel versions). If you have a non-english (localized) Excel version then you can use .FormulaLocal to write formulas in your localized language.
DateDiff function Parameters:
Interval in your case "d" (represnting days), is the first parameter, not the third.
You can't use C2 inside the DateDiff function, but you need to get the value from that cell by using Range("C2").Value.
Also, DateDiff will return a Numeric result in days, so you need to enter it in Range("D2").Value and not Formula.
Modify your code to:
Range("D2").Value = DateDiff("d", Range("C2").Value , Date)
Edit 1: To run this code for all occupied cells in Column C:
Dim LastRow As Long, i As Long
With Worksheets("In Progress")
LastRow = .Cells(.Rows.Count, "C").End(xlUp).Row
For i = 2 To LastRow
.Range("D" & i).Value = DateDiff("d", .Range("C" & i).Value, Date)
Next i
End With

Using VBA to check if a cell contains a date and if so advancing the date one month

I have a column of data (C) that has many cells that contain dates. I am trying to create a macro that checks to see if each cell contains a date and if it does then advance the date on month. I have the code to advance the date one month from here http://excel.tips.net/T002180_Automatically_Advancing_by_a_Month.html and it works fine but I am not sure how to replace the range with a dynamic range that evaluates all cells in column C. If possible I would also like to eliminate the need for a loop. Here is what I have so far.
Sub IncreaseMonth()
Dim dDate As Date
Dim NumberofTasks As Integer
NumberofTasks = ThisWorkbook.Worksheets("Dashboard").Range("Number_of_Tasks")
Dim x As Integer
For x = 1 To NumberofTasks
dDate = Range("C30").Value
Range("C30").Value = _
DateSerial(Year(dDate), _
Month(dDate) + 1, Day(dDate))
Next x
End Sub
Try something like the code below (I use DateAdd function to add 1 Month to the current date value)
Sub IncreaseMonth()
Dim dDate As Date
Dim NumberofTasks As Long
Dim x As Long
With Worksheets("Dashboard")
' I suspect you want to get the last row with data in Column C
NumberofTasks = .Cells(.Rows.Count, "C").End(xlUp).Row
For x = 1 To NumberofTasks
If IsDate(.Range("C" & x).Value) Then '<-- check if current cell at Column C is Date
.Range("C" & x).Value = DateAdd("m", 1, .Range("C" & x).Value) '<-- add 1 Month to current date in Column c, use DateAdd function
End If
Next x
End With
End Sub
This snippet should put you on the right track. I'm making a couple of assumptions here. The first is that you have a named range called "Number_of_Tasks" on which you wish to operate. Second is that all values in this range are a valid date. If values could be an invalid date (like a blank) you should check for this before setting the value.
You will also wish to ensure that the month does not become invalid. Incrementing the month past December will not be a valid date.
Sub IncreaseMonth()
Dim tempCell As Range
For Each tempCell In ThisWorkbook.Worksheets("Dashboard").Range("Number_of_Tasks")
tempCell.Value = DateSerial(Year(tempCell.value), Month(tempCell.value) + 1, Day(tempCell.value))
Next tempCell

How to copy paste data based on the number of days per month?

Hi and good day everyone,
Basically I wish to extract the data based on the number of days per month. Please find the link below for better illustration:
https://drive.google.com/open?id=0B15JqLrOZtewamlRQ1ZMTmJZNWc&authuser=0
For example, D1 shows the current month is September. Therefore I will only need to extract the data from day 1-30. Also I will need to copy the ID and paste it at left column of the date for 30 times.
Any idea?
Thanks for your time!
Cheers,
Patch
I'll get you started on the macro code necessary to determine the number of days within a month when the month is spelled out as text.
Dim dys As Long
With ActiveSheet
dys = Day(Application.EoMonth(DateValue(.Cells(1, 4).Value & " 1, " & Year(Date)), 0))
.Cells(2, 7).CurrentRegion.ClearContents
.Cells(2, 7) = "ID"
.Cells(2, 8).Resize(1, 4) = .Cells(2, 1).Resize(1, 4).Value
.Cells(3, 7).Resize(dys, 1) = .Cells(1, 2).Value
.Cells(3, 8).Resize(dys, 4) = .Cells(5, 1).Resize(dys, 4).Value
Debug.Print dys
End With
With the last day of the month determined, it is a small matter to use that to resize the source/target regions of cells. I tried to determine the best guess for the cell addresses from your sample but the merged areas may have fouled things up.