Solved

Excel VBA Find and replace within range

Posted on 2013-01-21
9
614 Views
Last Modified: 2013-01-22
I need to replace words only in columns D through U. My code keeps replacing parts of cells in coumns A-C as well as D-U. This code below takes a long time to run because it loops through each line but I'm not sure how to do this. How can I avoid making changes in columns A-C and speed up the find and replace?

Application.ScreenUpdating = False

Dim LR As Long
Dim i As Long
 
    LR = Range("D" & Rows.Count).End(xlUp).Row
    
     For i = 3 To LR
        Cells.Replace What:="sod", Replacement:="0", LookAt:=xlPart, SearchOrder _
           :=xlByRows, MatchCase:=False
        Cells.Replace What:="sdo", Replacement:="0", LookAt:=xlPart, SearchOrder _
            :=xlByRows, MatchCase:=False
        Cells.Replace What:="sick", Replacement:="0", LookAt:=xlPart, SearchOrder _
            :=xlByRows, MatchCase:=False
        Cells.Replace What:="dso", Replacement:="0", LookAt:=xlPart, SearchOrder _
            :=xlByRows, MatchCase:=False
        Cells.Replace What:="off", Replacement:="0", LookAt:=xlPart, SearchOrder _
            :=xlByRows, MatchCase:=False
        Cells.Replace What:="all", Replacement:="0", LookAt:=xlPart, SearchOrder _
           :=xlByRows, MatchCase:=False
        Cells.Replace What:="week", Replacement:="0", LookAt:=xlPart, SearchOrder _
            :=xlByRows, MatchCase:=False
        Cells.Replace What:="quit", Replacement:="0", LookAt:=xlPart, SearchOrder _
            :=xlByRows, MatchCase:=False
        Next i
Application.ScreenUpdating = True

End Sub

Open in new window

0
Comment
Question by:minamina6
[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
  • 5
  • 3
9 Comments
 
LVL 51

Expert Comment

by:Rgonzo1971
ID: 38804246
Hi,

You should specify the range instead of cells (the whole worksheet), it could be Columns("D:U")

Regards
0
 
LVL 47

Assisted Solution

by:Wayne Taylor (webtubbs)
Wayne Taylor (webtubbs) earned 25 total points
ID: 38804247
Try this instead...

Application.ScreenUpdating = False

Dim LR As Long
Dim i As Long
 
    LR = Range("D" & Rows.Count).End(xlUp).Row
    
     For i = 3 To LR
        With Range("D:U")
               .Replace What:="sod", Replacement:="0", LookAt:=xlPart, SearchOrder _
           :=xlByRows, MatchCase:=False
               .Replace What:="sdo", Replacement:="0", LookAt:=xlPart, SearchOrder _
            :=xlByRows, MatchCase:=False
             .Replace What:="sick", Replacement:="0", LookAt:=xlPart, SearchOrder _
            :=xlByRows, MatchCase:=False
              .Replace What:="dso", Replacement:="0", LookAt:=xlPart, SearchOrder _
            :=xlByRows, MatchCase:=False
              .Replace What:="off", Replacement:="0", LookAt:=xlPart, SearchOrder _
            :=xlByRows, MatchCase:=False
              .Replace What:="all", Replacement:="0", LookAt:=xlPart, SearchOrder _
           :=xlByRows, MatchCase:=False
              .Replace What:="week", Replacement:="0", LookAt:=xlPart, SearchOrder _
            :=xlByRows, MatchCase:=False
              .Replace What:="quit", Replacement:="0", LookAt:=xlPart, SearchOrder _
            :=xlByRows, MatchCase:=False
        End With
        Next i
Application.ScreenUpdating = True

End Sub

Open in new window

0
 
LVL 51

Accepted Solution

by:
Rgonzo1971 earned 25 total points
ID: 38804260
Hi,

Or maybe better, instead of Range("D:U"), you could use Range("D3:U" & Range("D" & Rows.Count).End(xlUp).Row) to exclude the first lines and drop your loop, which is, by now, unnecessary.

Regards
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:minamina6
ID: 38804311
This still makes changes in columns A-C. I took out some of the replacement code to make this more readable.

With Range("D3:U" & Range("D" & Rows.Count).End(xlUp).Row)
        Cells.Replace What:="all", Replacement:="0", LookAt:=xlPart, SearchOrder _
           :=xlByRows, MatchCase:=False
        Cells.Replace What:="sdo", Replacement:="0", LookAt:=xlPart, SearchOrder _
            :=xlByRows, MatchCase:=False
        Cells.Replace What:="sick", Replacement:="0", LookAt:=xlPart, SearchOrder _
            :=xlByRows, MatchCase:=False
         End With

Open in new window

0
 
LVL 51

Expert Comment

by:Rgonzo1971
ID: 38804326
HI,

You have to delete  all the "Cells" but the dot must remain.

Regards
0
 

Author Comment

by:minamina6
ID: 38804342
You mean like this
.Replace What:="all", Replacement:="0", LookAt:=xlPart, SearchOrder _
           :=xlByRows, MatchCase:=False

Open in new window


instead of this?
Cells.Replace What:="all", Replacement:="0", LookAt:=xlPart, SearchOrder _
           :=xlByRows, MatchCase:=False

Open in new window

0
 
LVL 51

Expert Comment

by:Rgonzo1971
ID: 38804354
Hi,

Yes, the with instruction allows this

Regards
0
 

Author Comment

by:minamina6
ID: 38804475
That seemed to work. Thanks!
0
 
LVL 51

Expert Comment

by:Rgonzo1971
ID: 38804483
All that jazz for 50 points :-(
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
This code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

687 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