Consulting

Results 1 to 4 of 4

Thread: Modify code to run on multiple files

  1. #1

    Modify code to run on multiple files

    I have a code created by Mark

    http://www.vbaexpress.com/forum/showthread.php?t=31784

    [vba]

    Option Explicit

    Sub exa()
    Dim _
    strLine As String, _
    strLineOut As String, _
    i As Long

    With CreateObject("Scripting.FileSystemObject").OpenTextFile(ThisWorkbook.Path & "\sample.txt")
    strLine = .ReadAll
    .Close
    End With

    With CreateObject("VBScript.RegExp")
    .Global = False
    .MultiLine = True
    .Pattern = "(<date)(.+)(/>)"

    If .Test(strLine) Then
    strLine = .Execute(strLine)(0)
    For i = 1 To Len(strLine)
    If IsNumeric(Mid(strLine, i, 1)) Then
    strLineOut = strLineOut & Mid(strLine, i, 1)
    End If
    Next
    Range("A1").Value = CLng(strLineOut)
    End If
    End With
    End Sub

    [/vba]
    How do I change this so that this would run on multiple text files? I really suck at VBA and I bravely attempted to modify the code but I am getting an error. "Run-time error 6: Overflow." on line "Cells(j, 4).Value = CLng(strLineOut)". Hovering it over strLineOut = "200906182010429". The correct value would have been "2010429" but it is including the previous value "20090618"

    What am doin wrong? Please offer me some advice.

    [vba]
    Sub LoopColumn()
    Dim c As Range
    Dim strLine As String
    Dim strLineOut As String
    Dim i As Long
    Dim j As Long
    j = 7
    For Each c In Range("G7", Range("G" & Rows.Count).End(xlUp))

    'Code here
    With CreateObject("Scripting.FileSystemObject").OpenTextFile(c)
    strLine = .ReadAll
    .Close
    End With

    With CreateObject("VBScript.RegExp")
    .Global = False
    .MultiLine = True
    .Pattern = "(<date)(.+)(/>)"

    If .Test(strLine) Then
    strLine = .Execute(strLine)(0)
    For i = 1 To Len(strLine)
    If IsNumeric(Mid(strLine, i, 1)) Then
    strLineOut = strLineOut & Mid(strLine, i, 1)
    End If
    Next

    Cells(j, 4).Value = CLng(strLineOut)

    j = j + 1
    End If
    End With
    'End of Code
    Next c
    End Sub
    [/vba]
    Last edited by Aussiebear; 03-22-2011 at 03:11 AM. Reason: Adjusted to use the correct tags around the code section

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    [vba]

    Sub LoopColumn()
    Dim c As Range
    Dim strLine As String
    Dim strLineOut As String
    Dim i As Long
    Dim j As Long
    j = 7
    For Each c In Range("G7", Range("G" & Rows.Count).End(xlUp))

    'Code here
    With CreateObject("Scripting.FileSystemObject").OpenTextFile(c)
    strLine = .ReadAll
    .Close
    End With

    strLineOut = ""

    With CreateObject("VBScript.RegExp")
    .Global = False
    .MultiLine = True
    .Pattern = "(<date)(.+)(/>)"

    If .Test(strLine) Then
    strLine = .Execute(strLine)(0)
    For i = 1 To Len(strLine)
    If IsNumeric(Mid(strLine, i, 1)) Then
    strLineOut = strLineOut & Mid(strLine, i, 1)
    End If
    Next

    Cells(j, 4).Value = CLng(strLineOut)

    j = j + 1
    End If
    End With
    'End of Code
    Next c
    End Sub[/vba]
    ____________________________________________
    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
    Amazing xld! You just added this line: strLineOut = "" and kaboom. problem solved. thanks a lot!

  4. #4
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    You actually told me what the problem was, the solution was quite easy when you did the work
    ____________________________________________
    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

Posting Permissions

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