VBA - Find and Replace - is there a better way to do this?

bbalch

Board Regular
Joined
Feb 23, 2015
Messages
53
I have pieced together different bits of code over the past few years to format a P&L report being generated from an accounting system. The vba is starting to run slowly and I'm trying to clean it up to make it run a faster.

The report being formatted has 100 tabs and the code formats each tab exactly the same. I'm interested to see if there is a more efficient way to find and replace text in column A. The code loops through each sheet, selects Column A, and finds and replaces text within column A. In total there are 75+ items that are replaced.

- I've read that using "Select" is not the best practice - is there a better way to find a replace?
- This currently loops through each sheet in the workbook - is there a way to find and replace in Column A for the entire workbook at once?

Code:
For Each Sh In Worksheets        Sh.Activate
            
            Columns("A:A").Select
            Selection.Replace What:="[OperatingIncome] ", Replacement:=""
            Selection.Replace What:="_OtherIncome]", Replacement:=""
            Selection.Replace What:="_TicketIncome]", Replacement:=""
            
    Next Sh
Thanks in advance for any suggestions.
 

steve the fish

Well-known Member
Joined
Oct 20, 2009
Messages
7,793
Office Version
365
Platform
Windows
This doesnt require the selection of the sheet or the range in that sheet:

Code:
For Each sh In ThisWorkbook.Worksheets
    With sh.Columns("A")
        .Replace What:="[OperatingIncome] ", Replacement:=""
        .Replace What:="_OtherIncome]", Replacement:=""
        .Replace What:="_TicketIncome]", Replacement:=""
    End With
Next sh
 

offthelip

Well-known Member
Joined
Dec 23, 2017
Messages
1,007
I think you will find it faster to load the column A into a variant array and do all the changes in memory. Try this code:

Code:
Dim sh As Worksheet
Dim tempstr As String


For Each sh In Worksheets
sh.Activate


lastrow = Cells(Rows.Count, "A").End(xlUp).Row
inarr = Range(Cells(1, 1), Cells(lastrow, 1))
For i = 1 To lastrow
  tempstr = inarr(i, 1)
  tempstr = Replace(tempstr, "[OperatingIncome]", "")
  tempstr = Replace(tempstr, "_OtherIncome]", "")
  tempstr = Replace(tempstr, "_TicketIncome]", "")
  inarr(i, 1) = tempstr
Next i


Range(Cells(1, 1), Cells(lastrow, 1)) = inarr


Next sh
Note I found you had a space after [OperatingIncome] which caused me to have a problem is that deliberate!!
 

Forum statistics

Threads
1,082,334
Messages
5,364,677
Members
400,810
Latest member
elbashka

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top