excel 在记事本中查找和替换.xls中的双引号的VBA代码

woobm2wo  于 2023-03-04  发布在  其他
关注(0)|答案(1)|浏览(175)

我需要在记事本中替换.xls SAP导出文件中的双引号,以便能够将其加载到Excel中并执行其他步骤。
xls的问题在于,有些双引号没有结束,这会导致在excel中打开文件时,文件将多行连接到中

我简单地通过加载到powerquery、转换为列、替换双引号来尝试,但结果并不令人满意,因为它将一行中的4列连接到一个单元格中

所以这部分工作,但最成功的一步是打开xls在记事本和取代“没有.这是手动工作,但当通过VBA它只是删除3/4的数据从45万行我有16万.
我正在使用以下代码

Sub changeDQ()
Dim objFSO
Const ForReading = 1
Const ForWriting = 2
Dim objTS
Dim strContents As String
Dim fileSpec As String
Dim DQ As String

fileSpec = "C:\31_12_2022.xls"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTS = objFSO.OpenTextFile(fileSpec, ForReading)

strContents = objTS.ReadAll
strContents = Replace(strContents, Chr(34), " ")

objTS.Close

Set objTS = objFSO.OpenTextFile(fileSpec, ForWriting)

objTS.Write strContents
objTS.Close
End Sub

有没有办法在将文件加载到excel之前删除双引号,并且不像power query那样破坏列分隔符结构?
我们无法在SAP中更改这些输入,因为这些是已关闭的案例
=================================评论意见建议:修改strContents = Replace(strContents, Chr(34), "")
我尝试了多个版本:

strContents = Replace(strContents, Chr(34), "")

strContents = Replace(strContents, Chr(34), "rplc")

strContents = Replace(strContents, """, "")

strContents = Replace(strContents, """", "")

Dim DQ as string
DQ = Chr(34)
strContents = Replace(strContents, DQ, "")

结果总是一样

oxiaedzo

oxiaedzo1#

尝试单独处理这些行。

Sub changeDQ()

    Const ForReading = 1
    Const ForWriting = 2
    Const fileSpec = "31_12_2022.xls"

    Dim fso, tsIn, tsOut, s As String, n As Long
    Dim t0 As Single: t0 = Timer
    
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set tsIn = fso.OpenTextFile(fileSpec, ForReading, Format:=-2)
    Set tsOut = fso.OpenTextFile("~" & fileSpec, ForWriting, _
                Create:=True, Format:=-2) ' -2=system default -1=Unicode
    
    Do While tsIn.AtEndOfStream = False
        s = tsIn.ReadLine
        tsOut.WriteLine Replace(s, Chr(34), "")
        n = n + 1
    Loop
    tsIn.Close
    tsOut.Close
    MsgBox n & " lines processed from " & fileSpec, _
           vbInformation, Format(Timer - t0, "0.0 secs")
   
End Sub

在新的空白工作簿中使用此脚本分析文件。输入100作为字节数,并选择带有对话框的文件。

Sub CheckFile()

    Dim BYTES As Long, filename As String
    
    BYTES = InputBox("Number of bytes", "Bytes")
    With Application.FileDialog(msoFileDialogFilePicker)
        .AllowMultiSelect = False
        .Show
        If .SelectedItems.Count = 0 Then Exit Sub
        filename = .SelectedItems(1)
    End With

    Dim objStreamIn, i, v
    Set objStreamIn = CreateObject("ADODB.Stream")
    With objStreamIn
        .Type = 1 'adTypeBinary
        .Open
        .LoadFromFile filename
        .Position = 0
        v = .Read(BYTES)
    End With
    
    With Sheet1
        .Cells.Clear
        .Range("A1:D1") = Array("Pos", "Dec", "Hex", "Chr")
        For i = 2 To BYTES + 1
            .Cells(i, 1) = i - 1
            .Cells(i, 2) = CLng(v(i - 2))
            .Cells(i, 3) = Hex(v(i - 2))
            .Cells(i, 4) = ChrW(v(i - 2))
        Next
    End With
    
    MsgBox BYTES & " bytes read to sheet1 from " & filename

End Sub

尝试此操作以进一步分析文件。

Sub AnalyseFile()
   
    Dim a() As Byte, i As Long, n As Long, filename As String
    Dim s As String, t0 As Single: t0 = Timer
    
    With Application.FileDialog(msoFileDialogFilePicker)
        .AllowMultiSelect = False
        .Show
        If .SelectedItems.Count = 0 Then Exit Sub
        filename = .SelectedItems(1)
    End With
    
    With CreateObject("ADODB.Stream")
         .Open
         .Type = 1  ' adTypeBinary
         .LoadFromFile filename
         a = .Read
         .Close
    End With
   
    For i = 0 To UBound(a)
        If a(i) = 34 Then
           n = n + 1
           a(i) = 32
        End If
    Next
    s = filename & vbLf & Format(n, "#,###") & " ""'s found and replaced in  " & _
        Format(i, "#,### bytes")
    MsgBox s, vbInformation, Format(Timer - t0, "0.0 secs")
    
    Dim newfile
    newfile = "test_" & Format(Now, "yyyymmdd_hhmmss") & ".xls"
    With CreateObject("ADODB.Stream")
         .Open
         .Type = 1  ' adTypeBinary
         .Write a
         .SaveToFile newfile
         .Close
    End With
    MsgBox "new file created: " & newfile
  
End Sub

相关问题