+ Reply to Thread
Results 1 to 2 of 2

Hard Code Folder Path for Browse Box

Hybrid View

  1. #1
    Registered User
    Join Date
    02-01-2005
    Posts
    3

    Hard Code Folder Path for Browse Box

    Hello,

    I found the following code on this excellent site. It opens a Browse Box and allows the user to multi select text files for import.

    I need to edit this to hard code a folder path and select all text files within the folder, without having the browse box open. ( ie) C:\Batch\*.txt )

    The Browse Box opens at

    FileList = Application.GetOpenFilename _
    (FileFilter:="All Files(*.*), *.*", _
    Title:="Select files", MultiSelect:=True)

    How would I go about editing the following code to accomplish this? Thank you kindly in advance.

    Adam


    _____________________________

    Sub InsertDataFromTextFiles()
    Dim ColumnOffset As Long
    Dim DestCell As Range
    Dim i As Long
    Dim SourceData As Range
    Dim FileList As Variant


    Const ColumnsPerFile As Long = 13

    FileList = Application.GetOpenFilename _
    (FileFilter:="All Files(*.*), *.*", _
    Title:="Select files", MultiSelect:=True)

    'returns an array if at least 1 file is selected
    'if user cancelled, returns Boolean = False
    If TypeName(FileList) <> "Variant()" Then Exit Sub

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    Set DestCell = Selection.Range("A1")
    ColumnOffset = -ColumnsPerFile 'will increment to 0 on 1st pass

    For i = LBound(FileList) To UBound(FileList)
    Set SourceData = _
    Workbooks.Open(FileName:=FileList(i)).Worksheets(1).UsedRange

  2. #2
    Registered User
    Join Date
    02-01-2005
    Posts
    3
    oops! The above code was cut off. Here it is:

    ________________________________

    Sub InsertDataFromTextFiles()
    Dim ColumnOffset As Long
    Dim DestCell As Range
    Dim i As Long
    Dim SourceData As Range
    Dim FileList As Variant


    Sheets("Sheet1").Select

    Const ColumnsPerFile As Long = 13

    FileList = Application.GetOpenFilename _
    (FileFilter:="All Files(*.*), *.*", _
    Title:="Select files", MultiSelect:=True)

    'returns an array if at least 1 file is selected
    'if user cancelled, returns Boolean = False
    If TypeName(FileList) <> "Variant()" Then Exit Sub

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False

    Set DestCell = Selection.Range("A1")
    ColumnOffset = -ColumnsPerFile 'will increment to 0 on 1st pass

    For i = LBound(FileList) To UBound(FileList)
    Set SourceData = _
    Workbooks.Open(FileName:=FileList(i)).Worksheets(1).UsedRange



    Selection.CurrentRegion.Select
    Selection.Copy
    ColumnOffset = ColumnOffset + ColumnsPerFile
    DestCell.Offset(0, ColumnOffset).PasteSpecial Paste:=xlFormulas

    Application.CutCopyMode = False
    SourceData.Parent.Parent.Close SAVECHANGES:=False
    Next i

    DestCell.Activate
    Application.DisplayAlerts = True
    Application.ScreenUpdating = True


    End Sub 'InsertDataFromTextFiles

+ 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