?
Solved

Excel VBA Find and replace within range

Posted on 2013-01-21
9
Medium Priority
?
645 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 52

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 100 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 52

Accepted Solution

by:
Rgonzo1971 earned 100 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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 52

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 52

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 52

Expert Comment

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

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

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…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

764 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