Automating import of txt file

sc58963

New Member
Joined
May 8, 2021
Messages
1
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Hi everyone, I am very new to Excel VBA and am trying to figure out how to import all the data in my txt file into excel to automate the process. As of right now, it is only importing the first three lines but I am unsure how I can alter it so it imports all the data. Here is the code I currently have as well as an image of the txt file I am trying to import (the data is tab delimited and each line contains a different number of tabs)
txt.JPG


VBA Code:
Sub ImportText()

    Dim fileToOpen As Variant
    Dim fileFilterPattern As String
    Dim wsMaster As Worksheet
    Dim wbTextImport As Workbook
    
    Application.ScreenUpdating = True
    
    fileFilterPattern = "Text Files (*.txt; *.csv), *.txt; *.csv"
 
    
    fileToOpen = Application.GetOpenFilename(fileFilterPattern)
    
    
    If fileToOpen = False Then
            ' Input cancelled
            MsgBox "No file selected."
        Else
            ' we have a file to open
    
    Workbooks.OpenText _
                Filename:=fileToOpen, _
                StartRow:=1, _
                DataType:=xlDelimited, _
                Tab:=True
                
      
        Set wbTextImport = ActiveWorkbook
        
        'Set wsMaster = ThisWorkbook.Worksheets.Add
        Set wsMaster = ThisWorkbook.Worksheets("Data")
        
        wbTextImport.Worksheets(1).Range("A1").CurrentRegion.Copy wsMaster.Range("A3")
        
        wbTextImport.Close False
    
    End If
    
    Application.ScreenUpdating = True
 

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.
Hi, without any attachment I just can say you must use the Import From File Excel feature in the Data tab​
and just well answer to the Import Wizard …​
Or according to your actual VBA procedure you can add a TextToColumns statement, to see in VBA help or to try from the same Data tab …​
 
Upvote 0
That's because you're copying the current region, which is bound by a blank row. Try using the UsedRange property of the Worksheet object instead...

VBA Code:
wbTextImport.Worksheets(1).UsedRange.Copy wsMaster.Range("A3")

Hope this helps!
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

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