2015-05-29 36 views
0

早上好,Excel VBA格式的首字母縮寫和日期

我試圖將一組首字母和DOB組合成一列,爲每個人製作一個唯一的標識符。例如,DOB「01/01/2001」的首字母「JS」應輸出JS01012001。 DOB列最初格式爲mm/dd/yyyy樣式。在我進行連接之前,我將它重新格式化爲「mmddyyyy」。這在DOB列中正確顯示。但是,當我運行宏(下面)時,它在ID列中輸出爲「JS01/01/2001」。我試圖在格式化中想到的每種組合都可以正確輸出,但我仍然沒有得到它。任何幫助將非常感激!

在此先感謝!

' 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 

回答

0

您需要將日期對象轉換成VBA字符串使用:

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