Results 1 to 7 of 7

Thread: Speeding up a replace macro running through a long text

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    VBAX Newbie
    Joined
    Jan 2023
    Posts
    4
    Location
    Quote Originally Posted by vtito.site View Post
    if you provide the file itself, as well as what sTEXT and what sREPLACETEXT needs to be changed, I could solve this problem.
    Thanks for your interest! You can find an example xml file here (2,7 MB ZIP):
    https://upload.disroot.org/r/ofHPFsD...5nAw5oSeqt8Gg=

    For this file, the first two lines in the macro would be
    Public Const sTEXT = "k=""archaeological_site"""
    Public Const sREPLACETEXT = "k=""site_type"""

  2. #2
    Quote Originally Posted by _frudo View Post
    Thanks for your interest! You can find an example xml file here (2,7 MB ZIP):
    Thanks for the source file. The task is really interesting. I had to resort to using Scripting.FileSystemObject, since the source your file has unix line endings (vbLF only)
    The presented algorithm works for me not to say that fast. VBA is still a slow language. I have a processing speed of 1000 lines per 6 seconds. That is, 650 thousand will be processed in more than an hour. Frankly speaking, in c# this task would have been completed faster.
    But you can always see at what stage of processing the algorithm is (due to Application.StatusBar = "Processing File " & myCounter & " lines processed...").
    And you won't need to record the output file, it will be saved automatically.

    Sub Duplicate_And_Replace_From_VS()
    Dim OutputFileNum As Integer
    Dim XmlLine As String
    Dim XmlNewLine As String
    Dim currentBarStatus As Boolean, myCounter As Long
    Dim startTime As Date, endTime As Date
    
    
    startTime = Now
    
    
    currentBarStatus = Application.DisplayStatusBar
    Application.DisplayStatusBar = True
    
    
    Dim fso As Scripting.FileSystemObject
    Set fso = New Scripting.FileSystemObject
    Dim fInputFile As Scripting.TextStream
    Set fInputFile = fso.OpenTextFile("arch NOT site 2023-01-14.osm", ForReading, False, TristateMixed)
    
    
    OutputFileNum = FreeFile()
    Open "arch NOT site 2023-01-14New.osm" For Append As #OutputFileNum
    
    
    While Not fInputFile.AtEndOfStream
      XmlLine = fInputFile.ReadLine()
      Print #OutputFileNum, XmlLine
      myCounter = myCounter + 1
      If InStr(1, XmlLine, sTEXT) > 0 Then
        XmlNewLine = Replace(XmlLine, sTEXT, sREPLACETEXT)
        Print #OutputFileNum, XmlNewLine
      End If
      Application.StatusBar = "Processing File " & myCounter & " lines processed..."
      DoEvents
    Wend
    fInputFile.Close
    Close #OutputFileNum
    Application.DisplayStatusBar = currentBarStatus
    endTime = Now
    Dim elapsedTime As Integer
    elapsedTime = DateDiff("s", startTime, endTime)
    MsgBox "Done for " & elapsedTime & "sec."
    End Sub

Posting Permissions

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