简体   繁体   中英

Excel VBA Format Initials & Date

Good morning,

I'm trying to have a set of initials and DOB combined into one column to make a unique identifier for each person. For example, Initials "JS" with DOB "01/01/2001" should output JS01012001. The DOB column is originally formatted as mm/dd/yyyy style. Before I do the concatenation, I reformat it as "mmddyyyy". This appears correctly in the DOB column. However, when I run the macro I have (below) it outputs as "JS01/01/2001" in the ID column. I'm trying every combination I can think of in the formatting to get it to output correctly, but I'm still not getting it. Any help would be much appreciated!

Thanks in advance!

' Format DOB column (M)
    Columns("M:M").Select
    Selection.NumberFormat = "mmddyyyy"

' Concatenate Initials (L) and DOB (M) columns and output in ID column (N)
    For i = 2 To LastRow
        Cells(i, 14) = Cells(i, 12) & "" & Cells(i, 13)
    Next i

您需要使用以下命令将日期对象转换为VBA中的字符串:

Cells(i, 14) = Cells(i, 12) & Format(Cells(i, 13), "mmddyyyy")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM