+ Reply to Thread
Results 1 to 3 of 3

VBA Script to automatically choose delimiter options on import

Hybrid View

  1. #1
    Registered User
    Join Date
    07-04-2013
    Location
    Calgary
    MS-Off Ver
    Excel 2010
    Posts
    36

    VBA Script to automatically choose delimiter options on import

    Good afternoon, this is my first post, thanks for having me!

    I am looking at importing text files into a workbook that happen to all be space delimited. I have macro assigned to a button that opens up a file prompt to choose the correct file and it works great, however I then have to manually switch it from TAB to Space delimited and click OK to complete the import. I recorded a macro of me importing and selecting the options I would like and it looks like the capability is there to run through the options however when I tried to implement it didn't work (mainly because I don't really understand where it should land.

    Below is the recorded Macro:

    ChDir "C:\Users\*****\Documents\Gauge Recorder Files\*******"
        Workbooks.OpenText Filename:= _
            "C:\Users\*****\Documents\Gauge Recorder Files\*****\bottomrecorder.rec", _
            Origin:=xlMSDOS, StartRow:=1, DataType:=xlDelimited, TextQualifier:= _
            xlDoubleQuote, ConsecutiveDelimiter:=True, Tab:=False, Semicolon:=False, _
            Comma:=False, Space:=True, Other:=False, FieldInfo:=Array(Array(1, 1), _
            Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1), Array(8, 1), _
            Array(9, 1), Array(10, 1), Array(11, 1), Array(12, 1)), TrailingMinusNumbers:=True
    And here is the current macro that I use for importing.

    Sub Import_TG_Raw()
    '
    ' Import_TG_Raw Macro
    '
    
        Dim DestBook As Workbook, SourceBook As Workbook
        Dim DestCell As Range
        Dim RetVal As Boolean
    
        ' Turn off screen updating.
        Application.ScreenUpdating = False
        
        ' Set active window for bottom raw
        ActiveWorkbook.Sheets("Top Raw").Activate
        
        ' Set object variables for the active book and active cell.
        Set DestBook = ActiveWorkbook
        Set DestCell = ActiveCell
    
        ' Show the Open dialog box.
        RetVal = Application.Dialogs(xlDialogOpen).Show("*.*")
            
        ' If Retval is false (Open dialog canceled), exit the procedure.
        If RetVal = False Then Exit Sub
    
        ' Set an object variable for the workbook containing the text file.
        Set SourceBook = ActiveWorkbook
    
        ' Copy the contents of the entire sheet containing the text file.
        Range(Range("A1"), Range("A1").SpecialCells(xlLastCell)).Copy
    
        ' Activate the destination workbook and paste special the values
        ' from the text file.
        DestBook.Activate
        DestCell.pastespecial Paste:=xlValues
    
        ' Close the book containing the text file.
        SourceBook.Close False
    
    End Sub
    Any ideas as to how this might fit in?

  2. #2
    Forum Expert tigeravatar's Avatar
    Join Date
    03-25-2011
    Location
    Colorado, USA
    MS-Off Ver
    Excel 2003 - 2013
    Posts
    5,361

    Re: VBA Script to automatically choose delimiter options on import

    Canuckle777,

    Welcome to the forum!
    Here's one way...
    Sub tgr()
        
        Dim oFSO As Object
        Dim strFilePath As String
        Dim arrText() As String
        
        strFilePath = Application.GetOpenFilename("Text Files, *.txt")
        If strFilePath = "False" Then Exit Sub  'Pressed cancel
        
        Set oFSO = CreateObject("Scripting.FileSystemObject")
        arrText = Split(oFSO.OpenTextFile(strFilePath).ReadAll, vbCrLf)
        
        With ActiveCell.Resize(UBound(arrText) - LBound(arrText) + 1)
            .Value = Application.Transpose(arrText)
            .TextToColumns .Cells, xlDelimited, xlTextQualifierDoubleQuote, True, Space:=True
        End With
        
        Set oFSO = Nothing
        Erase arrText
        
    End Sub
    Hope that helps,
    ~tigeravatar

    Forum Rules: How to use code tags, mark a thread solved, and keep yourself out of trouble

  3. #3
    Registered User
    Join Date
    07-04-2013
    Location
    Calgary
    MS-Off Ver
    Excel 2010
    Posts
    36

    Re: VBA Script to automatically choose delimiter options on import

    Thanks for the welcome Tigeravatar! I think idea is there. I am going to attempt to pull this data out:
    .TextToColumns .Cells, xlDelimited, xlTextQualifierDoubleQuote, True, Space:=True
    and see if I can get it to work with my above macro.

    I appreciate it!

+ 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] How to import multiple txt file and arrange by column with delimiter
    By warlock89 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-28-2012, 12:50 AM
  2. Import multiple text files into different sheets + delimiter
    By zijin_cheng in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 08-24-2012, 08:57 AM
  3. Remove semi-colon delimiter from import-text code
    By ron2k_1 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-17-2011, 11:20 AM
  4. choose 1 options then 1 cell hidden
    By lunar_star in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-18-2007, 07:35 AM
  5. Change delimiter in import function from comma to tab
    By marksince1984 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-13-2006, 12:40 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