Consulting

Results 1 to 4 of 4

Thread: VBA Userform using Danish CPR Numbers

  1. #1

    Arrow VBA Userform using Danish CPR Numbers

    Hi

    I need somesupport to run simple calculation in a VBA user form, based on a Danish CPR-number.

    In Demarkeach person has a unique CPR number with 10 digits.

    The number couldlook like: 010270-1223

    010270 isthe date of birth: 01 is the day, 02 is the month, 70 is the year meaning 1970
    Is digit 7:0,1,2,3 is the person borne in 19xx
    Is digit 7:4 or 9 is the person born in 19xx if the last 2 digits in the 6 digit is >36 are they < = is the person born in 20xx.
    Is digit 7:5,6,7 or 8 and the last 2 digits in the 6 digit is = 36 are the person born in2000

    I wouldlike to run the two below formulas based on the input in textbox 1:
    Example:

    Input Textbox1= 231117-5704
    Problem 1: I would likeTtextbox2 to perform thecalculating with the result: 23-11-2017

    “=TEXT(LEFT($A2;6);"00-00-00")+0” (Extractof date of birth and year without he 4 last digit)

    Problem 2: I would likeTtextbox3 to perform thecalculating with the result:23-10-2020
    “DATE(YEAR(C2)+3;MONTH(C2)-1;DAY(C2))” (Date of birth+ 2y and 11 months.

    I hope someof you can support me with some magic.


  2. #2
    Moderator VBAX Sage SamT's Avatar
    Joined
    Oct 2006
    Location
    Near Columbia
    Posts
    7,814
    Location
    Since I can't tell which MS Application you are using, I moved this thread to the Excel Help Forum

    Needs Work, Put "231117-5704 " in Cell C2
    Option Explicit
    
    Sub t()
    Dim C2 As Range
    Set C2 = Range("C2")
    Dim D, M, Y, X
    D = Left(C2, 2) & "/"
    M = MonthName(Mid(C2, 3, 2), True) & "/"
    Y = Mid(C2, 5, 2)
    
    X = Format(DateAdd("m", 35, M & D & Y), "d,mmm,yyyy")
    End Sub
    Last edited by SamT; 02-16-2018 at 08:52 AM.
    I expect the student to do their homework and find all the errrors I leeve in.


    Please take the time to read the Forum FAQ

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,727
    Location
    This logic seems to work (used Excel to test)

    You'll need to integrate into your own application / user forms



    Option Explicit
    
    'DDMMYY-abcd
    'a = 0,1,2,3    then born in 19YY
    'a = 4,9        if YY < 36 then born in 20YY, if YY > 36 born in 19YY
    'a = 5,6,7      if YY = 36 then born in 2000
    
    Sub CPRtest()
        Dim s As String
        Dim DD As Long, MM As Long, YY As Long, A As Long
    
    'textbox1
        s = "231117-5704"
        DD = Mid(s, 1, 2)
        MM = Mid(s, 3, 2)
        YY = Mid(s, 5, 2)
        
        A = Mid(s, 8, 1)
        Select Case A
            Case 0, 1, 2, 3
                YY = 1900 + YY
            Case 4, 9
                If YY > 36 Then
                    YY = 1900 + YY
                Else
                    YY = 2000 + YY
                End If
            Case 5, 6, 7
                If YY = 36 Then
                    YY = 2000
                Else
                    YY = 2000 + YY  '   ?????????????
                End If
        End Select
    
    
    'textbox2
        MsgBox DateSerial(YY, MM, DD)
    
    
    'textbox3
        MsgBox DateSerial(YY + 2, MM + 11, DD)
    End Sub
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    Hi Guys

    Many Thanks, it works perfect.

    /AK

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •