Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
| Search | Today's Posts | Mark Forums Read
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old June 28th, 2007, 09:48 AM
Friend of Wrox
 
Join Date: May 2006
Location: Jonesboro, AR, USA.
Posts: 144
Thanks: 0
Thanked 0 Times in 0 Posts
Default importing text file into excel

Hi,
  I'm hoping someone can point me in the right direction on this. I've done it a few times in access, but its a little different in excel.
  There's a text file on the network that is updated daily via the mainframe. I need to automate an excel workbook to import this text file. I've been trying to use the GetObject function to bring in the text file and store it in a variable for manipulation, but it seems to be a little different than with access.
  What is the best way to go about doing this?

Thanks,
David

  #2 (permalink)  
Old June 29th, 2007, 01:51 AM
Friend of Wrox
Points: 3,060, Level: 23
Points: 3,060, Level: 23 Points: 3,060, Level: 23 Points: 3,060, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2005
Location: , , .
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

It would be bettter to have that as a query table

Sub TXT_QueryTable()

Dim ConnString As String

Dim qt As QueryTable

ConnString = "TEXT;C:\Temp.txt"


Set qt = Worksheets(1).QueryTables.Add(Connection:=ConnStri ng, _
        Destination:=Range("B1"))

qt.Refresh



End Sub

This will apply if you have the same text file that is getting appended. If it changes then change the Connection string

Also you can refresh the Excel every time it gets opened using qt.Refresh


Private Sub Workbook_Open()
Dim qt As QueryTable
Set qt = ActiveWorkbook.Sheets(1).QueryTable(1)
qt.Refresh

End Sub

Cheers
Shasur


http://www.vbadud.blogspot.com
  #3 (permalink)  
Old July 10th, 2007, 11:28 AM
Friend of Wrox
 
Join Date: May 2006
Location: Jonesboro, AR, USA.
Posts: 144
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the reply Shasur!
I tried to use the above code, but never got it going quite right.
I did find a way to do import the text file though, using the code below.
Code:
Public Sub ImportTextFile()
    Dim RowNdx As Long
    Dim ColNdx As Integer
    Dim TempVal As Variant
    Dim WholeLine As String
    Dim Pos As Integer
    Dim NextPos As Integer
    Dim SaveColNdx As Integer
    Dim Sep As String
    Dim FName As String

    Cells.Select
    Range("A2:V3500").Select
    Selection.ClearContents

    FName = "Y:\INCOMING\sorter1.txt"
    Sep = ","

    Application.ScreenUpdating = False
    'On Error GoTo EndMacro:

    SaveColNdx = 1
    RowNdx = 2

    Open FName For Input Access Read As #1

    While Not EOF(1)
        Line Input #1, WholeLine
        If Right(WholeLine, 1) <> Sep Then
            WholeLine = WholeLine & Sep
        End If
        ColNdx = SaveColNdx
        Pos = 1
        NextPos = InStr(Pos, WholeLine, Sep)
        While NextPos >= 1
            TempVal = Mid(WholeLine, Pos, NextPos - Pos)
            Cells(RowNdx, ColNdx).Value = TempVal
            Pos = NextPos + 1
            ColNdx = ColNdx + 1
            NextPos = InStr(Pos, WholeLine, Sep)
        Wend
        RowNdx = RowNdx + 1
    Wend

EndMacro:
    On Error GoTo 0
    Application.ScreenUpdating = True
    Close #1

End Sub
The only problem I'm having now is that some of the fields have quotes. I need to get rid of the quotes. How can I accomplish this?

Thanks,
Dave



Similar Threads
Thread Thread Starter Forum Replies Last Post
Importing text file into excel anamarijaf Excel VBA 4 January 27th, 2007 05:50 AM
Importing a text file DBN Beginning VB 6 6 April 24th, 2006 02:52 PM
Importing data from a text file nav PHP How-To 2 March 7th, 2005 02:42 PM
Importing a text file to excel using VB ridders Excel VBA 1 April 10th, 2004 03:01 PM
Problem importing text file manuel Access 3 April 7th, 2004 09:36 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.