如何在VBA没有内容的情况下打开CVS文件?

时间:2019-06-21 08:32:02

标签: excel vba

每次交易时,我都必须从SAP中提取内容,每次执行交易时,都将其保存在cvs文件中,然后删除交易每一端的内容。 问题是某些事务可能没有数据,而且我事先也不知道,这就是为什么我在每个事务末尾删除内容的原因,所以它不会将数据从旧事务粘贴到我的Excel表中。

错误始终在同一位置,

。刷新BackgroundQuery:= False

        Sub OpenCSVFile()
        '
        'Load the CSV extract
         '
         '
        With ActiveSheet.QueryTables.Add(Connection:= _
       "TEXT;" & fpath & "\" & ffilename, Destination:=Range("$A$1"))
       .Name = "text"
       .FieldNames = True
       .RowNumbers = False
       .FillAdjacentFormulas = False
       .PreserveFormatting = True
       .RefreshOnFileOpen = False
       .RefreshStyle = xlInsertDeleteCells
       .SavePassword = False
       .SaveData = True
       .AdjustColumnWidth = True
       .RefreshPeriod = 0
       .TextFilePromptOnRefresh = False
       .TextFilePlatform = 850
       .TextFileStartRow = 1
       .TextFileParseType = xlDelimited
       .TextFileTextQualifier = xlTextQualifierDoubleQuote
       .TextFileConsecutiveDelimiter = False
       .TextFileTabDelimiter = True
       .TextFileSemicolonDelimiter = False
       .TextFileCommaDelimiter = False
       .TextFileSpaceDelimiter = False
       .TextFileOtherDelimiter = "|"
       .TextFileColumnDataTypes = Array(1)
       .TextFileTrailingMinusNumbers = True
       .Refresh BackgroundQuery:=False

      End With


      End Sub

       [script]

        Sub StartExtract()

     ' Set the sid and client to connect to
       W_System = "P10320"
    ' Run the GUI script
      RunGUIScript
    ' End the GUI session
      objSess.EndTransaction
     'effacer contenu feuille temp
      Sheets("temp").Select
      Cells.Select
      Selection.Delete Shift:=xlUp
     'Switch to the worksheet where the data is loaded to
      Sheets("temp").Select

      'Load the CSV file
      OpenCSVFile

 Sheets("BGSOCIAL").Select
 Columns("B:G").Select
 Selection.ClearContents
 Sheets("temp").Range("B:G").Copy
 Sheets("BGSOCIAL").Range("B:G").PasteSpecial Paste:=xlPasteValues
 Sheets("BGSOCIAL").Select
 Range("B1").Select
 ActiveCell.FormulaR1C1 = "Poste bilan/compte résultat"
 Range("C1").Select
 ActiveCell.FormulaR1C1 = "Texte pos. bilan/cpte résultat"
 Range("D1").Select
 ActiveCell.FormulaR1C1 = "Total période reporting"
 Range("E1").Select
 ActiveCell.FormulaR1C1 = "Total période de comparaison"
 Range("F1").Select
 ActiveCell.FormulaR1C1 = "Ecart absolu"
 Range("G1").Select
 ActiveCell.FormulaR1C1 = "Ecart relatif"


Workbooks.Open FileName:="C:\Users\p100789\Documents\SAP\SAP GUI\text.txt"
Cells.ClearContents
ActiveWorkbook.Close SaveChanges:=True

End Sub

1 个答案:

答案 0 :(得分:0)

您可以在开始之前简单地测试文件中是否有任何数据。

Dim ff As Long
ff = FreeFile
Open fpath & "\" & ffilename For Input As #ff
    If EOF(ff) Then MsgBox "No Data"
Close #ff

或者如果您的文件包含标题但没有数据

Dim ff As Long
ff = FreeFile
Dim dummy As String
Open fpath & "\" & ffilename For Input As #ff
    If EOF(ff) Then
        MsgBox "No Data"
    Else
        Line Input #ff, dummy 'reads the header row, if it exists
        MsgBox "No Data"
    End If
Close #ff

还有其他方法可以做到这一点,但这是最简单的

相关问题