Find next Function using 2 criteria help

wlyon

New Member
Joined
Nov 13, 2015
Messages
3
Hello All,


First of all sorry if this is answered somewhere already, but I didn't know exactly how to word it.

I am an Engineering Intern that has somehow been tasked with writing Excel Macro for the last month. I've taught myself quite a lot so far but cant figure this one out.

The first part of the macro needs to search Column Z for the next cell that is not blank nor contain a value of 12 in it and then select it. (I.E. a cell contain a value of 8 or 4.2)

The next part of the macro needs run a goal seek function changing column X until Column Z is 12

Then End itself when all of Column Z is either blank or 12


I have written This code so far and it works perfectly except it checks every single row. Which is super slow since there are currently 75500 rows at the moment and will only increase over the years.

Sub GoalSeek()


Dim i As Long
For i = 5 To 75500
If (Range("Z" & i) = 12 Or Range("Z" & i) = "") Then
Else
With Range("Z" & i)
.GoalSeek Goal:=12, ChangingCell:=Range("X" & i)
End With

End If
Next i
End Sub

My theory for writing it like this was to hopefully speed up the checking process before goal seek was used. but it is still very slow. ( I know the goal seek part will have to be slow no matter what)

This code runs when the file is saved and that will only happen if someone updates a value which will change minimum 27 rows and maximum 2145 rows, So the bulk of the 75500 rows will not need to be looked at before saving. (Which I can't seem to avoid)

Any help or lessons would be greatly appreciated, I'm a quick study most of the time, but I can't find any info on this one to study unfortunately.
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
I don't see another way besides checking each row like you already did. Here's a modification to your macro because you stated that the number of rows will increase over time.

Code:
Sub GoalSeek()
     lastRow = Range("Z" & Rows.Count).End(xlUp).Row
     For i = 5 To lastRow
          If Range("Z" & i) <> 12 And Range("Z" & i) <> "" Then
               With Range("Z" & i)
                    .GoalSeek Goal:=12, ChangingCell:=Range("X" & i)
               End With
          End If
     Next i
End Sub
 
Upvote 0
As you surmised, the goal seek is time consuming.Still, you will get some relief by simply suppressing calculations and screen updating. I used the modified code below for a quick test. In column Z (Z5) I had the simple formula:

=X5+2
with random values between -10 and +10 in column X I looped through 1000 rows in column Z. With suppression of calculation and screen updating the execution time was 16 seconds, while w/o suppression it was 74 seconds for an identical data set in column X.

Code:
Sub GoalSeek()


Dim i As Long
With Application
    .ScreenUpdating = False
    .Calculation = xlCalculationManual
End With
For i = 5 To 1005       '75500
If (Range("Z" & i) = 12 Or Range("Z" & i) = "") Then
Else
With Range("Z" & i)
.GoalSeek Goal:=12, ChangingCell:=Range("X" & i)
End With

End If
Next i
With Application
    .ScreenUpdating = True
    .Calculation = xlCalculationAutomatic
End With

End Sub
This code runs when the file is saved and that will only happen if someone updates a value which will change minimum 27 rows and maximum 2145 rows, So the bulk of the 75500 rows will not need to be looked at before saving. (Which I can't seem to avoid)
Perhaps there is some way to use a change event to identify the cells that are changed, thereby reducing the cells that you need to loop through.
 
Upvote 0
Thanks WarPigl3t JoeMo,

From what I understand about the part you modified WarPigl3t, that will use the last cell in the row as a reference even though there are random blanks through out the data.

Your Code compared to mine JoeMo was 60% faster on average, thank you very much. Still need to figure something out but this will help a whole lot for sure.


I was thinking and I have a way to always have the blank cells in column Z show 12 which wouldn't be to big of an issue for people using it, removing one of the criteria for the macro and then it would only skip cells with 12 in it. Not sure if it will help at all, but worth a shot.

Thanks again for the quick replies and the macro lessons.
 
Upvote 0
Thanks WarPigl3t JoeMo,

From what I understand about the part you modified WarPigl3t, that will use the last cell in the row as a reference even though there are random blanks through out the data.

Your Code compared to mine JoeMo was 60% faster on average, thank you very much. Still need to figure something out but this will help a whole lot for sure.


I was thinking and I have a way to always have the blank cells in column Z show 12 which wouldn't be to big of an issue for people using it, removing one of the criteria for the macro and then it would only skip cells with 12 in it. Not sure if it will help at all, but worth a shot.

Thanks again for the quick replies and the macro lessons.
You are welcome. Are the blank cells truly blank or do they have a formula that returns ""?
 
Upvote 0
Yes it's a formula so I can easily change "" to 12 or anything else if need be
Yes, but you still need to loop through the cell and test if it contains 12 so I doubt it will improve execution time. If you have a reasonably large fraction of cells that contain 12 or "" you might think about filtering them out and then using the specialcells method to loop through visible cells only. Typically, what fraction of the cells contain 12 or ""?
 
Upvote 0

Forum statistics

Threads
1,215,551
Messages
6,125,478
Members
449,233
Latest member
Deardevil

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