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

VBA Excel Characters and Spaces Concatenating Multiple Strings

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

假设您有代码 F 并且它必须在位置 2

输出:_F

现在,如果我们有代码 G,它位于 5.

输出:____G

现在如果我有两个错误怎么办?我怎样才能输出这个字符串:

_F__G

用户错误提示:

我的代码:

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

有没有简单的方法来做到这一点?我知道我可以对第一个代码执行此操作,然后对第二个代码执行此操作,取较早的数字 (2) 并将其从 (5) 中减去,然后去掉 1 然后 space 并添加字符。但是当我们有多个代码和 spaces.

时,这看起来很混乱和困难

当我在 VBA 中添加两个字符串时会发生什么?说“___A”+“_B”我假设它是“___A_B”。

有没有办法得到“_B_A?”

谢谢!

------------------------在尝试了两个答案的想法之后---------------- -----

我得到这个 只有 returns 一个错误代码

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

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

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

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

也就是说,还有其他几个选项。首先,类似于 Mike 的回答,您可以使用 Byte 数组...

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

请注意,要使用词典,您需要引用 Microsoft Scripting Runtime。在 VB 编辑器的菜单中,转到 Tools->References...,然后选中 "Microsoft Scripting Runtime" 旁边的框。您可能需要在列表中滚动一种方式。