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

  • Home
  • SEARCH
  • 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 8796457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:37:39+00:00 2026-06-13T23:37:39+00:00

I just want to do a simple find and replace for multiple strings. For

  • 0

I just want to do a simple find and replace for multiple strings. For example, I need to replace all “A1”, “A2”, “A3” with “system” and all “B1”, “B2” with “ACC” and so on…

Does anyone know a good route to take? I’m just not sure how to get this started. Thanks for the help!

  • 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-13T23:37:40+00:00Added an answer on June 13, 2026 at 11:37 pm

    Update at bottom adressing Michael’s comment re a better approach for many pattern replacements

    If you record a simple macro using the manual Replace options from the Excel menu you will get code that you can tidy up to this

    1. The first option will update a cell in the ActiveSheet than contains "I am A1" to "I am System" – a part string match
    2. The second option will only update cells in the ActiveSheet that contains only "A1" to "Sytem" – ie a whole cell string match

    code

    Sub UpdatePartial()
    With ActiveSheet.UsedRange
    .Replace "A1", "System", xlPart
    .Replace "A2", "System", xlPart
    .Replace "A3", "System", xlPart
    .Replace "B1", "ACC", xlPart
    .Replace "B2", "ACC", xlPart
    End With
    End Sub
    
    Sub UpdateWhole()
    With ActiveSheet.UsedRange
    .Replace "A1", "System", xlWhole
    .Replace "A2", "System", xlWhole
    .Replace "A3", "System", xlWhole
    .Replace "B1", "ACC", xlWhole
    .Replace "B2", "ACC", xlWhole
    End With
    End Sub
    

    Update

    The code below

    1. Uses a basic Timer to compare replacing all partial strings ranging from A1-A99 and B1-B99
    2. The two methods are
      • The Replace method above called 198 times (ie 2*99) in a loop
      • A RegExp \ variant array combo

    On my testing the second method is faster for the 198 replacements on a 1,000,000 cell range.

    Less replacements will improve the relative speed towards the Replace. More towards the RegExp
    More cells will also improve the relative speed towards the Replace. Less towards the RegExp

    I didn’t proceed with trying a Find method with later parsing of strings. As a hyrbrid type solution (find then parse ut wouldn’t be competetive to a single replace or parse)

    Timer

    Sub MainCaller()
    Dim dbTime As Double
    Dim lngCnt As Long
    
    dbTime = Timer()
    For lngCnt = 1 To 99
    Call UpdatePartial("A" & lngCnt, "System")
    Call UpdatePartial("B" & lngCnt, "System")
    Next lngCnt
    Debug.Print Timer() - dbTime
    dbTime = Timer()
    Call RegexReplace("(A|B)[1-99]", "System")
    Debug.Print Timer() - dbTime
    End Sub
    

    1) Replace Sub

    Sub UpdatePartial(StrIn As String, StrOut As String)
    ActiveSheet.UsedRange.Replace StrIn, StrOut, xlPart
    End Sub    
    

    2) Regexp – Variant Array Sub

    Sub RegexReplace(StrIn As String, StrOut As String)
        Dim rng1 As Range
        Dim rngArea As Range
        Dim lngRow As Long
        Dim lngCol As Long
        Dim lngCalc As Long
        Dim objReg As Object
        Dim X()
    
    
        'On Error Resume Next
        'Set rng1 = Application.InputBox("Select range for the replacement of leading zeros", "User select", Selection.Address, , , , , 8)
        'If rng1 Is Nothing Then Exit Sub
        'On Error GoTo 0
    
        ActiveSheet.UsedRange
        Set rng1 = ActiveSheet.UsedRange
    
        'See Patrick Matthews excellent article on using Regular Expressions with VBA
        Set objReg = CreateObject("vbscript.regexp")
        With objReg
        .Pattern = StrIn
        .ignorecase = False
        .Global = True
        End With
    
       'Speed up the code by turning off screenupdating and setting calculation to manual
       'Disable any code events that may occur when writing to cells
        With Application
            lngCalc = .Calculation
            .ScreenUpdating = False
            .Calculation = xlCalculationManual
            .EnableEvents = False
        End With
    
        'Test each area in the user selected range
    
        'Non contiguous range areas are common when using SpecialCells to define specific cell types to work on
        For Each rngArea In rng1.Areas
            'The most common outcome is used for the True outcome to optimise code speed
            If rngArea.Cells.Count > 1 Then
               'If there is more than once cell then set the variant array to the dimensions of the range area
               'Using Value2 provides a useful speed improvement over Value. On my testing it was 2% on blank cells, up to 10% on non-blanks
                X = rngArea.Value2
                For lngRow = 1 To rngArea.Rows.Count
                    For lngCol = 1 To rngArea.Columns.Count
                        'replace the leading zeroes
                        X(lngRow, lngCol) = objReg.Replace(X(lngRow, lngCol), StrOut)
                    Next lngCol
                Next lngRow
                'Dump the updated array back over the initial range
                rngArea.Value2 = X
            Else
                'caters for a single cell range area. No variant array required
                rngArea.Value = objReg.Replace(rngArea.Value, StrOut)
            End If
        Next rngArea
    
        'cleanup the Application settings
        With Application
            .ScreenUpdating = True
            .Calculation = lngCalc
            .EnableEvents = True
        End With
    
        Set objReg = Nothing
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to do a very simple script: just want to find the newest
I understand that Silverlight 3.0 has binding but just want a simple example on
I just want to design this very simple website. Basically there are multiple pages
Simple question, I just want to select the text from the <Template> tag. Here's
It's really simple, I just want one screen. Wow, that [shiny thing] must have
I've created a simple class and got a little problem: I just want to
I am trying a very simple override. I just want to add one line
I'm looking for as simple way to create an identity set. I just want
Really simple but just not getting it. I want to reference a view by
I just set up a simple virtual server with Debian netinst. Now I want

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.