+ Reply to Thread
Results 1 to 3 of 3

Transfer text file data to spreadsheet via vba

Hybrid View

Atom Transfer text file data to... 09-04-2016, 05:28 PM
AlphaFrog Re: Transfer text file data... 09-04-2016, 06:41 PM
Atom Re: Transfer text file data... 09-05-2016, 05:35 AM
  1. #1
    Forum Contributor
    Join Date
    08-05-2009
    Location
    Colorado Springs, CO
    MS-Off Ver
    Office 365
    Posts
    161

    Transfer text file data to spreadsheet via vba

    I am trying to selectively read a text file into a spreadsheet. Up until now I just copy and paste it in. This is fairly easy, but I'd like to be able to just load the parts of the file I want and have it load only the parts I want. The text file has a lot of extraneous information (180,000 lines) and I need less than 100. I have written some code to read the file and capture only the lines I want. Having done that, I want to transfer parts of the captured data into a range in a spreadsheet. Here's the code I have so far:

    Sub ReadLSTFile()
    Dim myFile As String, text As String, textline As String, posLat As Integer, posLong As Integer
    Dim i As Integer
    Dim LOC As String
    Dim Unit As String
    Dim PERIOD As String
    Dim LineParsed As Variant
    Dim ColName
    Dim MyData As Variant
    Dim FindColumns As Integer
    
    myFile = ActiveSheet.Range("LSTFileName")
    LOC = Range("LOC") + Range("LOC")
    Unit = Range("Unit")
    PERIOD = Range("PERIOD")
    LOC = LOC + PERIOD + Unit
    Open myFile For Input As #1
    Do Until EOF(1)
    Line Input #1, text
    If InStr(1, text, "Columns Section") > 0 Then FindColumns = FindColumns + 1
    
        If Mid(text, 2, 1) = "C" And FindColumns = 2 And InStr(text, LOC) > 0 Then
        LineParsed = SplitEx(text, " ", Chr(34), True)
        If LineParsed(5) <> 0 Then Debug.Print text
        
        End If
       
        
    
    
    Loop
    Close #1
    
    End Sub
    This is what the print statement produces:

    C    460  CCISRULnA         BS     26.859595             0            -0
     C    462  CCISRUlhC         BS     82.012188             0             0
     C    463  CCISRU..0         BS    121.888497             0            -0
     C    464  CCISRULTD         BS     26.859595             0             0
     C    466  CCISRUhtD         BS     82.012188             0             0
     C    467  CCISRUL9D         BS     25.031633             0            -0
     C    472  CCISRUh9D         BS     62.273607             0             0
     C    475  CCISRUSP1         BS      5.536804             0            -0
     C    476  CCISRUSA1         BS      0.749063             0             0
     C    477  CCISRUSI1         BS      1.874372             0             0
     C    478  CCISRUSN1         BS      0.713030             0             0
     C    479  CCISRUSC1         BS      0.111416             0             0
     C    480  CCISRUSH1         BS      1.072207             0             0
     C    481  CCISRUSB1         BS      0.275186             0             0
     C    484  CCISRUhP1         BS     12.941670             0            -0
     C    485  CCISRUhA1         BS      1.521523             0             0
     C    486  CCISRUd11         BS    180.620594             0             0
     C    488  CCISRUd31         BS     73.606563             0             0
     C    490  CCISRUh8D         BS     15.131447             0             0
     C    491  CCISRUDMY         BS     91.600996             0             0
     C    492  CCISRUHtM         BS    189.015874             0             0
     C    493  CCISRUnnA         BS     13.016714             0             0
     C    495  CCISRUh2C         BS     13.016714             0             0
     C    497  CCISRUhaa         BS      5.533621             0             0
     C    498  CCISRUhbb         BS     16.896174             0             0
     C    499  CCISRUHMa         BS      2.681707             0             0
     C    500  CCISRUHmb         BS     16.896174             0             0
     C    501  CCISRU85R         BS     13.920931             0             0
     C    502  CCISRU90R         BS     89.085954             0             0
    So far, so good. I just want the 3rd and 5th columns of data to go on the spreadsheet. I know I can get this done by brute force, but it seems like there must be a simple way to transfer the data to an array and plug it into the spreadsheet, and I'd like to know what that might be without have to reinvent the wheel.

    Thanks.

  2. #2
    Forum Guru
    Join Date
    07-25-2011
    Location
    Florida
    MS-Off Ver
    Excel 2003
    Posts
    9,653

    Re: Transfer text file data to spreadsheet via vba

    Try something like this (not tested)...

    Sub ReadLSTFile()
        Dim myFile As String, text As String, textline As String, posLat As Integer, posLong As Integer
        Dim i         As Long
        Dim arr(1 To 100, 1 To 2) As Variant
        Dim LOC       As String
        Dim Unit      As String
        Dim PERIOD    As String
        Dim LineParsed As Variant
        Dim ColName
        Dim MyData    As Variant
        Dim FindColumns As Integer
        
        myFile = ActiveSheet.Range("LSTFileName")
        LOC = Range("LOC") + Range("LOC")
        Unit = Range("Unit")
        PERIOD = Range("PERIOD")
        LOC = LOC + PERIOD + Unit
        Open myFile For Input As #1
        
        Do Until EOF(1)
            Line Input #1, text
            If InStr(1, text, "Columns Section") > 0 Then FindColumns = FindColumns + 1
        
            If Mid(text, 2, 1) = "C" And FindColumns = 2 And InStr(text, LOC) > 0 Then
                LineParsed = SplitEx(text, " ", Chr(34), True)
                If LineParsed(5) <> 0 Then 
                    Debug.Print text
                    i = i + 1
                    arr(i, 1) = LineParsed(2)
                    arr(i, 2) = LineParsed(5)
                 End If
            End If
        
        Loop
        Close #1
        
        Range("A1").Resize(i, 2).Value = arr
        
    End Sub
    Last edited by AlphaFrog; 09-04-2016 at 06:47 PM.
    Surround your VBA code with CODE tags e.g.;
    [CODE]your VBA code here[/CODE]
    The # button in the forum editor will apply CODE tags around your selected text.

  3. #3
    Forum Contributor
    Join Date
    08-05-2009
    Location
    Colorado Springs, CO
    MS-Off Ver
    Office 365
    Posts
    161

    Re: Transfer text file data to spreadsheet via vba

    Worked great. 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. VB to use data from spreadsheet and input box to create new folder and text file
    By cmccabe in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-19-2015, 05:40 PM
  2. [SOLVED] extracting data from text file and put it into spreadsheet
    By El Conquistador in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 03-27-2013, 08:27 AM
  3. Replies: 3
    Last Post: 01-09-2012, 09:04 PM
  4. Excel to text file transfer of data.
    By ramanan256 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-10-2010, 07:50 PM
  5. transfer excel file to text file format
    By tllcll in forum Excel General
    Replies: 1
    Last Post: 07-17-2006, 07:55 AM
  6. Transfer text to another spreadsheet
    By Susan in forum Excel - New Users/Basics
    Replies: 12
    Last Post: 03-23-2006, 06:06 PM
  7. Creating a text file and printing excel spreadsheet data onto it
    By Brett Smith in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 12-22-2005, 12:25 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