TextFile Automation

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DONE1
    New Member
    • Nov 2006
    • 9

    #1

    TextFile Automation

    Hello,
    I have written a macro that will import 4 different text files into 1 excel worksheet.
    After each import i append an ID(5th column) to distingush the source of the data.
    My problem is how to make sure that it always imports starting from the next free cell & to make sure it appends the ID correctly.
    Here is what i have for now:
    Code:
        Range("A1").Select
        With ActiveSheet.QueryTables.Add(Connection:= _
            "TEXT;C:\WK44.Net\LAG\MFS_list.csv" _
            , Destination:=Range("A1"))
            .Name = "MFS_list_1"
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .TextFilePromptOnRefresh = False
            .TextFilePlatform = 437
            .TextFileStartRow = 1
            .TextFileParseType = xlDelimited
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = False
            .TextFileTabDelimiter = True
            .TextFileSemicolonDelimiter = True
            .TextFileCommaDelimiter = False
            .TextFileSpaceDelimiter = False
            .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
        End With
        Range("A2").Select
        Selection.End(xlToRight).Select
        Range("E2").Select
        ActiveCell.FormulaR1C1 = "1"
        Range("E2").Select
        Selection.Copy
        Range("E3:E6").Select
        ActiveSheet.Paste
        Range("A7").Select
        Application.CutCopyMode = False
        With ActiveSheet.QueryTables.Add(Connection:= _
            "TEXT;C:\WK44.Net\IBD\MFS_list.csv" _
            , Destination:=Range("A7"))
            .Name = "MFS_list_2"
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .TextFilePromptOnRefresh = False
            .TextFilePlatform = 437
            .TextFileStartRow = 1
            .TextFileParseType = xlDelimited
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = False
            .TextFileTabDelimiter = True
            .TextFileSemicolonDelimiter = True
            .TextFileCommaDelimiter = False
            .TextFileSpaceDelimiter = False
            .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
        End With
        Selection.End(xlToRight).Select
        Range("E8").Select
        ActiveCell.FormulaR1C1 = "2"
        Range("E9").Select
        ActiveCell.FormulaR1C1 = "2"
        Range("A1").Select
        Selection.End(xlDown).Select
        Range("A10").Select
        With ActiveSheet.QueryTables.Add(Connection:= _
            "TEXT;C:\WK44.Net\BEN\MFS_list.csv" _
            , Destination:=Range("A10"))
            .Name = "MFS_list_3"
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .TextFilePromptOnRefresh = False
            .TextFilePlatform = 437
            .TextFileStartRow = 1
            .TextFileParseType = xlDelimited
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = False
            .TextFileTabDelimiter = True
            .TextFileSemicolonDelimiter = True
            .TextFileCommaDelimiter = False
            .TextFileSpaceDelimiter = False
            .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
        End With
        Selection.End(xlToRight).Select
        Range("E11").Select
        ActiveCell.FormulaR1C1 = "3"
        Range("A1").Select
        Selection.End(xlDown).Select
        Range("A12").Select
        With ActiveSheet.QueryTables.Add(Connection:= _
            "TEXT;C:\WK44.Net\ABJ\MFS_list.csv" _
            , Destination:=Range("A12"))
            .Name = "MFS_list_4"
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .TextFilePromptOnRefresh = False
            .TextFilePlatform = 437
            .TextFileStartRow = 1
            .TextFileParseType = xlDelimited
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = False
            .TextFileTabDelimiter = True
            .TextFileSemicolonDelimiter = True
            .TextFileCommaDelimiter = False
            .TextFileSpaceDelimiter = False
            .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
        End With
        Selection.End(xlToRight).Select
        Range("E13").Select
        ActiveCell.FormulaR1C1 = "4"
        Range("E14").Select
        ActiveCell.FormulaR1C1 = "4"
        Range("A1").Select
    End Sub
    Thanks in anticipation
  • DONE1
    New Member
    • Nov 2006
    • 9

    #2
    Can anybody help me out.

    Comment

    • albertw
      Contributor
      • Oct 2006
      • 267

      #3
      Originally posted by DONE1
      Can anybody help me out.
      hi you're using an extreme amount of statements if you ask me

      e.g:
      Range("E2").Sel ect
      ActiveCell.Form ulaR1C1 = "1"
      could be translated into 'real VBA' as
      Range( "E2").Value ="1"

      Code:
          Range("A2").Select
          Selection.End(xlToRight).Select
          Range("E2").Select
          ActiveCell.FormulaR1C1 = "1"
          Range("E2").Select
          Selection.Copy
          Range("E3:E6").Select
          ActiveSheet.Paste
          Range("A7").Select
      what do you want in A2 ?

      if you want to set range("e3:e6") to be "1"

      for i=0 to 4
      range("e3").off set(0,i).value= "1"
      next i


      Code:
          Range("E8").Select
          ActiveCell.FormulaR1C1 = "2"
          Range("E9").Select
          ActiveCell.FormulaR1C1 = "2"
          Range("A1").Select
          Selection.End(xlDown).Select
          Range("A10").Select
         With ActiveSheet.QueryTables.Add(Connection:= _
              "TEXT;C:\WK44.Net\BEN\MFS_list.csv" _
              , Destination:=Range("A10"))
      same: Range("E8").Val ue="2"
      range("e9").val ue to be "2" too ?
      then you switch to A1 and back to A10 where you tell the program
      to paste te next file from A10
      double statement ...

      by the way, you seem to know exactly how large the files are
      while you use fixed addresses.
      so why take the trouble for data-separation ?

      Comment

      Working...