VBA Macro, Replace and Formula - excel

I am trying to replace certain cells with a vlookup function.
Columns("R").Select
Columns("R").Replace What:="N/A", _
Replacement:="=VLOOKUP(RC[-15], [test.xls]test_data'!$C:$R , 16, FALSE)", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
The current code above does not replace the N/A with the vlookup. It just leaves the cell unchanged.
If I removed the "=" in brackets, so it looks like this:
Replacement:="VLOOKUP(RC[-15], [test.xls]test_data'!$C:$R , 16, FALSE)"
This does replace my cells, but wont be in a formula format.
Can anyone help?
Thanks,

You have several problems with your formula which is preventing Excel from actually accepting it. You are trying to use
=VLOOKUP(RC[-15], [test.xls]test_data'!$C:$R , 16, FALSE)
but that
has a syntax error, due to the missing quotation mark, i.e. [test.xls]test_data' should be '[test.xls]test_data', and
is mixing R1C1 notation (RC[-15]) and A1 notation ($C:$R)
The formula you intended to use was probably
=VLOOKUP(RC[-15], '[test.xls]test_data'!C3:C18 , 16, FALSE)
So you probably meant your code to say
Columns("R").Select
Columns("R").Replace What:="N/A", _
Replacement:="=VLOOKUP(RC[-15], '[test.xls]test_data'!C3:C18, 16, FALSE)", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
Now, if your Excel settings are set so that you use R1C1 notation as the default, the correction to your formula is probably all that you will have to change.
But, if you use A1 notation as the default, that formula won't be interpreted correctly due to C3:C18 being ambiguous as to the notation being used.
So you will possibly need to temporarily set Excel to use R1C1 notation:
Application.ReferenceStyle = xlR1C1
Columns("R").Select
Columns("R").Replace What:="N/A", _
Replacement:="=VLOOKUP(RC[-15], '[test.xls]test_data'!C3:C18, 16, FALSE)", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
Application.ReferenceStyle = xlA1

Related

VBA replacement not working for specific input

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.

Replace all instances of something in Excel formulas in the entire book using VBA?

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...)

Trying to filter data that contains "-M", but it's giving me an error

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.

How to reference a cell into a replacement code in a recorded 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

Have someone a workaround for an Selection.Replace issue?

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

Resources