date conversion

tinkythomas

Active Member
Joined
Dec 13, 2006
Messages
432
Hi,

I have a spreadsheet that has column (E) with shelf life. Would it be possible to convert this data into months? At the moment it is in the format years/yrs or months. What I need is just the number expressed in months.
Shelf life.xls
CDEF
1019EA2100V324-001742Issue32Years1500HRSFAN
10201200HRSOp
1021
1022EA2100V350-001689Issue218MonthsOnCondition.
1023SPEC689
1024
1025EA2100V215-001743Issue45Years1000HRSFAN
1026800F/HRS
1027
10283yrs.OnCondition
1029EA2100V349-00145453Issue3
1030
1031EA2100V338-00145453Issue35YearsOnCondition
1032
1033EA2100V371-00145453Issue35YearsOnCondition
1034
1035EA2100V368-00145453Issue35YearsOnCondition
1036
1037EA9300V022-001E43297-1Issue2NOTLESSTHAN5YRSOnCondition.
1038
1039EA2300V011-00146197Issue15Years
1040
1041EA2300V011-00246638Issue15Years
Sheet1


Any advice or ideas would be very welcome because there is a large amount of data to convert.

Thank-you
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
tinkythomas,

Your sample data:

Excel Workbook
CDEF
4EA2100V324-001742*****Issue*32*Years1500*HRS*FAN
51200*HRS*Op*
6
7EA2100V350-001689*****Issue*218*MonthsOn*Condition.
8SPEC*689
9
10EA2100V215-001743*****Issue*4*5*Years1000*HRS*FAN
11800***F/HRS
12
13EA2100V349-00145453*Issue*33*yrs.*On*Condition
14
15
16EA2100V338-00145453*Issue*35*YearsOn*Condition*
17
18EA2100V371-00145453*Issue*35*YearsOn*Condition
19
20EA2100V368-00145453*Issue*35*YearsOn*Condition
21
22EA9300V022-001E43297-1***Issue*2NOT*LESS*THAN*5YRSOn*Condition.
23
24EA2300V011-00146197*********Issue*15*Years
25
26EA2300V011-00246638*********Issue*15*Years
Sheet1



After the macro:

Excel Workbook
CDEF
4EA2100V324-001742*****Issue*3241500*HRS*FAN
51200*HRS*Op*
6
7EA2100V350-001689*****Issue*218On*Condition.
8SPEC*689
9
10EA2100V215-001743*****Issue*4*601000*HRS*FAN
11800***F/HRS
12
13EA2100V349-00145453*Issue*336On*Condition
14
15
16EA2100V338-00145453*Issue*360On*Condition*
17
18EA2100V371-00145453*Issue*360On*Condition
19
20EA2100V368-00145453*Issue*360On*Condition
21
22EA9300V022-001E43297-1***Issue*260On*Condition.
23
24EA2300V011-00146197*********Issue*160
25
26EA2300V011-00246638*********Issue*160
Sheet1




I have assumed that your data begins in row 1.

The macro utilizes column G for some interim calculations.

And, the macro utilizes Data, Filter, AutoFilter, and a Function ExtractNumber.


Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose)..



Press and hold down the 'ALT' key, and press the 'F11' key.

Insert a Module in your VBAProject, Microsoft Excel Objects

Copy the below code, and paste it into the Module1.

Code:
Option Explicit
Sub ConvertColumnEToMonths()

    Dim lngLastRow As Long
    Dim lngLoopCtr As Long
    Dim FilteredRange As Range
    Dim rng As Range
    
    Application.ScreenUpdating = False

    Worksheets("Sheet1").Select
    With Range("C1")
        .EntireRow.Insert
    End With

    Range("E1") = "Months"

    lngLastRow = Range("C" & Rows.Count).End(xlUp).Row

    With Worksheets("Sheet1")
        .AutoFilterMode = False
        With Range("E1:E" & lngLastRow)
            .AutoFilter Field:=1, Criteria1:="=*Years*", Operator:=xlOr, Criteria2:="=*yrs*"
        End With
    End With
    With Worksheets("Sheet1").AutoFilter.Range
        On Error Resume Next
        Set rng = .Offset(1, 0).Resize(.Rows.Count - 1, 1) _
            .SpecialCells(xlCellTypeVisible)
        On Error GoTo 0
    End With
    If rng Is Nothing Then
        'Do nothing
    Else
        Set rng = ActiveSheet.AutoFilter.Range
        rng.Offset(1, 2).Resize(rng.Rows.Count - 1).FormulaR1C1 = "=ExtractNumber(RC[-2])*12"
    End If

    With Worksheets("Sheet1")
        .AutoFilterMode = False
        With Range("E1:E" & lngLastRow)
            .AutoFilter Field:=1, Criteria1:="=*Months*", Operator:=xlAnd
        End With
    End With
    With Worksheets("Sheet1").AutoFilter.Range
        On Error Resume Next
        Set rng = .Offset(1, 0).Resize(.Rows.Count - 1, 1) _
            .SpecialCells(xlCellTypeVisible)
        On Error GoTo 0
    End With
    If rng Is Nothing Then
        'Do nothing
    Else
        Set rng = ActiveSheet.AutoFilter.Range
        rng.Offset(1, 2).Resize(rng.Rows.Count - 1).FormulaR1C1 = "=ExtractNumber(RC[-2])"
    End If
    
    Worksheets("Sheet1").AutoFilterMode = False
    
    With Columns("G:G")
        .Copy
        .PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks:=False, Transpose:=False
        Application.CutCopyMode = False
    End With
    
    For lngLoopCtr = 1 To lngLastRow Step 1
        If Cells(lngLoopCtr, "G") <> "" Then
            Range("G" & lngLoopCtr).Copy Range("E" & lngLoopCtr)
        End If
    Next lngLoopCtr
    
    Columns("G:G").Clear
    Rows("1:1").Delete Shift:=xlUp
    Range("A1").Select

    Application.ScreenUpdating = True

End Sub


Public Function ExtractNumber(rCell As Range, _
    Optional Take_decimal As Boolean, Optional Take_negative As Boolean) As Double
'
' http://www.ozgrid.com/VBA/ExtractNum.htm
'
'
    Dim iCount As Integer, i As Integer, iLoop As Integer
    Dim sText As String, strNeg As String, strDec As String
    Dim lNum As String
    Dim vVal, vVal2
     ''''''''''''''''''''''''''''''''''''''''''
     'Written by OzGrid Business Applications
     'www.ozgrid.com
     'Extracts a number from a cell containing text and numbers.
     ''''''''''''''''''''''''''''''''''''''''''
    sText = rCell
    If Take_decimal = True And Take_negative = True Then
        strNeg = "-" 'Negative Sign MUST be before 1st number.
        strDec = "."
    ElseIf Take_decimal = True And Take_negative = False Then
        strNeg = vbNullString
        strDec = "."
    ElseIf Take_decimal = False And Take_negative = True Then
        strNeg = "-"
        strDec = vbNullString
    End If
    iLoop = Len(sText)
            For iCount = iLoop To 1 Step -1
            vVal = Mid(sText, iCount, 1)
                If IsNumeric(vVal) Or vVal = strNeg Or vVal = strDec Then
                    i = i + 1
                    lNum = Mid(sText, iCount, 1) & lNum
                        If IsNumeric(lNum) Then
                            If CDbl(lNum) < 0 Then Exit For
                        Else
                          lNum = Replace(lNum, Left(lNum, 1), "", , 1)
                        End If
                End If
                If i = 1 And lNum <> vbNullString Then lNum = CDbl(Mid(lNum, 1, 1))
            Next iCount
    ExtractNumber = CDbl(lNum)
End Function


Then run the 'ConvertColumnEToMonths' macro.

Have a great day,
Stan
 
Last edited:
Upvote 0
stanleydgromjr, I can't thank-you enough!! This has saved me a lot of valuable time.

I can see you have put a lot of effort into this, thank-you so much.

Regards
 
Upvote 0

Forum statistics

Threads
1,221,614
Messages
6,160,839
Members
451,673
Latest member
wella86

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