将.txt保存为.csv会取消文件中宏所做的所有更改。如何预防呢?

时间:2019-02-05 20:16:44

标签: excel vba

此问题继续this主题,并且与my earlier post相关联。 该代码应处理.csv文件,以更改单元格的值和内部颜色。它可以完成工作,但是将.txt保存为.csv后,我得到的东西看起来像源文件-没有可见的更改。

我曾考虑使用字典,但据我了解,要这样做,我不得不编辑新保存的.csv,这正是我在以下代码中想要避免的。有谁知道如何保存更改?

Option Explicit

Sub fixCellsValue()
Dim wrk As Workbook
Dim Sh As Worksheet
Dim SourceFolder As String, Path As String, TmpFlName As String
Dim i As Long, lastrow As Long

SourceFolder = ThisWorkbook.Path & "\source"

'creating temporary .txt file
If Dir(SourceFolder & "SomeFile.*") <> "" Then
    If InStr(1, Dir(SourceFolder & "SomeFile.*"), ".csv") Then
                    TmpFlName = SourceFolder & "\TmpCsv.txt"
                    If Dir(TmpFlName) <> "" Then Kill TmpFlName
                    FileCopy SourceFolder & "SomeFile.csv", TmpFlName
                    Workbooks.OpenText Filename:=TmpFlName, origin:= _
                    1250, StartRow:=1, DataType:=xlDelimited, TextQualifier:=xlDoubleQuote, _
                    ConsecutiveDelimiter:=False, Tab:=False, Semicolon:=True, Comma:=False _
                    , Space:=False, Other:=False, TrailingMinusNumbers:=True, Local:=False

                    Set wrk = Application.Workbooks("TmpCsv.txt")
                    Set Sh = wrk.Worksheets(1)

        lastrow = Sh.Cells(Sh.Rows.Count, "A").End(xlUp).row

        'implementing changes to the temporary .txt file
        For i = 2 To lastrow
            If Len(Sh.Cells(i, 5)) > 10 Then
                Sh.Cells(i, 5) = Left$(Sh.Cells(i, 5).Value, 10)
                Sh.Cells(i, 5).Interior.ColorIndex = 6
            End If
        Next i

    End If
End If

'saving as .csv file and deleting .txt file
If InStr(1, wrk.Name, "TmpCsv.txt") Then
    wrk.SaveAs Filename:=Dir(SourceFolder & "SomeFile.*"), FileFormat:=xlCSV, Local:=True
    wrk.Close Savechanges:=True
    Kill TmpFlName
End If
End Sub

1 个答案:

答案 0 :(得分:0)

在您的earlier post中,看起来像是一个简单的以分号分隔的文本/ cvs文件,现在看起来很复杂。即使忽略了其他问题,在我的试用版中,我发现在从excel保存txt / csv文件时,它可能会在保存的文件中引入一些双引号(取决于行中逗号,空格和分号的位置)。可以引用链接(Saving a Excel File into .txt format without quotes)和linklink2

据我了解,您的要求很容易截断带有csv扩展名的分号分隔文件的第5列并将其保存回去,这种简单的方法可以解决您的问题。但是,我仍然不满意解决方法,并且邀请更简单直接的方法来解决问题(由txt文件组成,其中逗号,空格和分号,而分号应视为定界符)< / p>

尝试

Sub test2()
Dim Fname As String, Path As String, Txt As String, Txt2 As String
Dim INum As Integer, ONum As Integer, TrucTo As Integer, ColNo As Long
Dim Cols As Variant

' Modify the variables to your requirement
Path = "C:\Users\user\Desktop\"
Fname = "Somefile.csv"     ' Target file name
Fname2 = "Somefile2.csv"   ' Temp file name
TrucTo = 10                ' truncated to chars
ColNo = 4                   '  column to be truncated -1

If Dir(Path & Fname2) <> "" Then Kill Path & Fname2


INum = FreeFile
Open Path & Fname For Input As #INum
ONum = FreeFile
Open Path & Fname2 For Output As #ONum

Do Until EOF(1)
    Line Input #1, Txt
    Cols = Split(Txt, ";")
        If UBound(Cols) >= ColNo Then
            If Len(Cols(ColNo)) >= truncto Then
            Cols(ColNo) = Left(Cols(ColNo), TrucTo)
            End If
        End If
    Txt2 = Join(Cols, ";")
    Print #ONum, Txt2
Loop
Close #ONum
Close #INum


Kill Path & Fname
Name Path & Fname2 As Path & Fname


End Sub

This is the result input & Output