Results 1 to 11 of 11

Reference Text File To Import Using Generic Letter or Number

Threaded View

  1. #1
    Forum Contributor
    Join Date
    07-28-2008
    Location
    Leamington Spa, UK
    MS-Off Ver
    2010
    Posts
    142

    Reference Text File To Import Using Generic Letter or Number

    Hi all,

    I have recorded some script in excel to import a *.dat file, delete the first 19 rows of data, then export the data as a *.csv file.

    My problem is I have circa 30 *.dat files to import, all of which have large file names and I am fed up with copying and pasting them in.

    So my question is, in the below code, can I somehow use a reference at the start such as:

    abc1.dat = "1"
    abc2.dat = "2"

    Then where the files were used, reference the "1".dat and its exported version "1".csv for the abc1.dat case.

    Sub Import_dat_Modify_Export_csv()
    '
    '
    ' Macro to save *.dat files in *.csv format while removing the un-needed data at the top of the *.dat files
      '1
      With ActiveSheet.QueryTables.Add(Connection:= _
            "TEXT;D:\TEMP\DIR\abc1.dat" _
            , Destination:=Range("A1"))
            .Name = "abc1"
            .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 = True
            .TextFileTabDelimiter = False
            .TextFileSemicolonDelimiter = False
            .TextFileCommaDelimiter = False
            .TextFileSpaceDelimiter = True
            .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
        End With
        Rows("1:19").Select
        Selection.Delete Shift:=xlUp
        Range("A1").Select
        ChDir "D:\TEMP\X123"
        ActiveWorkbook.SaveAs Filename:= _
            "D:\TEMP\X123\abc1.csv" _
            , FileFormat:=xlCSV, CreateBackup:=False
        ActiveWindow.Close
        Workbooks.Add
     
      '2
      With ActiveSheet.QueryTables.Add(Connection:= _
            "TEXT;D:\TEMP\DIR\abc2.dat" _
            , Destination:=Range("A1"))
            .Name = "abc2"
            .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 = True
            .TextFileTabDelimiter = False
            .TextFileSemicolonDelimiter = False
            .TextFileCommaDelimiter = False
            .TextFileSpaceDelimiter = True
            .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
        End With
        Rows("1:19").Select
        Selection.Delete Shift:=xlUp
        Range("A1").Select
        ChDir "D:\TEMP\X123"
        ActiveWorkbook.SaveAs Filename:= _
            "D:\TEMP\X123\abc2.csv" _
            , FileFormat:=xlCSV, CreateBackup:=False
        ActiveWindow.Close
        Workbooks.Add
     
    End Sub
    Appologies as I am somewhat *poor* at VBA code - hence my use of the Recorder.

    Cheers
    Last edited by VBA Noob; 01-26-2009 at 05:59 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1