+ Reply to Thread
Results 1 to 5 of 5

Macro to load text file not working.

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    440

    Macro to load text file not working.

    Hi,

    I need to load a text file into an excel, but to paste all only in the column A and not having to split them out to other column.

    Here are the sample files:
    HELP.xlsm
    HELP.txt

    If anyone can tell me where is wrong?

    Thanks.
    Last edited by Leith Ross; 03-14-2014 at 05:01 PM.

  2. #2
    Valued Forum Contributor
    Join Date
    03-22-2013
    Location
    Australia,NSW, Wirrimbi
    MS-Off Ver
    Excel 2013
    Posts
    1,057

    Re: Macro to load text file not working.

    Hi..

    Try this.. change the file path to suit..

    Sub Test()
    Dim x
       x = Split(CreateObject("scripting.filesystemobject").opentextfile("C:\HELP.txt").readall, vbCrLf)
       Sheets("Original").Range("A1").Resize(UBound(x)).Value = Application.Transpose(x)
    End Sub
    or this with a File Browser Dialogue..
    Sub Test1()
     Dim x, fName
        fName = Application.GetOpenFilename("Text Files (*.txt),*.txt")
        If fName <> "False" Then
            x = Split(CreateObject("scripting.filesystemobject").opentextfile(fName).readall, vbCrLf)
            Sheets("Original").Range("A1").Resize(UBound(x)).Value = Application.Transpose(x)
        End If
    End Sub
    Last edited by apo; 03-14-2014 at 07:39 PM.

  3. #3
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,655

    Re: Macro to load text file not working.

    Try change
    Set sht = wb.Worksheets("Sheet1")
    to
    Set sht = wb.Worksheets(1)

  4. #4
    Forum Contributor
    Join Date
    01-16-2014
    Location
    Canada
    MS-Off Ver
    Excel 2007
    Posts
    440

    Re: Macro to load text file not working.

    The final code is:
    Sub Open_File()
       Dim wb                          As excel.Workbook
       Dim wsActive                    As excel.Worksheet
       Dim sht                         As excel.Worksheet
       Dim f                           As Object
    
       Set wsActive = ActiveSheet
       Set f = Application.FileDialog(3)
       f.AllowMultiSelect = False
       f.Show
    
       Set wb = Workbooks.Open(f.SelectedItems(1))
       Set sht = wb.Worksheets(1)
       wsActive.UsedRange.ClearContents
       LR = sht.UsedRange.SpecialCells(xlLastCell, xlNumbers).Row
       sht.Range("A1:L" & LR).Copy
       wsActive.Range("A1").PasteSpecial Paste:=xlPasteValues
       Application.CutCopyMode = False
       wb.Close False
    End Sub
    But how can I mod so if they cancel and dont load any file, it will prompt a message saying, you did not select any file?

    Thanks.

  5. #5
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,655

    Re: Macro to load text file not working.

    Try change
       f.Show
    to
    If Not f.Show Then
        MsgBox "you did not select any file?"
        Exit Sub
    End If

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Macro Working fine until last line of text file
    By masond3 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 02-17-2014, 12:10 PM
  2. VBA Importing Text File makes the file longer to load everytime
    By Hudas in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-15-2014, 11:26 AM
  3. How to load macro file Automatically in Excel
    By dalipsinghbisht in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-08-2013, 08:07 AM
  4. Opening a browse and load file box in a macro
    By Scott Calkins via OfficeKB.com in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-25-2005, 05:56 PM
  5. Replies: 0
    Last Post: 04-18-2005, 09:06 PM

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