"A simple routine in VBA to find text within a cell and replace it with another"
Today I found myself having to delete from an excel file with 25,000 records speficica a word. In a column, in addition to a date to me is useful for later analysis, there is the word ASD.
For example:
11/05/1979 04/11/1979 ASD ASD ASD
06/11/1979
...
The alternative is to use the classic VBA Find and Replace, Find in place where "ASD" and replace left blank.
Since I decided to automate things complete a routine for this little banalità.Eccola:
Sub togliasd ()
Sheets (1).
Activate Cells.Replace What: = "ASD", Replacement :="", LookAt: = xlPart, SearchOrder _
: = xlByRows, MatchCase: = False, SearchFormat: = False, ReplaceFormat: = False End Sub
Let us see in detail step by step:
- Sub togliasd () -> initialize the routine and the call "togliasd.
- Sheets (1). Activate -> On the spreadsheet I'm interested in, the first in order of appearance, alternatively, you can put the name you gave the sheet instead of # 1.
- Cell.Replace -> refers to the replacement function
- What: = -> The parameter that indicates what you want to replace. The thing must be entered after the equal sign and quotation marks "ASD".
- Replacement: = -> The parameter that indicates what you want to replace. The thing with that, always after the same and always in quotes, in this case is void, then between the "do not write anything.
- SearchOrder: = xlByRows -> Search order indicates how research should proceed, if you line by line (xlByRows) column by column ( xlByColumns) . In this case line by line. NB (the underscore _ is used to wrap)
- MatchCase: = False -> Indicates whether the search should be case sensitive or not. That is, if it considers that uppercase and lowercase are the same thing or not.
- SearchFormat: = False -> attribute is a further research. In particular, we can select the format of the cell ...
- ReplaceFormat: = False ->... and replace it with one made here.
- End Sub -> conlude routine.
NB: For the perfectionists
conscious clock cycles ... and for those who want to replace a specific word in a single column or a specific selection, add after Sheets (1). Activate:
Range ("A1: R99"). Select -> select the cells rettangolone from A1 to R99
or, in case you want to select one or more columns ...
Columns ("x: y"). Select -> x and y replaced with the range of columns you want to select eg: B: Z "and a single column B: B
ari hello: v
0 comments:
Post a Comment