Excel VBA生成字符A B C D

Excel VBA生成字符A B C D,excel,vba,excel-formula,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-我使用格式(单元格(c,3),“0000”)和“-” 但是我很难填充abcd等等。。每行 谢谢试试这个: Sub main() Dim i As Integer Dim j

我用Excel中的C列填充了按升序排序的数字。 我想用另一种格式在B栏写下这个数字

例如,我知道这是我需要得到的,但我不知道如何才能做到

ColumB  ColumC    
0001-A     1
0001-B     1
0002-A     2
0002-B     2
0002-C     2
要获得
0001-
我使用
格式(单元格(c,3),“0000”)和“-”
但是我很难填充
abcd
等等。。每行

谢谢

试试这个:

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
Sub-main()
作为整数的Dim i
作为整数的Dim j
与布尔值相同
对于i=1至5500
相同=正确
j=1
而flagSame=True
单元格(i+j-1,2)=“000”+字符串.Trim(Str(单元格(i+j-1,3)))+“-”+字符串.Trim(Chr(j+64))
如果细胞(i+j,3)细胞(i+j-1,3),那么
相同=错误
i=i+j-1
其他的
j=j+1
如果结束
温德
接下来我
端接头

这里还有一篇我在博客上写的关于字符串处理的文章,它可能会有帮助 Dim cl As范围 myinteger=64 currentinteger=0 currentvalue=范围(“C1”).值 对于范围内的每个cl(“C1:C5”) 如果cl.值当前值,则 currentinteger=1 当前值=cl.值 其他的 currentinteger=currentinteger+1 如果结束 cl.Offset(0,-1).Value=格式(cl.Value,“0000”)&“-”和Chr(myinteger+currentinteger) 下一个cl 端接头
在B1中尝试以下公式:


=TEXT(C1,“0000”)&“-”和LEFT(地址(1,COUNTIF(C$1:C1,C1),4,1),1)

我不喜欢硬编码的解决方案,加上它输入的第四个字符是
0
,与OP请求的不匹配。硬编码是什么意思?:)看起来没问题。。。但我想去的远不止2个。。。我的专栏转到5500。。。识别重复的唯一方法是使用字母A B C D等。B C D的模式是什么?你能提供更多罗斯莱特说的数字15我有8次在列C。。。因此,在B列中,我需要
0015-A
在第一个
15
前面,然后
0015-B
在第二个
15
前面…直到
0015-H
在最后一个
15
前面,这是一个完美的天才
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