Solved

Excel VBA Find and replace within range

Posted on 2013-01-21
9
538 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 48

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 48

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
 

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 48

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 48

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 48

Expert Comment

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

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Sparklines have been introduced with Excel 2010 and are a useful tool for creating small in-cell charts, used for example in dashboards. Excel 2010 offers three different types of Sparklines: Line, Column and Win/Loss. What it does not offer is a…
Approximate matching with VLOOKUP and MATCH seems to me to be a greatly under-used technique, and one which is vital for getting good performance out of large lookups. Until recently I would always have advised using an exact match for simplicity an…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now