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

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying

Marc L

Well-known Member
Joined
Apr 5, 2021
Messages
1,769
Office Version
  1. 2010
Platform
  1. Windows
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 …​
 

Domenic

MrExcel MVP
Joined
Mar 10, 2004
Messages
19,933
Office Version
  1. 365
Platform
  1. Windows
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!
 
Master Excel Bundle

Excel contains over 450 functions, with more added every year. That’s a huge number, so where should you start? Right here with this bundle.

Forum statistics

Threads
1,152,126
Messages
5,768,264
Members
425,460
Latest member
Astros1243

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
Top