Results 1 to 3 of 3

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

Threaded 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.

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