Link to home
Start Free TrialLog in
Avatar of daiwhyte
daiwhyteFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Add additional functionality to Macro

Hi,

I have a macro (created on this site!) which transforms data held in the P Column, I would like to add another transformation to the data in Column T. In this column, we currently have start date of contract (dd/mm/yyyy format), I would like to transform this data to how long contract has been live. I already have a formula to work this out but need to add it to the macro.

=DATEDIF(T2,TODAY(),"y")&"/"&DATEDIF(T2,TODAY(),"ym")
Works out duration of contract.

Here is the existing macro

Sub Button1_Click()
Dim sName As String, blankrow As Integer, rownum As Integer
Dim rng As Range, cell As Range
sName = ActiveSheet.Name
blankrow = 0
rownum = 1
If sName = "" Then Exit Sub

'Set rng = Range(Range("P1"), Cells(Rows.Count, 1).End(xlDown))
While blankrow < 10
Set cell = Range(("P" & rownum))
 If cell.Text = "Full Time Employment" Then
    cell.Value2 = "Employed FT"
 ElseIf cell.Text = "Part Time Employment" Then
    cell.Value2 = "Employed PT"
 ElseIf cell.Text = "Temporary or Contract" Then
    cell.Value2 = "Self Employed"
 ElseIf cell.Text = "" Then
    blankrow = blankrow + 1
 End If
 rownum = rownum + 1
Wend

End Sub


Thanks
Avatar of Eric Zwiekhorst
Eric Zwiekhorst
Flag of Netherlands image

Dear daiwhite, try this
 cell(rownum, 20) = datedif(cell(rownum, 20), today(), "y") & "/" & datedif(cell(rownum, 20), today(), "ym")

column 20 is T
so in this line the cell at column T is replaced with the duration.. If you would like duration in a other column change the cell(rownum, 20) = to the wanted column number


kind regards

Eric
Avatar of daiwhyte

ASKER

Eric, Ive hit a snag, Ive inserted your line and Ive getting a compile error, please see screenshot. User generated image
ASKER CERTIFIED SOLUTION
Avatar of Eric Zwiekhorst
Eric Zwiekhorst
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Eric, kk, inserted this line

cell(rownum, 20) = datedif(cell(rownum, 20), Now(), "y") & "/" & datedif(cell(rownum, 20), Now(), "ym")

It now says datedif is not defined as a sub or function and it highlights datedif

Here is the latest piece of code.


Dim sName As String, blankrow As Integer, rownum As Integer
Dim rng As Range, cell As Range
sName = ActiveSheet.Name
blankrow = 0
rownum = 1
If sName = "" Then Exit Sub

'Set rng = Range(Range("P1"), Cells(Rows.Count, 1).End(xlDown))
While blankrow < 10
Set cell = Range(("P" & rownum))
 If cell.Text = "Full Time Employment" Then
    cell.Value2 = "Employed FT"
 ElseIf cell.Text = "Part Time Employment" Then
    cell.Value2 = "Employed PT"
 ElseIf cell.Text = "Temporary or Contract" Then
    cell.Value2 = "Self Employed"
 ElseIf cell.Text = "" Then
    blankrow = blankrow + 1
cell(rownum, 20) = datedif(cell(rownum, 20), Now(), "y") & "/" & datedif(cell(rownum, 20), Now(), "ym")
 End If
 rownum = rownum + 1
Wend

End Sub
Can you attach the workbook?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
experts.csv


Here is a sample workbook guys.
this is a text file....
Yes, have the macro in a spreadsheet which is open in the background. When Im viewing the csv file, I press alt+f8 for the vb editor and I run the macro from there. The first part of the macro works on the Employment status its just the second part where it works out time in job (column T)

Thanks, it helped me but didnt totally resolve problem