Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default Replace() Range Function

MDW,

This is from the Excel Developer's Kit:

Declare Once, Use Many Times
If you are going to use an object more than once, declare and use object
variables rather than using a fully qualified object reference each time.
For instance, in the following example the FillCells2 procedure runs
approximately three times as fast as the FillCells1 procedure. This is
because the VBA engine doesn't have to resolve the entire object reference
each time it runs through the loop.

Example removed by me...

Anyway, the upshot of the above is that you could try changing your code to:

Dim oRange As Range
Set objOther = sht2.Range("A4:H850")
Set oRange = shtTotals.Cells(8, 1)

For J = 1 To 12
With oRange.Cells(J, 1)
If .Value < "" Then
objOther.Replace .Value, .Offset(0,1).Value
End If
Next

HTH,
Bernie
MS Excel MVP


"MDW" wrote in message
...
Sorry for the cross post. My original was posted in the wrong NG.

I've got some code that processes a couple hundred Excel workbooks. There

are two sheets. On the first sheet, it lists a person's name and their SSN.
On the second sheet, it just lists names. I use this code to loop through
the name/SSN section on the first page (which will always be A8:B19, where
column A has the name and column B has the SSN) and replace the names with
the SSNs on the second.

Here is my code(shtTotals is the first sheet, sht2 is the second sheet):

Set objOther = sht2.Range("A4:H850")

For J = 8 To 19

If shtTotals.Cells(J, 1).Value < "" Then

objOther.Replace shtTotals.Cells(J, 1).Value, shtTotals.Cells(J,

2).Value

End If

Next

Although it does work, I've identified this block of code as the

slowest-running section. I'm not sure why. Am I coding something improperly?
Any assistance would be appreciated.



  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default Replace() Range Function

Of course, that code requires an "End With" toward the bottom....

For J = 1 To 12
With oRange.Cells(J, 1)
If .Value < "" Then
objOther.Replace .Value, .Offset(0,1).Value
End If
End With
Next

Sorry about that,
Bernie
MS Excel MVP

"Bernie Deitrick" <deitbe @ consumer dot org wrote in message
...
Anyway, the upshot of the above is that you could try changing your code

to:

Dim oRange As Range
Set objOther = sht2.Range("A4:H850")
Set oRange = shtTotals.Cells(8, 1)

For J = 1 To 12
With oRange.Cells(J, 1)
If .Value < "" Then
objOther.Replace .Value, .Offset(0,1).Value
End If
Next



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Speed of Replace Range Monk[_2_] Excel Discussion (Misc queries) 6 May 24th 08 04:57 AM
multi range identify & replace Muralikrishnan Excel Worksheet Functions 0 February 24th 08 03:02 PM
replace null cells within a range to 0 Abbey Normal Excel Discussion (Misc queries) 5 August 29th 07 01:54 AM
replace a range of #'s e.g. 1.25 to 100 with text in excell Painter Excel Worksheet Functions 1 December 27th 05 08:36 PM
How can I replace a range of blank cells with a 0 Replacing blank cells Excel Discussion (Misc queries) 1 August 19th 05 12:01 AM


All times are GMT +1. The time now is 12:04 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"