简体   繁体   English

Excel VBA格式的缩写和日期

[英]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. 我正在尝试将一组缩写和DOB合并到一个列中,以为每个人创建一个唯一的标识符。 For example, Initials "JS" with DOB "01/01/2001" should output JS01012001. 例如,DOB为“ 01/01/2001”的缩写“ JS”应输出JS01012001。 The DOB column is originally formatted as mm/dd/yyyy style. DOB列最初的格式为mm / dd / yyyy。 Before I do the concatenation, I reformat it as "mmddyyyy". 在进行串联之前,我将其重新格式化为“ mmddyyyy”。 This appears correctly in the DOB column. 这在DOB列中正确显示。 However, when I run the macro I have (below) it outputs as "JS01/01/2001" in the ID column. 但是,当我运行宏时(在下面),它在ID列中输出为“ JS01 / 01/2001”。 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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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