When I'm using Cell.Replace to replace a variable with a date excel changes my date formatting. (See picture)
I've tried changing the ReplaceFormat option to True... see below
Cells.Replace What:=VariableName, Replacement:=VariableValue, LookAt:=xlPart, _
SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
ReplaceFormat:=True
https://photos.app.goo.gl/JswS61eYiKEC6EUt2
You can use find/replace, and use a specified format when replacing. Add this line above the Cells.Replace... line:
Application.ReplaceFormat.NumberFormat = "MM/DD/YYYY H:MM AM/PM"
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.
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 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
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 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