Solved

Excel VBA Find and replace within range

Posted on 2013-01-21
9
584 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
  • 5
  • 3
9 Comments
 
LVL 50

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 50

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 50

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 50

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 50

Expert Comment

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

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This tutorial explains how to create a series of drop-down lists that are dependent upon prior selections to guide (“force”) the user to make the correct selection and reduce data errors within Microsoft Excel. Excel 2010 was used for this tutorial;…
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial demonstrates how to create Excel charts: column, area, line, bar, and scatter charts. Formatting tips are provided as well.

696 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