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.
Related
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 have a code that filters a lot of values, but when I try to change "-M" to " "(nothing) in a worksheet, it gives me an error.
The error that is given to me is
'Application-defined or object-defined error' 'Run-Time error 4004'.
Here is my code:
Cells.Replace What:="-m", Replacement:="", LookAt:=xlPart, SearchOrder _
:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False _
, FormulaVersion:=xlReplaceFormula2
Try this one
Cells.Replace What:="-m", Replacement:="", LookAt:=xlPart, SearchOrder _
:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
I have the same problem, but once I removed the "formulaversion" argument, it works. Also according to Microsoft Online Document, the "formulaversion" is not one of the expected argument for the replace function.*
https://learn.microsoft.com/en-us/office/vba/api/excel.range.replace
* The addition of the optional formulaversion argument to Range.Replace seems to coincide with the introduction of Dynamic Array formulas. As of the writing of this answer, the documentation has not been updated to reflect this. Since it is optional, it can probably be safely dropped. It should be dropped if your version of Excel does not support Dynamic Array formulas.
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"
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 problem with the replace funktion in VBA
In one columns from my table there some Values like 10,5m or 15,354m
I want to replace that with an simple vba command
like this
Columns("E:E").Select
Selection.Replace What:="m", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=True
but then my Values are 105 or 15354
When I use the build in command in excel without VBA it works like it should.
How Can I fix it?
Here is a test file with an Import txt file, where I collect the data
https://www.dropbox.com/sh/wtn83rs83dx455s/AABH7MmHKQxVJA6Tx7KVwM54a?dl=0
Thanks for your help
The values are not actually 10,5m or 15,354m but 10,5 and 15,354
it's a formatting thing that shows the "m" even if its not part of the value.
the reason is that your import macro sets the range format:
Range("E:E").NumberFormat = "#0.0""m"""
change it to:
Range("E:E").NumberFormat = "#0.0"
..and the "m" is gone.
I found the problem, at first I must replace "," with a "." after that I can replace "m" with "".
And after that I can set the correct format "#0.0""m"""
Columns("E:E").Select
Selection.Replace What:=",", Replacement:=".", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=True
Selection.Replace What:="m", Replacement:="", LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=True
Range("E:E").NumberFormat = "#0.0""m"""
bye