2012-01-05 2 views
3

아래 스크립트를 사용하여 텍스트 파일로 내 보낸 통합 문서가 있습니다. 그것은 잘 작동하지만 텍스트 파일을 열 때 항상이 텍스트 파일을 생성 한 후에 실행하는 다른 스크립트에 문제가 발생한 끝에 빈 줄이 있습니다. 수출에서 빈 줄을 제거하는 방법에 대한 도움.Excel VBA 텍스트 파일로 내보내기. 빈 줄을 지워야합니다.

코드 : 나는 장 - 프랑수아 코르 베트 당신의 코멘트를 upticked 있지만

Sub Rectangle1_Click() 
    Dim fPath As String 
    Dim exportTxt As String 
    fPath = CreateObject("WScript.Shell").SpecialFolders("Desktop") & "\Sample_" & Format(Now(), "HHNNSS") & ".txt" 

    exportTxt = ActiveWorkbook. 

    Open fPath For Append As #1 'write the new file 
    Print #1, exportTxt; 
    Close #1 
End Sub 
+0

는 엑셀 문서. 그러나 저장 후 즉시 파일을 읽고 여분의 줄을 제거 할 수 있습니다. –

+2

다른 스크립트가 너무 민감하다고 말하고 싶습니다 ... 뒤의 빈 줄에 숨 막히지 않도록 변경하십시오! –

+0

VBA가 활성 워크 시트를 내보내는 방식을 변경하려고 시도했지만 작동하지 않는 것 같습니다. 위의 내용에서 saveas와 같은 활성 워크 시트의 모든 내용을 저장하기 위해 exporttxt 변수를 가져올 수 없습니다. – user1132827

답변

0

:

Sub Rectangle1_Click() 
    Application.DisplayAlerts = False 

' Save file name and path into a variable 
    template_file = ActiveWorkbook.FullName 

' Default directory would be c:\temp. Users however will have the ability 
' to change where to save the file if need be. 

     fileSaveName = Application.GetSaveAsFilename(_ 
     InitialFileName:="C:\users\%username%\SNSCA_Customer_" + _ 
     VBA.Strings.Format(Now, "mmddyyyy") + ".txt", _ 
     fileFilter:="Text Files (*.txt), *.txt") 

    If fileSaveName = False Then 
     Exit Sub 
    End If 

    ' Save file as .txt TAB delimited fileSaveName, FileFormat:=36, 

     ActiveWorkbook.SaveAs Filename:= _ 
     fileSaveName, FileFormat:=xlTextWindows, _ 
     CreateBackup:=False 

     file_name_saved = ActiveWorkbook.FullName 
    MsgBox "Your SNSCA configuration upload file has been " _ 
     & "successfully created at: " & vbCr & vbCr & file_name_saved 

End Sub 

편집 ... 여기

중 하나를 작동하지 않는 대안이다 아래 VBA를 사용하여 txt 파일의 마지막 줄을 지울 수 있습니다 (이 방법으로 저장하면 빈 줄이 기록됨).

이 VBA는 일반적으로 사용되는 루틴을 기반으로합니다. 그것은

  • 는 새로 만든 텍스트 파일을 읽고 예 (* SNSCA_Customer_01092012.txt *)
  • 분할 것이 라인으로 라인
  • 다음 새 txt 파일로 마지막을 제외한 모든 라인을 다시 작성

  • (* SNSCA_Customer_01092012clean.txt *)

    주어진 입력에 많은 이유로 발생 될 수있다
    Sub Rectangle1_Click() 
    Dim strTemplateFile As String 
    Dim strFname As String 
    Dim strFnameClean As String 
    Dim FileSaveName 
    
    Application.DisplayAlerts = False 
    ' Save file name and path into a variable 
    strTemplateFile = ActiveWorkbook.FullName 
    
    ' Default directory would be c:\temp. Users however will have the ability 
    ' to change where to save the file if need be. 
    
    FileSaveName = Application.GetSaveAsFilename(_ 
           InitialFileName:="C:\users\%username%\SNSCA_Customer_" + _ 
               VBA.Strings.Format(Now, "mmddyyyy") + ".txt", _ 
           fileFilter:="Text Files (*.txt), *.txt") 
    
    If FileSaveName = False Then 
        Exit Sub 
    End If 
    
    ' Save file as .txt TAB delimited fileSaveName, FileFormat:=36, 
    ActiveWorkbook.SaveAs Filename:= _ 
             FileSaveName, FileFormat:=xlTextWindows, _ 
             CreateBackup:=False 
    
    strFname = ActiveWorkbook.FullName 
    strFnameClean = Replace(ActiveWorkbook.FullName, ".txt", "clean.txt") 
    MsgBox "Your SNSCA configuration upload file has been " _ 
        & "successfully created at: " & vbCr & vbCr & strFname 
    Call Test(strFname, strFnameClean) 
    End Sub 
    
    
    Sub Test(ByVal strFname, ByVal strFnameClean) 
    Const ForReading = 1 
    Const ForWriting = 2 
    
    Dim objFSO As Object 
    Dim objTF As Object 
    Dim strAll As String 
    Dim varTxt 
    Dim lngRow As Long 
    
    Set objFSO = CreateObject("Scripting.FileSystemObject") 
    Set objTF = objFSO.OpenTextFile(strFname, ForReading) 
    strAll = objTF.readall 
    objTF.Close 
    Set objTF = objFSO.createTextFile(strFnameClean, ForWriting) 
    varTxt = Split(strAll, vbCrLf) 
    For lngRow = LBound(varTxt) To UBound(varTxt) - 1 
        objTF.writeline varTxt(lngRow) 
    Next 
    objTF.Close 
    End Sub 
    
관련 문제