将符号放在Excel中的一个string之间

我有一串string如下。 我怎么能把符号'<'放在字符之间?

'ABCDE' 'BCG' 'ABCD' 

预期产出应该是:

  A<B<C<D<E B<C<G A<B<C<D 

=concatenate(left(A1,1),"<",mid(A1,2,1),"<",mid(A1,3,1),(if(len(A1)>3,"<"&mid(A1,4,1)&if(len(A1)>4,"<"&mid(A1,5,1),""),"")))

将做你想要的值最多5个字母,只有3个字母。 否则,你可以改变它。

基本上,前3个字母之间加了一个“<”,然后检查string是否长于3个字母,如果是,则添加更多的“<”字符。 如果这需要更多的dynamic,在vba中更容易。

一个手动的,一次性的,无VBA的方法是:

  • 使用“固定宽度”的“文本到列”工具,并将标记放置在每个字符之后。
  • 然后使用这样的公式追加值和分隔符

如果您的值在第1行,公式可能看起来像这样

 =A1&IF(LEN(B1)>0,">"&B1,"")&IF(LEN(C1)>0,">"&C1,"")&IF(LEN(D1)>0,">"&D1,"")&IF(LEN(E1)>0,">"&E1,"") 

调整公式以适应单元格中的最大字符数。

这样的事情不是为了…

当您将问题标记为Excel-VBA时,也是如此:

 ''''''' Private Sub sb_Test_fp_AddSym() Debug.Print fp_AddSym("abncd", "<") End Sub Public Function fp_AddSym(pStr$, pSym$) As String Dim i&, j&, iLB&, iUBs&, iUBt& Dim tSrc() As Byte, tTgt() As Byte, tSym As Byte tSrc = pStr tSym = Asc(pSym) iLB = LBound(tSrc) iUBs = UBound(tSrc) iUBt = iUBs * 2 + 3 ReDim tTgt(iLB To iUBt) For i = iLB To iUBs Step 2 j = i * 2 tTgt(j) = tSrc(i) tTgt(j + 1) = tSrc(i + 1) tTgt(j + 2) = tSym tTgt(j + 3) = 0 Next ReDim Preserve tTgt(iLB To (iUBt - 4)) Debug.Print tTgt Stop fp_AddSym = tTgt End Function ''' 

这对我工作:

 Sub SymbolInsert() Dim cl As Range, temp As String For Each cl In Range("A1:A3") '~~~> Define your range here For i = 1 To Len(cl) temp = temp & Mid(cl, i, 1) & "<" Next i cl = IIf(VBA.Right$(temp, 1) = "<", VBA.Left$(temp, Len(temp) - 1), temp) temp = vbNullString Next cl End Sub 

它可以用Excel公式完成任意长度,但这里是最短的VBA解决scheme

 For Each c In Range("A:A").SpecialCells(xlCellTypeConstants) c.Value2 = Replace( Left$( StrConv( c, vbUnicode), Len(c) * 2 - 1), vbNullChar, "<") Next