copy text file data to a specific excel sheet via vba

dgehlod

New Member
Joined
Nov 14, 2014
Messages
10
hi,
i m making a utility and want to give import and export data facility. i have made code for export but for coding import data i am in trouble.
while exporting data from excel to txt user can use any name or destination of file so it will not defined. can you help me to give a vba code that open file dialog and select text file and copy the data in txt file and paste in a specific sheet.

i use this code to open dialog but how to copy data from selected text file and paste in specific sheet.

Call Application.FileDialog(msoFileDialogOpen _
).Filters.Add("Text Files Only", "*.txt")

Waiting for your answers.:help:
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
.
.

Try something like this:

Code:
 Sub ImportTextFile()
  Dim strDelimiter$
  Dim strLineOfData$
  Dim varFileName
  Dim varLineSplit
  Dim intFileNum%
  Dim lngLineCount&
  Dim wksDestination As Worksheet
  
  strDelimiter = Chr$(44) 'comma
  varFileName = Application.GetOpenFilename("Text Files (*.txt),*.txt")
  If varFileName = False Then Exit Sub
  Set wksDestination = Worksheets.Add
  
  intFileNum = FreeFile%
  Open varFileName For Input As #intFileNum
  Do Until EOF(intFileNum)
    lngLineCount = 1 + lngLineCount
    Line Input #intFileNum, strLineOfData
    varLineSplit = Split(strLineOfData, strDelimiter)
    wksDestination.Cells(lngLineCount, 1).Resize(, 1 + UBound(varLineSplit)) = varLineSplit
  Loop
  Close #intFileNum
End Sub
 
Upvote 0
thanks for reply but this code given data in one cell and i want it to be in tab delimited format

so i search and found this code and it works
Code:
Option ExplicitConst FILEPATH = "c:\"
 
Sub ImportData()
    Dim UserFilename As String
    Dim NewImport As Variant
     
     'ChDir FILEPATH
    ChDir "C:\"
    UserFilename = Application.GetOpenFilename '(filefilter:="Text Files(*.txt),*.txt", Title:="Text Files to Import")
    NewImport = "Text;" & UserFilename
     
    Sheets("dev").Select
    Range("A2").Select
    With ActiveSheet.QueryTables.Add(Connection:=NewImport, _
        Destination:=Range("A2"))
        .Name = "TabDelimitedFile"
        .FieldNames = True
        .RowNumbers = False
        .FillAdjacentFormulas = False
        .PreserveFormatting = True
        .RefreshOnFileOpen = False
        .RefreshStyle = xlInsertDeleteCells
        .SavePassword = False
        .SaveData = True
        .AdjustColumnWidth = True
        .RefreshPeriod = 0
        .TextFilePromptOnRefresh = False
        .TextFilePlatform = 437
        .TextFileStartRow = 1
        .TextFileParseType = xlDelimited
        .TextFileTextQualifier = xlTextQualifierDoubleQuote
        .TextFileTabDelimiter = True
         ' This is for importing four columns
        .TextFileColumnDataTypes = Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1)
         ' it would read ...=Array(1, 1, 1, 1, 1, 1, 1, etc.....) if you imported 7 or more columns
        .TextFileTrailingMinusNumbers = True
        .Refresh BackgroundQuery:=False
    End With
End Sub
 
Upvote 0
hi, the above code given is working but not working on hidden sheet. the code you had given can it work on hidden sheets with tab delimited format.
please give me reply
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,915
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top