0

过去几次,我用 excel 和宏编写了 txt 文件。我没有达到10000行或更多。永远不要把话说绝了...

我的 .csv 文件有超过 87000 行,例如"15k50,CityABC,56ab,CountryofCity,ID,Street"。我使用 Split() 函数来分隔值。宏格式化并将值作为单行写入 txt 文件。

txt 文件关闭了大约 9800 行...但是为什么呢?我尝试使用 Slepp() 来确保打印算法没有过载或其他原因。

计数器 10000 在那里是因为我想让你更容易理解。如果超过 10000,则问题已“解决”。

信息txt-文件格式:

  • ASCII
  • Unix (LF)

捷径,经过多次评论

  • 使用Minimal, Reproducible Example使代码过度工作(删除睡眠,简化变量名称,尝试从头开始编写代码)
  • 更改SplitString()Split(),因为调用函数很愚蠢...
  • 在将第 9000 行打印到 txt 文件后,代码行会弹出以下错误“运行时错误 5:无效的过程调用或参数”fso.WriteLine ("# " & strArr(0) & " # " & strArr(1) & ...
Option Explicit

#If VBA7 Then
 Public Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As LongPtr) 'For 64 Bit Systems
#Else
 Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long) 'For 32 Bit Systems
#End If

Sub formattedToTxt()

Dim strArr() As String
Dim strB As String
Dim intC As Integer

Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")

strB = filePathExport & "\" & filenameExport & fileFormatExport
Set fso = fso.CreateTextFile(strB, True)

Do While counter <= 10000
    strArr = Split(ActiveCell.Value, ",")
    intC = CalcWhitespace(strArr(5), 40)
    fso.WriteLine ("#  " & strArr(0) & "  #  " & strArr(1) & "  # " & strArr(2) & " # " & strArr(3) & "  # " & strArr(4) & " # " & strArr(5) & Space(intC) & "#")
    ActiveCell.Offset(1, 0).Select
    If ((counter Mod 1000) = 0) Then
        Debug.Print ("Entry " & counter & " written")
    End If
    counter = counter + 1
Loop

End Sub

Function CalcWhitespace(rawStr As String, maxLen As Integer) As Integer
    CalcWhitespace = maxLen - Len(rawStr)
End Function

任何想法?

4

1 回答 1

1

感谢上述评论中所有有用的 VBA 人员。我清理了代码。以下截图是完整的解决方案。

.csv 表在每行中包含不同长度的不同字符串。在最终解决方案中,之前检查了空格。了解数据中的最大长度字符串很重要。格式化 txt 文件输出可读。

也许,其他解决方案具有更好的性能,但在我的情况下效果很好。

祝你有美好的一天!

'Find max string length for each whitespace
counter = 0
ActiveSheet.Cells(2, 1).Select   'ignore Headlinedata, because diffrent format in compare to data
intC = (UBound(arrWhitespace) - LBound(arrWhitespace))
Do While ActiveCell.Value <> ""
    strArr = Split(ActiveCell.Value, ",")
    For counterTwo = 0 To intC
        If Len(strArr(counterTwo)) > arrWhitespace(counterTwo) Then arrWhitespace(counterTwo) = Len(strTempArr(counterTwo))
    Next counterTwo
    counter = counter + 1
    ActiveCell.Offset(1, 0).Select
    If ((counter Mod 1000) = 0) Then
        Debug.Print ("Entry " & counter & " checked")
    End If
Loop

'Print Body of txt-file
Do While ActiveCell.Value <> ""
    strArr = Split(ActiveCell.Value, ",")
        
    'build string for each line
    strB = ""
    strB = strB & "#  " & strArr(0) & "  #"
    intC = CalcWhitespace(strArr(1), arrWhitespace(1))
    strB = strB & "  " & strArr(1) & Space(intC) & "  #"
    intC = CalcWhitespace(strArr(2), arrWhitespace(2))
    strB = strB & " " & strArr(2) & Space(intC) & " #"
    intC = CalcWhitespace(strArr(3), arrWhitespace(3))
    strB = strB & " " & strArr(3) & Space(intC) & "  #"
    intC = CalcWhitespace(strArr(4), arrWhitespace(4))
    strB = strB & " " & strArr(4) & Space(intC) & " #"
    intC = CalcWhitespace(strArr(5), arrWhitespace(5))
    strB = strB & " " & strArr(5) & Space(intC) & "  #"
    fso.WriteLine (strB)
    ActiveCell.Offset(1, 0).Select
    If ((counter Mod 1000) = 0) Then
        Debug.Print ("Entry " & counter & " written")
    End If
    counter = counter + 1
Loop

Function CalcWhitespace(rawStr As String, maxLen As Integer) As Integer
    CalcWhitespace = maxLen - Len(rawStr)
End Function

下次我将使用最小可重现示例来避免.Select

于 2021-01-27T07:41:37.770 回答