+ Reply to Thread
Results 1 to 1 of 1

How to rename sheet to match name of the imported file

  1. #1
    Registered User
    Join Date
    03-10-2010
    Location
    Atlanta, GA
    MS-Off Ver
    Excel 2003
    Posts
    1

    How to rename sheet to match name of the imported file

    Hey, everyone. I've written a Macro which allows me to import multiple .csv files as separate sheets. But I'm having trouble finding a way to rename each new sheet to match the name of the file being imported.

    For example, if I want to import the contents of files called "File1.csv" and "File2.csv" to new worksheets, I'd like to name those worksheets "File1" and "File2" respectively.

    Any advice?

    I'll attach my very amateur coding.

    Thanks in advance!

    >>>

    Sub ImportFilesToSheets()

    Dim fpath As String
    Dim fname As Variant
    Dim X As Long


    fpath = "D:\Documents and Settings\"
    fname = Application.GetOpenFilename _
    (filefilter:="CSV Files (*.csv), *.csv", MultiSelect:=True)

    For X = LBound(fname) To UBound(fname)

    Sheets("Main Sheet").Select
    Sheets.Add

    With ActiveSheet.QueryTables.Add(Connection:="TEXT;" _
    & fname(X), Destination:=Range("A1"))
    .Name = "fname"
    .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 = False
    .TextFileSemicolonDelimiter = False
    .TextFileCommaDelimiter = True
    .TextFileSpaceDelimiter = False
    .TextFileColumnDataTypes = Array(1, 1)
    .TextFileTrailingMinusNumbers = True
    .Refresh BackgroundQuery:=False
    End With

    Next X

    End Sub

    >>>
    Last edited by GoJackets; 03-11-2010 at 09:23 PM.

+ Reply to Thread

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