Results 1 to 8 of 8

Macro to combine a Text File & Excel file into one Separate workbook

Threaded View

bdouglas1011 Macro to combine a Text File... 08-28-2014, 07:37 AM
Bernie Deitrick Re: Macro to combine a Text... 08-28-2014, 10:00 AM
bdouglas1011 Re: Macro to combine a Text... 08-28-2014, 11:44 AM
Bernie Deitrick Re: Macro to combine a Text... 08-28-2014, 12:57 PM
bdouglas1011 Re: Macro to combine a Text... 08-28-2014, 02:08 PM
bdouglas1011 Re: Macro to combine a Text... 08-28-2014, 03:43 PM
Bernie Deitrick Re: Macro to combine a Text... 08-29-2014, 08:37 AM
bdouglas1011 Re: Macro to combine a Text... 08-29-2014, 03:42 PM
  1. #1
    Forum Contributor
    Join Date
    10-02-2013
    Location
    Houston, TX
    MS-Off Ver
    office 365
    Posts
    648

    Macro to combine a Text File & Excel file into one Separate workbook

    I have 2 Files that I would like to combine into one workbook- each having its own TAB AT BOTTOM. I would like to open a excel spreadsheet named "Comparison" and have a button "Macro" that when chosen it would ask for the first file to import and then you would select the Text File (Pason Data Run 1) and it would import it into its own tab with the excel sheet.

    One of the problems I have when importing the text file is 2 of the columns I would like married together as one. ( Date & Time ) on the new worksheet and the other data in there own respective columns
    I would like the date and time to read like ( ex: "8/12/14 06:01" )

    I have included an excel sheet showing (Pason data run 1) after I imported it manually - notice that column (a,b,c) are highlighted Coulumn 'A' would not format as date so I had to create column 'B' and manually enter in the dates then in Column 'D' I combine column (C & B) to make my date and time format.

    then you could run another macro for the Excel sheet (well Guide Run 1) to import into its own tab with in the new workbook.

    I use both Files (Pason Data run 1 & Well guide Run 1) to run a correlation log and I need the date and times the same for that to happen.

    This is some code I have partially working
    Sub CombineTextFiles()
        Dim FilesToOpen
        Dim x As Integer
        Dim wkbAll As Workbook
        Dim wkbTemp As Workbook
        Dim sDelimiter As String
    
        On Error GoTo ErrHandler
        Application.ScreenUpdating = False
    
        'sDelimiter = "|"
    
        FilesToOpen = Application.GetOpenFilename _
          (FileFilter:="Text Files (*.txt), *.txt", _
          MultiSelect:=True, Title:="Text Files to Open")
    
        If TypeName(FilesToOpen) = "Boolean" Then
            MsgBox "No Files were selected"
            GoTo ExitHandler
        End If
    
        x = 1
        Set wkbTemp = Workbooks.Open(Filename:=FilesToOpen(x))
        wkbTemp.Sheets(1).Copy
        Set wkbAll = ActiveWorkbook
        wkbTemp.Close (False)
        wkbAll.Worksheets(x).Columns("A:A").TextToColumns _
          Destination:=Range("A1"), DataType:=xlDelimited, _
          TextQualifier:=xlDoubleQuote, _
            ConsecutiveDelimiter:=False, _
            Tab:=False, Semicolon:=False, _
            Comma:=True, Space:=False, _
            Other:=False, FieldInfo:=Array(Array(1, 5), _
            Array(2, 1)), TrailingMinusNumbers:=True
            x = x + 1
    
        While x <= UBound(FilesToOpen)
            Set wkbTemp = Workbooks.Open(Filename:=FilesToOpen(x))
            With wkbAll
            wkbTemp.Sheets(1).Move After:=.Sheets(.Sheets.Count)
            .Worksheets(x).Columns("A:A").TextToColumns _
            Destination:=Range("A1"), DataType:=xlDelimited, _
          TextQualifier:=xlDoubleQuote, _
            ConsecutiveDelimiter:=False, _
            Tab:=False, Semicolon:=False, _
            Comma:=True, Space:=False, _
            Other:=False, FieldInfo:=Array(Array(1, 5), _
            Array(2, 1)), TrailingMinusNumbers:=True
            End With
            x = x + 1
        Wend
    
    ExitHandler:
        Application.ScreenUpdating = True
        Set wkbAll = Nothing
        Set wkbTemp = Nothing
        Exit Sub
    
    ErrHandler:
        MsgBox Err.Description
        Resume ExitHandler
    End Sub
    Once that imports I go into the first text file that got imported and I have to combine 2 of the cells to have the date & time together. I used this code to start but I do not know how to get the format to carry all the way down the sheet.
    Sub CombineDateTime()
        Columns("C:C").Select
        Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
        Range("C2").Select
        ActiveCell.FormulaR1C1 = "=RC[-2]+RC[-1]"
        Range("C2").Select
        Selection.NumberFormat = "dd-mmm-yy  hh:mm"
    End Sub
    It would be great if I could add this somehow to the first original text file that is imported and have it do this seamlessly.
    Also How can I get the Macro to Paste the 2 files into the existing open workbook and not start a new one
    Last edited by bdouglas1011; 08-28-2014 at 10:07 AM. Reason: Added some code I am trying

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Macro to combine a Text File & Excel file into one Separate workbook
    By bdouglas1011 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 08-28-2014, 03:23 AM
  2. Replies: 2
    Last Post: 04-12-2013, 07:56 PM
  3. Replies: 1
    Last Post: 02-23-2013, 08:36 AM
  4. Macro button to separate workbook as a file name referenced from cell.
    By Moonpie in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 08-09-2012, 12:19 PM
  5. Can you e-mail separate sheets in an Excel Workbook file?
    By FruityPebbles in forum Excel General
    Replies: 5
    Last Post: 04-01-2012, 03:05 AM

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