Consulting

Results 1 to 5 of 5

Thread: I am totlay lost with VBA

  1. #1

    I am totlay lost with VBA

    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

  2. #2
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    Hi....Posting a sample file would help us solve your problem.
    Peace of mind is found in some of the strangest places.

  3. #3
    I didn't understand what example do you want because i gave an example in the previous post

  4. #4
    VBAX Expert Tinbendr's Avatar
    Joined
    Jun 2005
    Location
    North Central Mississippi (The Pines)
    Posts
    993
    Location
    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.

  5. #5
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,286
    Location
    Not tested but it could be something like this :
    [VBA]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 Sub[/VBA]Charlize

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •