CSV file import into Excel sheet

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

    #1

    CSV file import into Excel sheet

    Hello,
    I am trying to import 4 separate csv files from 4 different servers into an excel sheet.I want to write a macro which will automate this.Also, i want to append the name of the server for each file in next free column.
    How do i ensure that the next csv file does not overwrite the previous import.i.e to make sure that next free cell (*,1) is selected before the next import.Here is what i have been able to generate so far:
    Code:
    With ActiveSheet.QueryTables.Add(Connection:= _
            "TEXT;C:\Documents and Settings\Forcados\My Documents\My Data Sources\Output.NET\ssh\ssh1_list.csv" _
            , Destination:=Range("A1"))
            .Name = "LAG-BSC_list"
            .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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
            1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
        End With
        ActiveWindow.SmallScroll Down:=15
        Range("A41").Select
        With ActiveSheet.QueryTables.Add(Connection:= _
            "TEXT;C:\Documents and Settings\Forcados\My Documents\My Data Sources\Output.NET\ssh\ssh1_list.csv" _
            , Destination:=Range("A41"))
            .Name = "IBD-BSC_list"
            .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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, _
            1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
        End With
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #2
    So, is it the "A41" in your example that you want to replace with something dynamically generated?

    Comment

    • DONE1
      New Member
      • Nov 2006
      • 9

      #3
      It is not always A41.
      The next free cell depends on the content of the csv file.The problem is how to identify the next free cell in the first column(i.e cell(*,1). & to indicate the source of the file in a column in the excell file.

      Thanks

      Comment

      • Killer42
        Recognized Expert Expert
        • Oct 2006
        • 8429

        #4
        Originally posted by DONE1
        It is not always A41.
        The next free cell depends on the content of the csv file.The problem is how to identify the next free cell in the first column(i.e cell(*,1). & to indicate the source of the file in a column in the excell file.
        Hm... well, identifying the next empty cell should be fairly simple. Although I'm sure there is a more direct way, one thing you could try is to do the equivalent of jumping to the bottom of column A, then hitting [End]-[Up] to jump to the next non-blank, then move down a row.

        Not sure about the server-name thing, though.

        Don't forget, you can often find the way to do something in code by just doing it yourself, while Excel records it as a macro. Then go edit/copy the code that it generated. This doesn't always work, but it's a handy shortcut.

        Comment

        Working...