Solved

Find and Replace with Excel VBA

Posted on 2015-02-06
3
108 Views
Last Modified: 2016-02-10
Cells.Replace What:="1/2/1900", Replacement:="", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False

Is it possible to modify the above VBA code to look for special range rather than the whole worksheet ?

Tks
0
Comment
Question by:AXISHK
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 59

Accepted Solution

by:
Saurabh Singh Teotia earned 300 total points
ID: 40593141
Yes you can do so by...

Lets say i want to check a1 to a10

range("A1:A10").Replace What:="1/2/1900", Replacement:="", LookAt:=xlPart, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False

Open in new window


In the first command cells means every cell in your worksheet.

Saurabh...
0
 
LVL 29

Assisted Solution

by:gowflow
gowflow earned 200 total points
ID: 40593175
correct and more

you have there Lookat:=xlPart
You have to be careful as you are searching for a date not a string which is something specific (unless this date comes in a string like: I went to the store on 1/2/1900 and got banned ..) but don't think so as this 1/2/1900 is more like a no date that shows as a date

So I would change the Lookat:=xlPart
by
Lookat:=xlwhole

and more but following does not apply to Replace but rather apply to Find but it is good mentioning as we are talking about the subject here.,

when you search for dates usually excel have a problem as the date you see is only a format of the real date that is a serial number so you may end up most of the time looking for a date with this instruction and not finding it although it is obvious for you that the date is there. All this to say that when you look for a date you are better off specifying in the argument Lookin xlformulas and not xlvalues as one would do in values.

So to sum it up for your instruction it would be safer to have it as follows for a range of say A1:A10
range("A1:A10").Replace What:="1/2/1900", Replacement:="", LookAt:=xlWhole, _
    SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False
0
 

Author Closing Comment

by:AXISHK
ID: 40607134
Tks
0

Featured Post

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa‚Ķ

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question