Search/Replace in defined array VBA

jdaywalt

Board Regular
Joined
Jul 18, 2012
Messages
63
I am just starting to learn about using arrays in VBA to improve efficiency when performing various functions across large data sets. I have an array defined as follows:

Dim MyArray as Variant
MyArray = Range("BS27:DS50000")


The values in this range will either contain text or a value of zero. I want to replace all zeroes with a blank/null value. How do I write the code that does this? I have recorded the search & replace code but I believe the syntax would be different when using an array vs. actually selecting the range from the worksheet. Just FYI, here is the recorded code (minus apostrophes, of course):

' Range("BS27:DS50000").Replace What:="0", Replacement:="", LookAt:=xlWhole, _
' SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
' ReplaceFormat:=False
' Range("BS27").Select
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
maybe:
Code:
Dim MyArray As Variant
MyArray = Range("BS27:DS50000")
For i = LBound(MyArray) To UBound(MyArray)
    For j = LBound(MyArray, 2) To UBound(MyArray, 2)
        If MyArray(i, j) = 0 Then MyArray(i, j) = Empty
    Next j
Next i
'Range("A1:D3") = MyArray
 
Upvote 0
Great, so far so good! However, I forgot to ask proper syntax for "copying" back into the original range after the search/replace has been performed. Can you help with that too please?
 
Upvote 0
The values in this range will either contain text or a value of zero. I want to replace all zeroes with a blank/null value. How do I write the code that does this? I have recorded the search & replace code but I believe the syntax would be different when using an array vs. actually selecting the range from the worksheet. Just FYI, here is the recorded code (minus apostrophes, of course):
I would expect simply replacing the zeroes with nothing would be the fastest way to do this...
Code:
Sub ReplaceZeroesWithEmptyString()
  Range("BS27:DS50000").Replace 0, "", xlWhole
End Sub
 
Upvote 0
Yes, Rick---that indeed worked very well! I thought the array method would be faster, but actually yours worked just as quickly. Appreciate the alternative approach!
 
Upvote 0
Great, so far so good! However, I forgot to ask proper syntax for "copying" back into the original range after the search/replace has been performed. Can you help with that too please?
The answer is the penultimate line of blah below.
The array method is faster if there are a lots of zeroes. If there are none the replace method is faster.
Trials with all zeroes in the range was 14 times as fast on my machine. Application.ScreenUpdating = False made little difference.

The two subs used for testing:
Code:
Sub blah()
Dim MyArray As Variant
Range("BS27:DS5000") = 0
MyArray = Range("BS27:DS5000")
starttime = Timer
For i = LBound(MyArray) To UBound(MyArray)
    For j = LBound(MyArray, 2) To UBound(MyArray, 2)
        If MyArray(i, j) = 0 Then MyArray(i, j) = Empty
    Next j
Next i
[B][COLOR=#0000ff]Range("BS27:DS5000") = MyArray[/COLOR][/B]
MsgBox Timer - starttime
End Sub



Sub ReplaceZeroesWithEmptyString()
Range("BS27:DS5000") = 0
starttime = Timer
Range("BS27:DS5000").Replace 0, "", xlWhole
MsgBox Timer - starttime
End Sub
I took a nought off your original range as my machine was starting to bend under the weight.
 
Last edited:
Upvote 0
I will try both methods and see which works best (and I love your timer function---I'm stealing that too!) Either way, I love that I now have two methods in my arsenal for current AND future use. Thank you so very much!
 
Upvote 0

Forum statistics

Threads
1,214,991
Messages
6,122,628
Members
449,095
Latest member
bsb1122

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top