2014-03-06 107 views
-1

我有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等。每一行

谢谢

回答

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 
+0

完美的天才 – sharkantipav

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

+0

我不喜欢硬编码的解决方案,加上它输入第4个字符为'0',它不符合OP请求的内容。 – 2014-03-06 16:36:03

+0

你是什么意思的硬编码?:) – Pedrumj

+0

这看起来好...但我想远远超过2 ...我的专栏去到5500 ...唯一的方法来识别重复是字母ABCD etcc – sharkantipav

1

试试这个公式中的B1抄了下来:

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

相关问题