简体   繁体   English

VBA:逐字符分配字符串

[英]VBA: assign string character by character

Most people ask how to get the characters from a string, which can be done by Mid().大多数人问如何从字符串中获取字符,这可以通过 Mid() 来完成。 I am trying to assign a string character by character in VBA code.我试图在 VBA 代码中逐个字符地分配一个字符串。 The characters to be assigned depend on some calculated results.要分配的字符取决于一些计算结果。

I do not want to use string concatenation to form the string.我不想使用字符串连接来形成字符串。

I have searched the web, but the posted solution, strName.Chars(i) (eg, at MS development network), is not recognized in my 2007 Access VBA.我在网上搜索过,但我的 2007 Access VBA 无法识别已发布的解决方案 strName.Chars(i)(例如,在 MS 开发网络中)。 Thanks谢谢

You can use Mid to set values, too.您也可以使用Mid来设置值。

Sub showMidExample()

    Dim s As String
    s = "aaaaa"

    Dim i As Integer
    For i = 1 To Len(s)
        Mid(s, i) = "n"
        Debug.Print s

    Next i

End Sub

This prints out这打印出来

naaaa
nnaaa
nnnaa
nnnna
nnnnn

Which is what you are looking for.这就是你要找的。

Since no working answer is posted.由于没有发布工作答案。 I assume that cannot be done in VBA and I have to use concatenation to form the string although that is cumbersome in my case.我认为这不能在 VBA 中完成,我必须使用连接来形成字符串,尽管在我的情况下这很麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM