我有Excel中的列C填充數字按升序排序。 我想在B列寫,這個數字爲另一種格式VBA生成字符A B C D
比如我,這就是我需要得到但沒有的我怎麼能做到這一點
ColumB ColumC
0001-A 1
0001-B 1
0002-A 2
0002-B 2
0002-C 2
線索有0001-
我用Format(Cells(c,3),"0000") & "-"
但我很難填充A B C D
等。每一行
謝謝
我有Excel中的列C填充數字按升序排序。 我想在B列寫,這個數字爲另一種格式VBA生成字符A B C D
比如我,這就是我需要得到但沒有的我怎麼能做到這一點
ColumB ColumC
0001-A 1
0001-B 1
0002-A 2
0002-B 2
0002-C 2
線索有0001-
我用Format(Cells(c,3),"0000") & "-"
但我很難填充A B C D
等。每一行
謝謝
Sub Macro3()
Dim cl As Range
myinteger = 64
currentinteger = 0
currentvalue = Range("C1").Value
For Each cl In Range("C1:C5")
If cl.Value <> currentvalue Then
currentinteger = 1
currentvalue = cl.Value
Else
currentinteger = currentinteger + 1
End If
cl.Offset(0, -1).Value = Format(cl.Value, "0000") & "-" & Chr(myinteger + currentinteger)
Next cl
End Sub
試試這個:
Sub main()
Dim i As Integer
Dim j As Integer
Dim flagSame As Boolean
For i = 1 To 5500
flagSame = True
j = 1
While flagSame = True
Cells(i + j - 1, 2) = "000" + Strings.Trim(Str(Cells(i + j - 1, 3))) + "-" + Strings.Trim(Chr(j + 64))
If Cells(i + j, 3) <> Cells(i + j - 1, 3) Then
flagSame = False
i = i + j - 1
Else
j = j + 1
End If
Wend
Next i
End Sub
而且,這裏是我寫我的博客上關於字符串處理的文章,它可能會幫助Excel VBA String Processing and Manipulation
我不喜歡硬編碼的解決方案,加上它輸入第4個字符爲'0',它不符合OP請求的內容。 – 2014-03-06 16:36:03
你是什麼意思的硬編碼?:) – Pedrumj
這看起來好...但我想遠遠超過2 ...我的專欄去到5500 ...唯一的方法來識別重複是字母ABCD etcc – sharkantipav
試試這個公式中的B1抄了下來:
= TEXT(C1,「0000」)&「 - 」& LEFT(ADDRESS(1,COUNTIF(C $ 1:C1,C1),4,1),1)
完美的天才 – sharkantipav