PDA

View Full Version : I am totlay lost with VBA



alr7alal1
10-18-2010, 06:57 AM
I am trying to check a text file which contain diffierent type of data as follow:
Name=
Type=
Dest 1=
Dest 2=
ENd
Name=
Type=
Dest 1=
Dest 2=
END
....
...
etc
and i want to check for a specific type if it is available then put it in the first colum and the name in second column

and it is contain more than one of this type

austenr
10-18-2010, 07:33 AM
Hi....Posting a sample file would help us solve your problem.

alr7alal1
10-18-2010, 10:57 AM
I didn't understand what example do you want because i gave an example in the previous post

Tinbendr
10-18-2010, 11:58 AM
What have you written already?

We need to know what KIND of file, the format, etc.

So, we want an actual sample, real or fictional..., say 10 records.

Attach with the Manage attachments button.

Charlize
10-19-2010, 03:24 AM
Not tested but it could be something like this :
Option Explicit
Public Sub DoTheImport()
'filename
Dim FName As Variant
'separator used
Dim Sep As String
'choose your file
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 = "="
'call the import module
ImportTextFile CStr(FName), Sep
End Sub
Public Sub ImportTextFile(FName As String, Sep As String)
'row
Dim Rw As Long
'line in textfile, number of the file
Dim WholeLine As String, myfile As Long
'worksheet
Dim WS As Worksheet
'variables used for storing info of files
Dim myname As Variant, mytype As Variant
'when an error occures, quit
On Error GoTo EndMacro:
'set activesheet as the sheet to use
Set WS = ActiveSheet
'define last row to start
Rw = WS.Cells(Rows.Count, 1).End(xlUp).Row
Rw = Rw + 1
'get a free filenumber
myfile = FreeFile
'open the file as read
Open FName For Input Access Read As #myfile
'do if not eof
While Not EOF(myfile)
'get textline of the file
Line Input #myfile, WholeLine
'if the textline you just read contains Name then read 2 lines of text
If InStr(1, WholeLine, "Name=") > 0 Then
myname = Split(WholeLine, Sep)
Line Input #myfile, WholeLine
mytype = Split(WholeLine, Sep)
If mytype = "what you want" Then
WS.Range("A" & Rw).Value = myname
Rw = Rw + 1
End If
End If
Wend
EndMacro:
On Error GoTo 0
Close #myfile
End SubCharlize