循环前的第一行

时间:2016-10-09 13:23:19

标签: vba

此函数将一个长连续字符串分解为较小的字符串并添加前缀和后缀/

这行代码给了我需要它的问题,因为第一个前缀与其他前缀不同,但它会导致第一行产生两次而不确定如何重写行/代码克服这个问题?

.WriteLine "s = """ & Trim$(Mid$(strInput, 1, intSize * AtomSize)) & """"

以下是完整的成功:

Sub StringBuilder(intSize As Integer, Optional AtomSize As Long = 3)
Dim i As Long
Dim strInput As String

strInput = CreateObject("Scripting.FileSystemObject").OpenTextFile(CurrentProject.Path & "\input.txt").ReadAll
With CreateObject("Scripting.FileSystemObject").CreateTextFile(CurrentProject.Path & "\output.txt", True)
    .WriteLine "s = """ & Trim$(Mid$(strInput, 1, intSize * AtomSize)) & """"
  For i = 1 To Len(strInput) - intSize * AtomSize Step intSize * AtomSize
    .WriteLine "s = s & """ & Trim$(Mid$(strInput, i, intSize * AtomSize)) & """"
  Next
  .WriteLine "s = s & """ & Trim$(Mid$(strInput, i, intSize * AtomSize)) & """"
End With

End Sub

2 个答案:

答案 0 :(得分:1)

简单的改变

.WriteLine "s = """ & Trim$(Mid$(strInput, 1, intSize * AtomSize)) & """"

.WriteLine "s = """

Sub StringBuilder(intSize As Integer, Optional AtomSize As Long = 3)
    Dim i As Long
    Dim strInput As String

    strInput = CreateObject("Scripting.FileSystemObject").OpenTextFile(CurrentProject.Path & "\input.txt").ReadAll
    With CreateObject("Scripting.FileSystemObject").CreateTextFile(CurrentProject.Path & "\output.txt", True)
        .WriteLine "s = """
      For i = 1 To Len(strInput) - intSize * AtomSize Step intSize * AtomSize
        .WriteLine "s = s & """ & Trim$(Mid$(strInput, i, intSize * AtomSize)) & """"
      Next
      .WriteLine "s = s & """ & Trim$(Mid$(strInput, i, intSize * AtomSize)) & """"
    End With

End Sub

答案 1 :(得分:0)

这是一个可用于将字符串分成组的函数:

Sub test()
    Dim myString As String

    myString = "abcd"
    Debug.Print GroupString(myString, 2, , "[", "]")

    myString = "This is a very long string, it would be nice if it were split into several lines, where each line is assigned to a variable"
     Debug.Print GroupString(myString, 20, """" & vbCrLf & "s = s & """, "s = """, """")
End Sub

测试它:

[ab,cd]
s = "This is a very long "
s = s & "string, it would be "
s = s & "nice if it were spli"
s = s & "t into several lines"
s = s & ", where each line is"
s = s & " assigned to a varia"
s = s & "ble"

输出:

console.log(new Date(milliseconds)

将它用于您的问题:将输入文件读入字符串,然后在单个写操作中,使用单个写操作写入应用于字符串的函数的结果,使用类似于第二个的调用我的测试代码中的示例。