+ Reply to Thread
Results 1 to 2 of 2

Handling multiple txt files within single excel sheet

  1. #1
    Registered User
    Join Date
    10-19-2012
    Location
    United Kingdom
    MS-Off Ver
    Excel 2003
    Posts
    1

    Handling multiple txt files within single excel sheet

    Hi guy,

    I only registered in this site today since I am going to be spending some of my time from now on to perfect my Excel programmimg skills from, well scratch really. So, I would need you experts help.

    My first task is to write a macro that will:

    -Allow me to selects a number of files in a directory (so when I run it opens the directory to allow me to select a number of files in one go)
    - Selects same two coloumns in all files
    -plots on the same scale

    To start with, I would appraciate if I get some heads up. Right now, as a first exercise, I am recording a macro to see the automatically generated code.
    Thanks.

  2. #2
    Forum Contributor
    Join Date
    10-18-2012
    Location
    Telford, England
    MS-Off Ver
    Excel 2010 (2003)
    Posts
    294

    Re: Handling multiple txt files within single excel sheet

    Sound approach. That will give you the chart building VBA code.

    You may either produce the chart as you open each file or leave that and batch chart them when all the files have been opened.
    Batching allows you to know the range of values in the different files and fix the axis dimensions so they are all the same and allow good visual comparison (if desirable).

    Here is a functionthat asks the user to select a file (or files) to open:

    Public Function fncFileSelected(prompt As String, Optional myPath As String = "")
    Dim FD As FileDialog
    Dim WKB(10) As Workbook
    On Error GoTo Err_fncFileSelected
    Set FD = Application.FileDialog(msoFileDialogFilePicker)
    FD.AllowMultiSelect = False ' make this True to collect all at one prompt*
    FD.ButtonName = "Select " & prompt & " file(s)"
    FD.Filters.Clear
    FD.Add "Excel Files", "" * .xls * ""
    FD.Title = "Select needed files"

    If myPath <> "" Then ChDir (myPath)

    If FD.Show = True Then
    fncFileSelected = FD.SelectedItems()
    Else
    fncFileSelected = ""
    End If

    Set FD = Nothing

    Err_fncFileSelected:
    Exit Function
    End Function



    Here's how to call it. For my preference, open the files one at a time (facilitates different folders)
    if you choose to open the files in a single user prompt, you'll get an array of file details to unpack
    For x = 1 To upperlimit(WKB)
    myFile = fncFileSelected("File #" & x)
    If myFile = "" Then Exit For
    Set WKB(x) = Workbook.Open(FileMame:=myFile)
    Next

    Here add your recorded code for the chart...

    And follow that with

    Workbooks.Close True ' save them with the charts
    For x = 1 To x - 1: Set WKB(x) = Nothing: End Sub




    Best wishes

+ 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