Consulting

Results 1 to 5 of 5

Thread: Please Help with VBA code

  1. #1
    VBAX Visitor
    Joined
    Apr 2016
    Posts
    1
    Location

    Please Help with VBA code

    URGENT HELP REQUIRED
    ... I am VBA noob

    My requirement is simple. I need to open the attached FILE.TXT file by the button click and read through each line of file.
    Whenever I find the word "polisnummer", I write value of the policy in cell A6 & then the value of "attribuut" in cell B6, the the value of "waarde" in cell C6 & the the value of " foutmelding " in cell D6.

    Once the next " polisnummer " string is found, I fill similarly in cell A7,B7,C7,D7....and so on (as evident from the excel)


    img.JPG
    Sample file in the File.ZIP


    Attached Files Attached Files

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Public Sub ImportText()
    Dim this As Worksheet
    Dim txtFile As Workbook
    Dim cell As Range
    Dim firstaddress As String
    Dim nextrow As Long
    
        Set this = ActiveSheet
        nextrow = 6
        
        With Application.FileDialog(msoFileDialogOpen)
        
            .AllowMultiSelect = False
            .InitialFileName = "File.txt"
            If .Show = -1 Then
            
                Workbooks.OpenText Filename:=.SelectedItems(1), _
                                                 DataType:=xlDelimited, _
                                                 Tab:=True
                                                 
                With ActiveWorkbook
                
                    With .Worksheets(1).Columns(1)
                
                        Set cell = .Find("Polisnummer", LookIn:=xlValues)
                        If Not cell Is Nothing Then
                        
                            firstaddress = cell.Address
                            Do
                            
                                this.Cells(nextrow, "B").Value = Trim(Replace(cell.Value, "Polisnummer", ""))
                                this.Cells(nextrow, "C").Value = Trim(Replace(cell.Offset(2, 0).Value, "Attribuut", ""))
                                this.Cells(nextrow, "D").Value = Trim(Replace(cell.Offset(3, 0).Value, "Waarde", ""))
                                this.Cells(nextrow, "E").Value = Trim(Replace(cell.Offset(4, 0).Value, "Foutmelding", ""))
                                
                                Set cell = .FindNext(cell)
                                nextrow = nextrow + 1
                            Loop While Not cell Is Nothing And cell.Address <> firstaddress
                            
                        End If
                    End With
                    
                    .Close SaveChanges:=False
                End With
            End If
        End With
    End Sub
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Knowledge Base Approver VBAX Wizard p45cal's Avatar
    Joined
    Oct 2005
    Location
    Surrey UK
    Posts
    5,876
    Quote Originally Posted by Anirban View Post
    URGENT HELP REQUIRED
    Clearly not the same urgency to say 'Thank-you'.

  4. #4
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,059
    Location
    Thank you XLD
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  5. #5
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    You're welcome Mr Bear!
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

Tags for this Thread

Posting Permissions

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