VBA Excel字符和空格连接多个字符串

时间:2015-06-26 19:58:34

标签: excel vba character space string-concatenation

我需要创建一个错误代码字符串,其中某些错误字符占据字符串中的某些位置。

假设你有代码 F ,它必须位于 2

输出: _F

现在,如果我们的代码 G 位于 5

输出: ____ G

现在如果在一个实例中我有两个错误怎么办?我该如何输出这个字符串:

_F__G

用户错误提示:

Prompt

我的代码:

ErrorCode = Space(CodeNumber.Value) + CodeCharacter.Value

有一种简单的方法吗?我知道我可以为第一个代码adn执行此操作,然后在第二个代码中使用较早的数字( 2 )并从( 5 )中减去它并取 1 < / strong>离开然后空间并添加角色。但是当我们有多个代码和空格时,这似乎很麻烦。

在VBA中添加两个字符串会发生什么?说&#34; ___ A&#34; +&#34; _B&#34;我假设它是&#34; ___ A_B&#34;。

有没有得到&#34; _B_A ?&#34;

谢谢!

------------------------从两个答案中尝试创意后----------------- ---

我得到只返回一个错误代码

Private Sub ExportError_Click()
    Dim myErr As String
    myErr = ""

Dim myFile As String
myFile = "C:\Reformatted.txt"
Open myFile For Output As #1

    If F_2 = True Then
    AddCode "F", 2, myErr
    ElseIf G_3 = True Then
    AddCode "G", 3, myErr
    ElseIf H_4 = True Then
    AddCode "H", 4, myErr
    End If

Print #1, myErr

Close #1
Shell "C:\Windows\Notepad.exe C:\Reformatted.txt", 1

End Sub

Function AddCode(Lett As String, Pos As Long, ByRef ErrString As String)
    If Pos > Len(ErrString) Then
        ErrString = ErrString & Space(Pos - Len(ErrString))
    End If
    Mid(ErrString, Pos, 1) = Lett
End Function

Private Sub UserForm_Click()

End Sub

3 个答案:

答案 0 :(得分:4)

Sub Tester()
    Dim myErr As String

    AddCode "F", 2, myErr
    AddCode "G", 5, myErr
    AddCode "A", 1, myErr

    Debug.Print myErr
End Sub

Function AddCode(Lett As String, Pos As Long, ByRef ErrString As String)
    If Pos > Len(ErrString) Then
        ErrString = ErrString & Space(Pos - Len(ErrString))
    End If
    Mid(ErrString, Pos, 1) = Lett
End Function

从你的例子:

Private Sub ExportError_Click()
    Dim myErr As String
    myErr = ""

    Dim myFile As String
    myFile = "C:\Reformatted.txt"
    Open myFile For Output As #1

    If F_2 = True Then AddCode "F", 2, myErr
    If G_3 = True Then AddCode "G", 3, myErr
    If H_4 = True Then AddCode "H", 4, myErr

    Print #1, myErr

    Close #1
    Shell "C:\Windows\Notepad.exe C:\Reformatted.txt", 1

End Sub

答案 1 :(得分:3)

最好使用数组来保存错误代码,然后在最后连接数组中的每个值以获取完整的错误代码。然后,您可以通过数组索引访问它们的正确位置。

Dim codes(0 To 4) As String
Dim errorString As String
Dim code As Variant

codes(1) = "F"
codes(4) = "G"

For Each code In codes
    If code <> "" Then
        errorString = errorString + code 'if there is a code, insert it
    Else
        errorString = errorString + " " 'if no code, insert a space
    End If
Next code

我对错误进行了硬编码,但是如果你在某处有一个列表,你可以遍历它们并像下面的伪代码一样分配它们:

For Each error In errors
    codes(error.CodeNumber) = error.CodeCharacter
Next error

答案 2 :(得分:1)

@ Mike或@TimWilliams的解决方案都可以使用 - 您的(新的?)问题是如何选择要添加到错误字符串中的项目。当你使用'ElseIf'时,你只会得到你的三个条件中的一个。

那就是说,这里还有其他一些选择。首先,类似于迈克的答案,你可以使用字节数组......

Private Sub ExportError_Click()
    Dim myErr() As Byte
    myErr = StrConv(String$(5, " "), vbFromUnicode)

    Dim myFile As Integer
    myFile = FreeFile
    Open "C:\Reformatted.txt" For Output As #myFile

    If F_2 = True Then myErr(1) = CByte(Asc("F"))
    If G_3 = True Then myErr(2) = CByte(Asc("G"))
    If H_4 = True Then myErr(3) = CByte(Asc("H"))

    Print #myFile, StrConv(myErr, vbUnicode)
    Close #myFile

    Shell "Notepad.exe C:\Reformatted.txt", 1
End Sub

...或我个人的偏好,词典:

Private Sub ExportError_Click()
    Dim myErr As New Scripting.Dictionary
    Dim pos As Integer

    'Initialize to spaces.
    For pos = 1 To 5
        myErr.Add pos, " "
    Next pos

    Dim myFile As Integer
    myFile = FreeFile
    Open "C:\Reformatted.txt" For Output As #myFile

    If F_2 = True Then myErr(2) = "F"
    If G_3 = True Then myErr(3) = "G"
    If H_4 = True Then myErr(4) = "H"

    Print #myFile, Join$(myErr.Items, vbNullString)
    Close #myFile

    Shell "Notepad.exe C:\Reformatted.txt", 1
End Sub

请注意,要使用Dictionary,您需要引用Microsoft Scripting Runtime。在VB编辑器的菜单中,转到Tools-&gt; References ...,然后选中“Microsoft Scripting Runtime”旁边的框。您可能需要在列表中滚动方式。