Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 36
Default Opening a txt file

Is there a way to create a button that open a text file with the same name
all the time and bypass the additional dialog boxes

Thanks
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Opening a txt file

What kind of text file are we talking about... plain text, CSV file,
something else?

How or where did you want to open it... in a TextBox, MessageBox, single
cell, multiple cells, etc.?

Rick


"Novice Lee" wrote in message
...
Is there a way to create a button that open a text file with the same name
all the time and bypass the additional dialog boxes

Thanks


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 36
Default Opening a txt file

I guess you can call it plain text ( I can open it in Notepad). When I select
the file the text import wizard pops up, I hit next twice then finish. and
there the file is. Each piece of data in it's own cell.

I was also thinking about get rid of < in alot of the cells, but that for
another day

Thanks


"Rick Rothstein (MVP - VB)" wrote:

What kind of text file are we talking about... plain text, CSV file,
something else?

How or where did you want to open it... in a TextBox, MessageBox, single
cell, multiple cells, etc.?

Rick


"Novice Lee" wrote in message
...
Is there a way to create a button that open a text file with the same name
all the time and bypass the additional dialog boxes

Thanks



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,836
Default Opening a txt file

Can you use this?
Public Sub DoTheImport()
Dim FName As Variant
Dim Sep As String

FName = Application.GetOpenFilename _
(filefilter:="Text Files(*.txt),*.txt,All Files (*.*),*.*")
If FName = False Then
MsgBox "You didn't select a file"
Exit Sub
End If

Sep = InputBox("Enter a single delimiter character.", _
"Import Text File")
ImportTextFile CStr(FName), Sep

End Sub



Public Sub ImportTextFile(FName As String, Sep As String)

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

Application.ScreenUpdating = False
'On Error GoTo EndMacro:

SaveColNdx = ActiveCell.Column
RowNdx = ActiveCell.Row

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

Is it dynamic, not static. I will give you a lot more control of your
import process (you search for the file that you want to import, you select
the method of delimitation, etc.)



Regards,
Ryan---

--
RyGuy


"Novice Lee" wrote:

I guess you can call it plain text ( I can open it in Notepad). When I select
the file the text import wizard pops up, I hit next twice then finish. and
there the file is. Each piece of data in it's own cell.

I was also thinking about get rid of < in alot of the cells, but that for
another day

Thanks


"Rick Rothstein (MVP - VB)" wrote:

What kind of text file are we talking about... plain text, CSV file,
something else?

How or where did you want to open it... in a TextBox, MessageBox, single
cell, multiple cells, etc.?

Rick


"Novice Lee" wrote in message
...
Is there a way to create a button that open a text file with the same name
all the time and bypass the additional dialog boxes

Thanks



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 36
Default Opening a txt file

Thanks for the code, I tried the code. What do I put for the delimiter? I
used "," and it put everything from line one of the text file into cell a1,
next line into b1 and so on. am I doing something wrong?
thanks

"ryguy7272" wrote:

Can you use this?
Public Sub DoTheImport()
Dim FName As Variant
Dim Sep As String

FName = Application.GetOpenFilename _
(filefilter:="Text Files(*.txt),*.txt,All Files (*.*),*.*")
If FName = False Then
MsgBox "You didn't select a file"
Exit Sub
End If

Sep = InputBox("Enter a single delimiter character.", _
"Import Text File")
ImportTextFile CStr(FName), Sep

End Sub



Public Sub ImportTextFile(FName As String, Sep As String)

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

Application.ScreenUpdating = False
'On Error GoTo EndMacro:

SaveColNdx = ActiveCell.Column
RowNdx = ActiveCell.Row

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

Is it dynamic, not static. I will give you a lot more control of your
import process (you search for the file that you want to import, you select
the method of delimitation, etc.)



Regards,
Ryan---

--
RyGuy


"Novice Lee" wrote:

I guess you can call it plain text ( I can open it in Notepad). When I select
the file the text import wizard pops up, I hit next twice then finish. and
there the file is. Each piece of data in it's own cell.

I was also thinking about get rid of < in alot of the cells, but that for
another day

Thanks


"Rick Rothstein (MVP - VB)" wrote:

What kind of text file are we talking about... plain text, CSV file,
something else?

How or where did you want to open it... in a TextBox, MessageBox, single
cell, multiple cells, etc.?

Rick


"Novice Lee" wrote in message
...
Is there a way to create a button that open a text file with the same name
all the time and bypass the additional dialog boxes

Thanks


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
opening a file in Excel starts application but dose not open file Bob Shelton Excel Discussion (Misc queries) 1 July 2nd 08 07:51 PM
Opening file in Excel 2003 opens multipule instances of same file Ed_B Excel Discussion (Misc queries) 1 June 21st 07 07:10 PM
File:1 and File:2 -- Double Files when Opening One File dallin Excel Discussion (Misc queries) 1 January 25th 07 02:53 AM
opening an excel file opens a duplicate file of the same file skm Excel Discussion (Misc queries) 1 December 7th 05 05:52 PM
Error:Invalid File format,while opening an Excel Template file Saurabh Excel Programming 1 January 17th 05 07:15 AM


All times are GMT +1. The time now is 02:17 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"