+ Reply to Thread
Results 1 to 3 of 3

VBA script to import text data in the first empty column of a range

Hybrid View

  1. #1
    Registered User
    Join Date
    06-08-2021
    Location
    Italy
    MS-Off Ver
    2013
    Posts
    10

    VBA script to import text data in the first empty column of a range

    Hello to all of you,
    I created a VBA script to import a text file in a specif spreadsheet columns range (AK5:AR44) and I would like to make it more efficient with following modifications for which I may need your help:

    Use as Destination:=Range the first empty column in the predefined range
    Create a sort of loop, after each file is imported, using a pop up window, user should be able to decide whether to import another file to the first new empty column or to stop the import.
    Below the code I have create until now, unfortunalety I wasn't able to add a sort of dynamic destination for the newly imported data.
    Thanks in advance for your help.

     Sub Import_samples_data_rep()
    Dim Sample1_repfile As String
        MsgBox "Select text file", vbOKOnly
        Sample1_repfile = Application.GetOpenFilename("Text Files (*.txt), *.txt")
    Dim Stuff
    'assign variables
     On Error GoTo ErrHandler:
    ErrHandler: If Err.Number = 1004 Then
    ErrMsg = Error(Err.Number)
    Exit Sub
    End If
        With ActiveSheet.QueryTables.Add(Connection:= _
            "TEXT;" & Sample1_repfile, Destination:=Range("$AK$5"))
            .Name = "*.txt"
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = False
            .RefreshPeriod = 0
            .TextFilePromptOnRefresh = False
            .TextFilePlatform = 850
            .TextFileStartRow = 14
            .TextFileParseType = xlDelimited
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = False
            .TextFileTabDelimiter = False
            .TextFileSemicolonDelimiter = False
            .TextFileCommaDelimiter = True
            .TextFileSpaceDelimiter = False
            .TextFileColumnDataTypes = Array(9, 9, 1, 9)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
        End With
    Dim answer As Integer
    answer = MsgBox("Import another file?", vbQuestion + vbYesNo + vbDefaultButton2, "Import again")
    If answer = vbYes Then
    Sample2_repfile = Application.GetOpenFilename("Text Files (*.txt), *.txt")
        With ActiveSheet.QueryTables.Add(Connection:= _
            "TEXT;" & Sample2_repfile, Destination:=Range("$AL$5"))
            .Name = "*.txt"
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = False
            .RefreshPeriod = 0
            .TextFilePromptOnRefresh = False
            .TextFilePlatform = 850
            .TextFileStartRow = 14
            .TextFileParseType = xlDelimited
            .TextFileTextQualifier = xlTextQualifierDoubleQuote
            .TextFileConsecutiveDelimiter = False
            .TextFileTabDelimiter = False
            .TextFileSemicolonDelimiter = False
            .TextFileCommaDelimiter = True
            .TextFileSpaceDelimiter = False
            .TextFileColumnDataTypes = Array(9, 9, 1, 9)
            .TextFileTrailingMinusNumbers = True
            .Refresh BackgroundQuery:=False
        End With
    Else
    MsgBox "File import ended", vbOKOnly
    End If
    End Sub
    Last edited by JS85; 06-11-2021 at 04:42 AM.

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,662

    Re: VBA script to import text data in the first empty column of a range

    Sub Import_samples_data_rep()
        Dim Sample1_repfile As String
        Dim Stuff
        Dim answer As Long
        Dim DestColumn As Variant
        
        
        MsgBox "Select text file", vbOKOnly
        'assign variables
        Do 
            Set DestColumn = Range("as4").End(xlToLeft).Offset(ColumnOffset:=1)
            DestColumn = DestColumn.Address
            
            Sample1_repfile = Application.GetOpenFilename("Text Files (*.txt), *.txt")
            
            With ActiveSheet.QueryTables.Add( _
                    Connection:="TEXT;" & Sample1_repfile, _
                    Destination:=Range( DestColumn))
                .Name = "*.txt"
                .FieldNames = True
                .RowNumbers = False
                .FillAdjacentFormulas = False
                .PreserveFormatting = True
                .RefreshOnFileOpen = False
                .RefreshStyle = xlInsertDeleteCells
                .SavePassword = False
                .SaveData = True
                .AdjustColumnWidth = False
                .RefreshPeriod = 0
                .TextFilePromptOnRefresh = False
                .TextFilePlatform = 850
                .TextFileStartRow = 14
                .TextFileParseType = xlDelimited
                .TextFileTextQualifier = xlTextQualifierDoubleQuote
                .TextFileConsecutiveDelimiter = False
                .TextFileTabDelimiter = False
                .TextFileSemicolonDelimiter = False
                .TextFileCommaDelimiter = True
                .TextFileSpaceDelimiter = False
                .TextFileColumnDataTypes = Array(9, 9, 1, 9)
                .TextFileTrailingMinusNumbers = True
                .Refresh BackgroundQuery:=False
            End With
            answer = MsgBox("Import another file?", vbQuestion + vbYesNo + vbDefaultButton2, "Import again")
            Loop While answer = vbYes 
        MsgBox "File import ended", vbOKOnly
    End Sub
    Last edited by protonLeah; 06-12-2021 at 01:05 PM.
    Ben Van Johnson

  3. #3
    Registered User
    Join Date
    06-08-2021
    Location
    Italy
    MS-Off Ver
    2013
    Posts
    10

    Re: VBA script to import text data in the first empty column of a range

    Hello protonLeah,
    thanks a lot for your reply and sorry for my very late feedback but I had time only today to look a that issue.
    I modified the script according to your suggestion and I have one question concerning the range setup.
    Assuming my range to import the data is AB5:AQ5 the script should be like

    Set DestColumn = Range("AB5:AQ5").End(xlToLeft).Offset(ColumnOffset:=1)

    If yes, this does not work as expected, the first file is imported correctly but the second one is causing another column creation messing up the column order.
    Thanks.

+ 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. Replies: 3
    Last Post: 01-09-2021, 04:51 PM
  2. Script Error while trying to import Web data
    By Eitan Halperin in forum Excel General
    Replies: 1
    Last Post: 12-14-2020, 03:30 AM
  3. [SOLVED] Getting Excel (via VBA Script) to import multiple textpad files in the next empty cell.
    By red_dot in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-02-2013, 05:49 AM
  4. [SOLVED] Program a conditional script that will delete entire row if cell in column B is empty
    By Rabbitstew in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-17-2012, 01:54 PM
  5. [SOLVED] Import Text File VBA Script
    By aharb in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 05-17-2012, 12:00 PM
  6. Macro broken - copy data range column to empty data range column
    By Cascus in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 10-06-2011, 05:20 PM
  7. How to Import two consecutive text files in same column. No empty rows between.
    By zeroJames in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-20-2011, 03:44 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