Setting data range and speeding up VBA code

artz

Well-known Member
Joined
Aug 11, 2002
Messages
830
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I have some simple VBA code which does not work the way I want. It's probably something simple. First, in the code
Code:
LastRow = Cells(Rows.Count, "B").End(xlUp).Row

I would like to start the row count at row B3 not row B1; tried several options but all returned errors.
Code:
Sub Test2()
Dim LastRow As Long, i As Long
LastRow = Cells(Rows.Count, "B").End(xlUp).Row
For i = 1 To LastRow
Range("M" & i).Value = "dog"
    If Range("M" & i).Value = "dog" Then Range("N" & i).Value = "cat"
Next i
End Sub

Although this code returns an answer, it is very slow. It takes 5 minutes to process only about 20,000 rows. If anyone in the Forum could help move the range starting point to cell B3 and speed up execution time to a couple of seconds that would be great. And BTW, I'm not locked into this code, whatever works best will be used.

Any help would be very much appreciated. Am running this on Excel 2007 and Excel 2016.

Thanks,

-Art
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Why are you making
Range("M" & i).Value = "dog"
Then asking IF Range("M" & i).Value = "dog" then.....

Why not simply

Code:
Sub Test2()
Dim LastRow As Long, i As Long
LastRow = Cells(Rows.Count, "B").End(xlUp).Row
For i = 3 To LastRow
    If Range("M" & i).Value = "dog" Then Range("N" & i).Value = "cat"
Next i
End Sub
 
Upvote 0
Since you are setting column M values, there is no need for an If().
Code:
Range("N3:N" & Cells(Rows.Count, "B").End(xlUp).Row) = "cat"
 
Upvote 0
This will be quicker....saves looping

Code:
Sub MM1()
Dim lastrow As Long, rng As Range
lastrow = Cells(Rows.Count, "B").End(xlUp).Row
With Sheet1 'change sheet name to suit
    Set rng = .Range("M3:M" & lastrow)
    rng.AutoFilter field:=1, Criteria1:="dog"
    rng.Offset(1, 1).SpecialCells(12).Value = "cat"
    .AutoFilterMode = False
End With
End Sub
 
Upvote 0
If you wanted the values in column M and change the others:
Code:
Sub Test()
  Dim i As Long, a
  a = Range("M3:M" & Cells(Rows.Count, "B").End(xlUp).Row)
  For i = 1 To UBound(a)
    If a(i, 1) = "dog" Then a(i, 1) = "cat"
  Next i
  Range("N3").Resize(UBound(a)) = a
End Sub
 
Upvote 0
Here is another macro that you can consider...
Code:
Sub DogToCat()
  With Range("M3:M" & Cells(Rows.Count, "B").End(xlUp).Row)
    .Offset(, 1) = Evaluate(Replace(Replace("IF(@=""dog"",""cat"",if(#="""","""",#))", "@", .Address), "#", .Offset(, 1).Address))
  End With
End Sub
 
Upvote 0
Hi Guys,

Thanks so much for the overwhelming response! :-) You probably realize that we're not really doing dogs and cats in the VBA example; that was an illustrative example. From what I see, you've cleaned up my crummy VBA code.

I'll try all the suggestions tomorrow. What I really need in this exercise is code that executes in a flash for 2-3 hundred thousand rows of data. Yes, large data set. That's the endgame for this exercise.

Any suggestions for how to get there?

Thanks,

Art
 
Upvote 0
I'd try the provided codes first.
I don't think you will need to apply any of the suggestions at the link you provided.
 
Upvote 0
Hi Michael M,

I am starting to go through the code suggestions. Your code as posted ran fine. However, I made one necessary change as shown below:
Code:
Sub Test4()
Dim LastRow As Long, i As Long
LastRow = Cells(Rows.Count, "B").End(xlUp).Row
For i = 3 To LastRow
    Range("M" & i).Value = "dog"
    If Range("M" & i).Value = "dog" Then Range("N" & i).Value = "cat"
Next i
End Sub
The code I added being:
Code:
Range("M" & i).Value = "dog"

When I added this, the code took more than 5 minutes to execute.

Any thoughts or suggestions?

Thanks,

Art
 
Upvote 0

Forum statistics

Threads
1,224,819
Messages
6,181,153
Members
453,021
Latest member
Justyna P

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