Solved

Add additional functionality to Macro

Posted on 2011-02-17
10
179 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:daiwhyte
  • 5
  • 3
  • 2
10 Comments
 
LVL 6

Expert Comment

by:Eric Zwiekhorst
ID: 34916366
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
0
 

Author Comment

by:daiwhyte
ID: 34916550
Eric, Ive hit a snag, Ive inserted your line and Ive getting a compile error, please see screenshot. Error
0
 
LVL 6

Accepted Solution

by:
Eric Zwiekhorst earned 250 total points
ID: 34916865
That would mean your excel version does not recognice today, try Now() instead..

kr

Eric
0
 

Author Comment

by:daiwhyte
ID: 34919347
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
0
 
LVL 4

Expert Comment

by:yuppydu
ID: 34924770
Can you attach the workbook?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 4

Assisted Solution

by:yuppydu
yuppydu earned 250 total points
ID: 34924794
First of all is DateDiff (2 f) and not datedif, and the syntax is: DateDiff(Interval, date1, date2)
Intervals are:
yyyy = year
q = quarter
m = month
d = day

It would be helpful to, as I said in previous post, to have the workbook
0
 

Author Comment

by:daiwhyte
ID: 34926870
experts.csv


Here is a sample workbook guys.
0
 
LVL 4

Expert Comment

by:yuppydu
ID: 34926941
this is a text file....
0
 

Author Comment

by:daiwhyte
ID: 34926982
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)

0
 

Author Closing Comment

by:daiwhyte
ID: 34951259
Thanks, it helped me but didnt totally resolve problem
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
The viewer will learn how to simulate a series of sales calls dependent on a single skill level and learn how to simulate a series of sales calls dependent on two skill levels. Simulating Independent Sales Calls: Enter .75 into cell C2 – “skill leve…
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now