I inherited an Excel macro for use with my companies' bank deposits. I was able to read line by line and figure out how the macro worked.
There's so many Cells.Replace functions that 32 bit Excel throws
"Compile Error: Procedure too large".
I need to slim it down. For example:
Cells.Replace What:="DIRECT DEPOSIT COMPANY A OF CALIFORNIA", Replacement:="COMPANY", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Cells.Replace What:="DIRECT DEPOSIT COMPANY A UTAH SECTOR", Replacement:="COMPANY", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
Both are from the same company, and treated as such afterwards, but because they have slightly different names I've been adding a new function for every one.
I assumed that because it is using 'LookAt:=xlPart" I'd be able to say:
Cells.Replace What:="DIRECT DEPOSIT COMPANY", Replacement:="COMPANY", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False
But I end up with "COMPANY", + everything after what is specified in the What function tacked onto the end.
Is there a more streamlined method of finding all cells starting with "DIRECT DEPOSIT COMPANY A", disregarding anything after that, and replacing with just "COMPANY"?
Here's how you can run repeated "replace all if starts with" replacements:
Sub tester()
ReplaceStartsWith "COMPANY A", "COMPANY"
ReplaceStartsWith "COMPANY B", "COMPANY"
End Sub
Sub ReplaceStartsWith(startsWith, replaceWith)
Cells.Replace what:=startsWith & "*", replacement:=replaceWith, lookat:=xlWhole
End Sub
Note you might want to add a space at the end of the startsWith if you don't want (eg) "COMPANY A" to match "COMPANY ADVANCED"
Related
I am trying to make a VBA macro in excel which searches for a heading (succeeded at that), then selects the column of the heading (succeeded at that too) to finally do a replacement through the selected column (only partly succeeded in this).
I want to replace e.g SUM(C22:G22) with SUM(C22:INDIRECT(ADDRESS(ROW(); COLUMN()-1; 4))). It works just fine in excel when I select a column and then ctrl+H and find what :*) replace with :INDIRECT(ADDRESS(ROW(); COLUMN()-1; 4))).
Below is the code I tried that works:
Sub FindString_Search_Replace_Column()
Rows("1:1").Select
Selection.Find(What:="XTOTAL 2021", After:=ActiveCell, LookIn:= _
xlFormulas2, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.EntireColumn.Select
Selection.Replace What:=":*)", Replacement:=":A3)", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
End Sub
It does exactly what I want when it "just" needs to replace with e.g. A3. However, when the code replacement bit gets a bit longer, see below code, with what I actually need, it seems like it stops right after ActiveCell.EntireColumn.Select. It never actually changes anything.
Sub FindString_Search_Replace_Column()
Rows("1:1").Select
Selection.Find(What:="XTOTAL 2021", After:=ActiveCell, LookIn:= _
xlFormulas2, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, SearchFormat:=False).Activate
ActiveCell.EntireColumn.Select
Selection.Replace What:=":*)", Replacement:=":INDIRECT(ADDRESS(ROW(); COLUMN()-1; 4)))", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
End Sub
I am thinking it is the inside ADDRESS bit it kind of chokes on but here my google skills cannot help me any further (no results suiting my problem).
Any ideas? :)
Try replacing the ; separators in your formula with , - when adding a formula in VBA typically you use "US-style" separator (unless assigning the formula via FormulaLocal).
Possibly that also applies to updating an existing formula.
In every formula of every cell of every sheet, the entire book, I want to replace part of the formula string, changing all "+" to "-", is there an easy way to do this in VBA if I have a particular workbook object xlWb?
I am trying:
xlWb.Cells.Replace What:="+", Replacement:="-", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=False, FormulaVersion:=xlReplaceFormula2
but get "Object does not support this property or method"
A workbook has not cells, only worksheets have. This causes your runtime error. Your Replace-command itself looks okay to me.
Simply loop over all the worksheets of your workbook should do the trick:
Dim ws As Worksheet
For Each ws In xlWb.Worksheets
ws.Cells.Replace What:="-", Replacement:="+", LookAt:=xlPart, SearchOrder _
:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False _
, FormulaVersion:=xlReplaceFormula2
Next
(If I where you, I would make a backup of the workbook before running the macro...)
I'm trying to make code that transforms a list of positions titles into standard positions predefined, an IF function would be too big.
The only way I thought of is to record a macro by replacing "this" for "that" and do this for all positions, but it would be a lot easier if it's possible to reference a cell instead of a text string inside the code:
What:="Field Installation Supervisor" (This part should be a cell reference from another sheet)
Replacement:= _ "Installation Supervisor" (This part is also a referenced cell from the same sheet as the above)
Don't know if it is possible to use cell reference inside this replacement code
Sub replacing()
Sheets("Active Employees June 01 2019").Select
Cells.Replace What:="Field Installation Supervisor", Replacement:= _
"Installation Supervisor", LookAt:=xlPart, SearchOrder:=xlByRows, _
MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
End Sub
I expect to use reference cells to make update/maintenance of the file easier
Just reference the cells in the normal way (and no need to select the sheet).
Sheets("Active Employees June 01 2019").Cells.Replace _
What:=Sheets("this").Range("A1").Value, _
Replacement:=Sheets("this").Range("A2").Value, _
LookAt:=xlPart, SearchOrder:=xlByRows, _
MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
I have a worksheet which contains a Pivot Table, the data is populated from a file output by another system.
Within Power Query I transform the data, specifically I use one of the fields to construct a URL.
"'=HYPERLINK(""" & "https://website.com/"&[Code] & """, """ & [Code] & """) "
When the data is in the Pivot Table, I need to manipulate the formatting to get it to display correctly.
In Excel I use a find and replace to convert the string into a proper URL showing the friendly name (and I resize the column).
Sub Macro1()
Cells.Replace What:="'=", Replacement:="=", LookAt:=xlPart, SearchOrder _
:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Columns("A:A").EntireColumn.AutoFit
End Sub
Refreshing the data means that the macro above needs running again.
I was keen to automate this for the users. However, running the following macro looks to do the formatting first THEN the refresh, what am I doing wrong?
Sub Macro2()
ActiveWorkbook.Connections("Query - XXX"). _
Refresh
'
Cells.Replace What:="'=", Replacement:="=", LookAt:=xlPart, SearchOrder _
:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Columns("A:A").EntireColumn.AutoFit
End Sub
NOTE the two parts of the macro work correctly independently, just not when combined as above.
If you find the query in the "Connections" sections on the Data ribbon, open it's properties, and uncheck the "Background Refresh" option, it will pause any VBA code until it is done refreshing. This property can also be set in VBA.
Sub Macro2()
ActiveWorkbook.Connections("Query - XXX").OLEDBConnection.BackgroundQuery = False
ActiveWorkbook.Connections("Query - XXX").Refresh
Cells.Replace What:="'=", Replacement:="=", LookAt:=xlPart, SearchOrder _
:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Columns("A:A").EntireColumn.AutoFit
End Sub
I am trying to locate the top 10 in a list of thousands of entries, to create an ongoing report of failures of hardware. The report displays a top 10 for various things. For example top 10 errors, top 10 downtime etc. It is used to target engineers to the most critical machines in a manufacturing facility.
Our macro has worked OK for when we have had 255 errors. We have now extended our error list to 2048, and we envisage that our macros will run incredibly slowly.
I have an idea to utilise the Search and Replace function, and utilise the number of replacements to achieve the top 10. Manually a message box is displayed giving this number. When recording this macro, no message box is seen (which is good) but I cannot locate where the number is.
The function returns a Boolean.
Macro recorded looks like this.
Sub searchmacrotest()
' searchmacrotest Macro
Cells.Replace What:="a", Replacement:="AZ", LookAt:=xlPart, SearchOrder _
:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
Cells.Replace What:="AZ", Replacement:="a", LookAt:=xlPart, SearchOrder _
:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
End Sub
The first line simply changes the a to Az, and the second changes it back to a. They run quickly and gave a number of 75 with my list.
Logic:
Use a unique word to replace. While choosing ensure that there is no possibility of the word to occur.
Use Countif to count the occurrences of this unique word after replacing
Code: Try this
Sub GetReplaceCount()
Dim ws As Worksheet
'~~> Set this to a word which is unique
Dim magicword As String: magicword = "Sid" & Format(Now, ddmmyyhhmmss)
'~~> This is what you want to replace
Dim searchText As String: searchText = "a"
Set ws = ThisWorkbook.Sheets("Sheet1")
With ws
.Cells.Replace What:=searchText, Replacement:=magicword, LookAt:=xlPart, SearchOrder _
:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
'~~> This will give you the number of occurences
Debug.Print Application.WorksheetFunction.CountIf(.UsedRange, "*" & magicword & "*")
.Cells.Replace What:=magicword, Replacement:=searchText, LookAt:=xlPart, SearchOrder _
:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
End With
End Sub