OPTIMIZE code

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

OPTIMIZE code

Post by sal21 »

This code return the date in a Fiscal Code (i dont know if other country exists)...

Is possible to optimize?

Code: Select all

Option Explicit
Sub estrai()

    Dim i As Integer
    Dim a As String
    Dim tutto As String
    Dim giorno As String
    Dim mese As String
    Dim anno As String
    
        a = "MPRSVT57C27F839T"
        tutto = Mid(a, 7, 5)
        anno = Mid(tutto, 1, 2)
        mese = Mid(tutto, 3, 1)
        giorno = Mid(tutto, 4, 2)
        
        If mese = "a" Or mese = "A" Then
            mese = "01"
            ElseIf mese = "b" Or mese = "B" Then
            mese = "02"
            ElseIf mese = "c" Or mese = "C" Then
            mese = "03"
            ElseIf mese = "d" Or mese = "D" Then
            mese = "04"
            ElseIf mese = "e" Or mese = "E" Then
            mese = "05"
            ElseIf mese = "h" Or mese = "H" Then
            mese = "06"
            ElseIf mese = "l" Or mese = "L" Then
            mese = "07"
            ElseIf mese = "m" Or mese = "M" Then
            mese = "08"
            ElseIf mese = "p" Or mese = "P" Then
            mese = "09"
            ElseIf mese = "r" Or mese = "R" Then
            mese = "10"
            ElseIf mese = "s" Or mese = "S" Then
            mese = "11"
            ElseIf mese = "t" Or mese = "T" Then
            mese = "12"
        End If
        
        If giorno > 40 Then
            giorno = giorno - 40
        End If
        
        Debug.Print giorno & "/" & mese & "/" & anno
        
End Sub

User avatar
HansV
Administrator
Posts: 78531
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: OPTIMIZE code

Post by HansV »

I don't have the slightest idea what your fiscal code means, but here is a shorter version:

Code: Select all

Sub estrai()
    Dim i As Integer
    Dim a As String
    Dim tutto As String
    Dim giorno As String
    Dim mese As String
    Dim anno As String

    a = "MPRSVT57C27F839T"
    tutto = Mid(a, 7, 5)
    anno = Mid(tutto, 1, 2)
    mese = UCase(Mid(tutto, 3, 1))
    giorno = Mid(tutto, 4, 2)

    mese = Format(InStr("ABCDEHLMPRST", mese), "00")

    If giorno > 40 Then
        giorno = giorno - 40
    End If

    Debug.Print giorno & "/" & mese & "/" & anno
End Sub
Best wishes,
Hans