Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Input Box selection and file open

My pleasure... I'm glad this all worked out for you.

Remember, by the way, that the "general" solution requires the "junk" at the
beginning of the file to **never** have a line start with 7 digits followed
by a space. If you are guaranteed that, then the general solution will work
even if the number of lines of "junk" should ever change in the future.

Rick


"cottage6" wrote in message
...
Hey Rick, thanks for both solutions. I've tried both of them and they
work
great. I'm sure I'll use both methods in the future, and it always helps
to
get several solutions. That's the only way I ever learn. Again, thanks
for
all your help!

"Rick Rothstein (MVP - VB)" wrote:

Here is a possibly more general solution... if the "junk" in the front of
the file **never** can start with 7 digits followed by a space, then you
can
use the following replacement Import_PTC subroutine to read the file (it
will skip over any number of lines of "junk" in the front of the file,
not
just 45 of them).

Rick

Sub Import_PTC()
Dim X As Long
Dim Z As Long
Dim P As Long
Dim FileNum As Long
Dim JunkCount As Long
Dim Entry As Variant
Dim FieldWidths As Variant
Dim TotalFile As String
Dim Records() As String
FileNum = FreeFile
FieldWidths = Array(7, 26, 12, 5, 9, 10, 10, 15, 10, 15, 12)
Open Path & ListBox1.Text For Binary As #FileNum
TotalFile = Space(LOF(FileNum))
Get #FileNum, , TotalFile
Close #FileNum
Records = Split(TotalFile, vbCrLf)
With ActiveSheet
For X = 0 To UBound(Records)
If Records(X) Like "####### *" Then
P = 1
For Z = LBound(FieldWidths) To UBound(FieldWidths)
Entry = Mid$(Records(X), P, FieldWidths(Z))
If Z - LBound(FieldWidths) = 2 Then
Entry = "'" & Entry
End If
Cells(X + 1 - JunkCount, Z + 1 - LBound(FieldWidths)).Value =
Entry
P = P + FieldWidths(Z)
Next
Else
JunkCount = JunkCount + 1
End If
Next
End With
End Sub



"Rick Rothstein (MVP - VB)" wrote
in
message ...
The replacement Import_PTC subroutine below will skip over the first 44
lines of your file and begin outputting the 45th line onward starting
at
Row 1.

Rick

Sub Import_PTC()
Dim X As Long
Dim Z As Long
Dim P As Long
Dim FileNum As Long
Dim JunkCount As Long
Dim Entry As Variant
Dim FieldWidths As Variant
Dim TotalFile As String
Dim Records() As String
FileNum = FreeFile
FieldWidths = Array(7, 26, 12, 5, 9, 10, 10, 15, 10, 15, 12)
Open Path & ListBox1.Text For Binary As #FileNum
TotalFile = Space(LOF(FileNum))
Get #FileNum, , TotalFile
Close #FileNum
Records = Split(TotalFile, vbCrLf)
With ActiveSheet
For X = 45 To UBound(Records)
P = 1
For Z = LBound(FieldWidths) To UBound(FieldWidths)
Entry = Mid$(Records(X), P, FieldWidths(Z))
If Z - LBound(FieldWidths) = 2 Then
Entry = "'" & Entry
End If
Cells(X - 44, Z + 1 - LBound(FieldWidths)).Value = Entry
P = P + FieldWidths(Z)
Next
Next
End With
End Sub



"cottage6" wrote in message
...
Hi Rick,
Actually in the Text Import Wizard, you can choose which row to start
the
import with from the .txt file. The text file has 44 rows of total
garbage
at the top that I don't need (header rows, the report name, etc). We
have a
lot of old programs here. I can talk to the programmer about getting
rid
of
these because I think he can do that right in his job. Thanks again!

"Rick Rothstein (MVP - VB)" wrote:

Works like a charm!

Great!

I can think of several files I can use this in. Never ends for you
does
it....
any way you know of that I can start the import at row 45?

When you say "import at row 45", you mean start placing the text
imported
from the file at row 45, right? You can control which line the print
out
starts on by making a small change on the 6th line up from the bottom
of
the
Import_PTC subroutine. This is the line as it appeared in my code
(where
the
imported data was outputted to Row 1)...

Cells(X + 1, Z + 1 - LBound(FieldWidths)).Value = Entry

and here is that same line changed to start outputting the data on
Row
45
instead...

Cells(X + 45, Z + 1 - LBound(FieldWidths)).Value = Entry

Notice, all I changed is the value being added to the X loop index
value
in
the first argument.

Rick






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
ON OPEN VBA Code input incorrectly now excel sheet wont open mmartin New Users to Excel 1 February 16th 11 11:33 PM
Input selection choices ARGT Excel Discussion (Misc queries) 4 July 8th 08 03:59 AM
how do i open file for input Jac Excel Discussion (Misc queries) 3 December 3rd 07 03:47 AM
Detecting user cancel out of "File Open" input box [email protected] Excel Programming 3 September 25th 07 02:55 PM
Input box does not allow selection in another file Tom Ogilvy Excel Programming 2 August 26th 04 04:53 AM


All times are GMT +1. The time now is 11:17 AM.

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

About Us

"It's about Microsoft Excel"