VBA生成字符A B C D.

时间:2014-03-06 16:23:36

标签: excel vba excel-formula

我在Excel中填写了C列,按升序排序。 我想写在B列,这个号码是另一种格式

例如我就是我需要得到的但却不知道我该怎么做

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等..

谢谢

3 个答案:

答案 0 :(得分:1)

在B1中复制以下公式:

<强> = TEXT(C1, “0000”)及 “ - ” &安培; LEFT(ADDRESS(1,COUNTIF(C $ 1:C1,C1),4,1),1) < / p>

答案 1 :(得分:0)

试试这个:

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

enter image description here

此外,我在博客上撰写了一篇关于字符串处理的文章,它可能有所帮助Excel VBA String Processing and Manipulation

答案 2 :(得分:0)

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