Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8860431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:15:54+00:00 2026-06-14T15:15:54+00:00

I use Excel VBA and the following method to search a string in html

  • 0

I use Excel VBA and the following method to search a string in html files, and replace it with the same string after adding bold tag.

FindAndReplace ("C:\xxx.htm", "hello world", "<b>hello world</b>")

Private Sub FindAndReplace(filePath As String, findWhat As String, replaceWith As String)

Dim nextFileNum As Long
Dim oldFileContents As String
Dim newFileContents As String
Dim textFileTypes() As String
Dim fileExtension As String

Dim sFileName As String
Dim iFileNum As Integer
Dim sBuf As String
Dim strFound As Integer

  If Len(Dir(filePath)) = 0 Then
    Exit Sub
  End If

  nextFileNum = FreeFile

  Open filePath For Input As #nextFileNum
  oldFileContents = Input$(LOF(nextFileNum), #nextFileNum)
  Close #nextFileNum

  newFileContents = Replace(oldFileContents, findWhat, replaceWith)

  nextFileNum = FreeFile

  Open filePath For Output As #nextFileNum
  Print #nextFileNum, newFileContents
  Close #nextFileNum
End Sub

The problem I am facing is the function won;t find the string if it splits in between because of the html source code line break.

For example, the string is found if the code is:

<p>hi hola hello world</p>

but it is not found if the code is:

<p>hi hola hello 
world</p>

Is there any other VBA method that I can use to search and replace text, or some functionality can be added to the above code so that it ignores the line break in between.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T15:15:56+00:00Added an answer on June 14, 2026 at 3:15 pm

    Try using a variation of:

    Function RemoveCarriageReturns(SourceString As String) As String
    
        Dim s As String
    
        'strip out CR and LF characters together
        s = Replace(SourceString, vbCrLf, "")
    
        'just in case, remove them one at a time
        s = Replace(s, Chr(13), "")
        s = Replace(s, Chr(10), "")
    
        RemoveCarriageReturns = s
    
    End Function
    

    The ASCII characters 13 and 10 are the carriage return and line feed characters.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to use VBA to filter some information in excel. As I have
In an Excel formula you can use =ISERR(A1) or =ISERROR(A1) In a VBA macro
Is there a way to use VBA (excel) to generate a clock time with
When I program in Excel-VBA I use Hungarian notation. Worksheet variables start with ws,
My Excel VBA takes ~300 XLS files and grabs 8 cells to deposit into
I've come across a nasty little bug in Excel VBA's .OpenText method.. It errors
I have the following line in Excel VBA: ActiveCell.Offset(r, 1).Value = IIf(rs2.Fields(SalesRelatedCallsQTD).Value = 0,
many people use extensively arrays in Excel/VBA to store a list of data. However,
I am have the following code for excel vba that will email a range
I have a sheet that I use an Excel VBA macro to automatically fill

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.