Revision in code requested to avoid hanging of Excel Workbook

hsandeep

Well-known Member
Joined
Dec 6, 2008
Messages
1,226
Office Version
  1. 2010
Platform
  1. Windows
  2. Mobile
I am using a Function findhigh as

Rich (BB code):
Function findhigh(a As Range, b As Range, c As Range) As String

In the code.
Then I am finding number of 1s in c Range using
Rich (BB code):
y = Application.CountIf(c, 1)
Now the code works smoothly when

Rich (BB code):
If y >= 3 Then
     If a(y) = 0 Or a(y) = "" Then GoTo finish
     If b(y) = 0 Or b(y) = "" Then GoTo finish
Do While y > 1
          hv = a(y): lv = b(y)
      For q = y - 1 To 1 Step -1
            If a(q) > hv And b(q) < lv Then
                     findhigh = Format(a(q), "0.00")
                     GoTo finish
            End If
          y = y - 1
       Next q
Loop
End If

But the excel workbook gets hanged when

Rich (BB code):
If y = 2 Then
       If a(y) = 0 Or a(y) = "" Then GoTo finish
        If b(y) = 0 Or b(y) = "" Then GoTo finish
                     If a(y - 1) > a(y) And b(y - 1) < b(y) Then
                                               findhigh = Format(a(y - 1), "0.00")
                                               GoTo finish

                      End If
End If

I am finishing the code with

Rich (BB code):
findhigh = "null"
         finish:
End With
End Function

What mistake is there in the code? Any revision/help so that hanging issue of the workbook gets solved will be highly appreciated.
 
I guess 90% hanging possibility is from Do...Loop
It could help a lot if you can post a picture/XL2BB mini sheet, or attach a sample file via google drive
Sample file attached: For you I have reduced the a range & b range up to D40:M40 (a Range) & D42:M42 (b Range)
537.500.000.000.000.000.000.000.000.000.00#NAME?SBI532.45537.50
529.550.000.000.000.000.000.000.000.000.00#NAME?529.55
0​
0​
0​
0​
0​
0​
0​
0​
0​
0​
93.300.000.000.000.000.000.000.000.000.00#NAME?RBL92.2593.30
91.350.000.000.000.000.000.000.000.000.00#NAME?91.35
815.500.000.000.000.000.000.000.000.000.00#NAME?ICICI814.60815.50
805.300.000.000.000.000.000.000.000.000.00#NAME?805.30
395.250.000.000.000.000.000.000.000.000.00#NAME?Hi392.40395.25
385.900.000.000.000.000.000.000.000.000.00#NAME?385.90
379.000.000.000.000.000.000.000.000.000.00#NAME?JS377.20379.00
365.250.000.000.000.000.000.000.000.000.00#NAME?365.25
247.600.000.000.000.000.000.000.000.000.00#NAME?VD245.50247.60
240.650.000.000.000.000.000.000.000.000.00#NAME?240.65
449.250.000.000.000.000.000.000.000.000.00#NAME?TM442.25449.25
427.400.000.000.000.000.000.000.000.000.00#NAME?427.40
221.450.000.000.000.000.000.000.000.000.00#NAME?TP218.15221.45
216.900.000.000.000.000.000.000.000.000.00#NAME?216.90
37449000000000#NAME?37434.0037448.95
37106000000000#NAME?37105.75
 
Upvote 0

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
I agree with @bebo021999, we need to see an example.

You are very specific about the problem - it exists until F10=1. So why can't you post that example?

Bear in mind that we have no idea what your workbook looks like, and what other code or formulae might be there.

The code you've posted doesn't loop for y<3, but in Post #9 you say
First Part: When F10 is not equal to 1 then get findhigh & findlow (here Do While....n<3.....Loop)

There is no such loop in the code you've posted. So is the code you've posted the code that's causing the problem?
 
Last edited:
Upvote 0
Sample file attached: For you I have reduced the a range & b range up to D40:M40 (a Range) & D42:M42 (b Range)

That's a start, thanks ...

But we need to see:

- the cell addresses - which is D40 for example?
- the formulae you're using.
- No #NAME errors
- the Function code you're using, if it's not the same as what you've already posted.

It's best if you can use XL2BB to do this, then we can copy/paste everything into a test workbook.
 
Upvote 0
That's a start, thanks ...

But we need to see:

- the cell addresses - which is D40 for example?
- the formulae you're using.
- No #NAME errors
- the Function code you're using, if it's not the same as what you've already posted.

It's best if you can use XL2BB to do this, then we can copy/paste everything into a test workbook.
 
Upvote 0
I am using the code in my Workbook which has now several ws with code (28 precisely). Code were running fine in all the ws till I inserted this new ws (in this same Workbook) & in the Module I pasted a ‘new code’.

Thereafter, I am facing ‘troubling issues’ with my Workbook getting ‘hanged’, Worksheets flickering & the arrow cursor turning in to a blue spinning ball. The astonishing part of this code's execution is that the 'troubling issues' remains for a specified time period I.e it starts when now>= 07-29-2022 9:15:00 AM & remains up to now<07-29-2022 10:00:00 AM i.e. from 9:15 till 10:00; whenever the Workbook is kept opened. As soon as now>=10:00, all these ‘troubles’ vanishes & the all ws in the Workbook functions normal including this new Worksheet.
 
Upvote 0
1 more astonishing part of the code: I start the wb without this code in the ws whose tab named is ZZZ. But at now>=10:00, I paste this code in this ws ("ZZZ") & all these ‘troubles’ vanishes & the all ws in the Workbook functions normal including this new Worksheet.
 
Upvote 0
537.00533.70532.00530.20530.70530.45532.00530.45530.35530.35#NAME?
532.25531.00528.30527.30527.55529.70529.85529.05528.15527.50#NAME?
1111111111
93.7093.5092.7592.0092.3092.5593.0592.8592.7092.40#NAME?
92.4092.6091.3591.1091.4592.1592.2092.2092.3591.75#NAME?
823.40818.80816.75812.15815.00813.95815.25815.25815.50815.40#NAME?
816.80815.50809.95808.00809.35811.70813.00813.00814.00813.90#NAME?
399.25405.45407.60407.00408.50410.25410.00410.85411.85411.80#NAME?
395.20397.70400.65404.35405.00407.95408.70409.85409.85409.95#NAME?
384.65384.50391.50392.90394.30394.60394.00394.40393.00392.30#NAME?
379.75379.25382.05389.30389.80392.55390.45391.10391.20389.90#NAME?
103.00105.35109.20108.50107.85108.45108.05107.90107.75107.50#NAME?
102.15102.70104.80107.10106.55107.65107.10107.40107.40106.70#NAME?
451.50450.95449.65448.00448.00452.30452.55450.80450.30450.00#NAME?
445.15448.50445.45445.80446.40447.20449.60448.70449.00448.50#NAME?
221.55221.95221.50220.00220.00220.70220.85220.90220.60220.60#NAME?
219.80220.75219.45218.85219.05219.70220.00220.15220.00219.65#NAME?
37800377123764237489375663758537625376203759737585#NAME?
37640375983736737283373853750037541375243751337444#NAME?
 
Upvote 0
I agree with @bebo021999, we need to see an example.

You are very specific about the problem - it exists until F10=1. So why can't you post that example?

Bear in mind that we have no idea what your workbook looks like, and what other code or formulae might be there.

The code you've posted doesn't loop for y<3, but in Post #9 you say


There is no such loop in the code you've posted. So is the code you've posted the code that's causing the problem?
I also think that a different code DO WHILE is needed when n<3 AND F10<>1...& 'proper' exit of DO WHILE should solve the problem
 
Upvote 0
With lot of ‘googling’ I have REVISED the ‘new vba code’ in Module in the Worksheet (tab named “ZZZ”) using User Defined Function to get outputs in some cells where UDFs are there. Same Workbook which has several ws & some ws with Worksheet codes (28 ws with code precisely). In ‘ThisWorkbook’ also I am using a macro.

Codes were running fine in all the ws till I inserted this new Worksheet tab named “ZZZ” (in this same Workbook) & in the Module I pasted this ‘new vba code’.

Since the addition of ‘new vba code’, I am facing ‘troubling issues’ with my Workbook getting ‘hanged’, Worksheets flickering & the arrow cursor turning in to a blue spinning ball. The astonishing part of this new vba code 's execution is that the 'troubling issues' remains for a specified time period I.e it starts when now>= 08-09-2022 9:15:00 AM & remains up to now<08-09-2022 10:00:00 AM i.e. from 9:15 till 10:00; whenever the Workbook is kept opened. As soon as now>=10:00, all these ‘troubles’ vanishes & the all ws in the Workbook functions normal including this new Worksheet tab named “ZZZ”.

The problem PERSISTS ONLY WHEN D10:E10=1 & as soon as F10=1, ALL PROBLEMS VANISH.

The problem ‘identified’ is ALL T COLUMNS BECOME #VALUE! even when the conditions are met (when E10=1). Here I would definitely say, that I had observed that ‘sometimes’ column T populated desired values when E10 was “1” for FEW SECONDS but again some/all ‘function cells’ became #VALUE!

My guess: When D10=1 code finds y=1; & thereafter when E10=1 code finds y=2. The trouble starts & PERSISTS ONLY WHEN D10:E10=1. AFTER>=10:00 I.E. WHEN F10=1, EVERY ACTIONS BECOME ‘NORMAL’.

The a Range & all b Range (in this new Worksheet tab named “ZZZ”) are D4:M4 & D6:M6; similarly D12:M12 & D14:M14……& so on till D40:M40 & D42:M42 & they can generate0” zero OR a numerical value only & not '' (null) or space/(s)

D10:M10 becomes 1 from 0. By default D10:M10=0 & time function in another worksheet to which they (D10:M10) are ‘linked’ using simple Excel function, make them “1”; one by one. Example: D10=Time!C32, E10=Time!C56……& so on.

I am using a Function findhigh & findlow in this new Worksheet tab named “ZZZ” & the complete REVISED code is

Rich (BB code):
'Code finds the findhigh when the criteria are met
Rich (BB code):
Function findhigh(a As Range, b As Range, c As Range) As String
Dim x As Integer
x = Application.Sheets("ZZZ").Range("D10:M10").Cells.Count
Dim y As Integer
y = Application.WorksheetFunction.CountIf(Sheets("ZZZ").Range("D10:M10"), "=1")
If y = x And Sheets("ZZZ").Range("WA10").Value = "=1" Then
GoTo finish
End If

If y >= 3 Then
If a(y) = 0 Or a(y) = "" Then GoTo finish
If b(y) = 0 Or b(y) = "" Then GoTo finish
Do While y > 1
hv = a(y): lv = b(y)
For q = y - 1 To 1 Step -1
If a(q) > hv And b(q) < lv Then
findhigh = Format(a(q), "0.00")
GoTo finish
End If
y = y - 1
Next q
Loop
End If

If y = 2 And Sheets("ZZZ").Range("F10").Value < 1 Then
If a(y) = "" Then GoTo finish
If b(y) = "" Then GoTo finish
If a(y) = 0 Or b(y) = 0 Then
findhigh = Format(a(y - 1), "0.00")
GoTo finish
End If

q = y - 1
Do While q <> 0
hv = a(y): lv = b(y)
For q = y - 1 To 1 Step -1
If a(q) > hv And b(q) < lv Then
findhigh = Format(a(q), "0.00")
GoTo finish
End If
y = y - 1
Next q
Loop
End If

If y = 1 And Sheets("ZZZ").Range("E10").Value < 1 Then
If a(y) = 0 Or a(y) = "" Then GoTo finish
If b(y) = 0 Or b(y) = "" Then GoTo finish
findhigh = Format(a(y), "0.00")
GoTo finish
End If
If y = 0 And Sheets("ZZZ").Range("D10").Value < 1 Then
GoTo finish
End If
findhigh = "null"
finish:
End Function

'Code finds the findlow when the criteria are met
Function findlow(a As Range, b As Range, c As Range) As String

Dim x As Integer
x = Application.Sheets("ZZZ").Range("D10:M10").Cells.Count
Dim y As Integer
y = Application.WorksheetFunction.CountIf(Sheets("ZZZ").Range("D10:M10"), "=1")
If y = x And Sheets("ZZZ").Range("WA10").Value = "=1" Then
GoTo finish
End If

If y >= 3 Then
If a(y) = 0 Or a(y) = "" Then GoTo finish
If b(y) = 0 Or b(y) = "" Then GoTo finish
Do While y > 1
hv = a(y): lv = b(y)
For q = y - 1 To 1 Step -1
If a(q) > hv And b(q) < lv Then
findlow = Format(b(q), "0.00")
GoTo finish
End If
y = y - 1
Next q
Loop
End If

If y = 2 And Sheets("ZZZ").Range("F10").Value < 1 Then
If a(y) = "" Then GoTo finish
If b(y) = "" Then GoTo finish
If a(y) = 0 Or b(y) = 0 Then
findlow = Format(b(y - 1), "0.00")
GoTo finish
End If

q = y - 1
Do While q <> 0
hv = a(y): lv = b(y)
For q = y - 1 To 1 Step -1
If a(q) > hv And b(q) < lv Then
findlow = Format(b(q), "0.00")
GoTo finish
End If

y = y - 1
Next q
Loop
End If

If y = 1 And Sheets("ZZZ").Range("E10").Value < 1 Then
If a(y) = 0 Or a(y) = "" Then GoTo finish
If b(y) = 0 Or b(y) = "" Then GoTo finish

findlow = Format(b(y), "0.00")
GoTo finish
End If

If y = 0 And Sheets("ZZZ").Range("D10").Value < 1 Then
GoTo finish
End If
findlow = "null"
finish:
End Function

For another 12 ws, I am using the below code pasted in ThisWorkbook:

Rich (BB code):
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

'Delete data in F2:G42 if cell A2="z" The code triggers ONLY when the file is clicked SAVED
'Then the code FILLS BLANKS in F2:G42 by removing the EXISTING VALUES if any from the worksheets
'tab named H001:H9
'Declare the variables
Dim ary As Variant, i As Long

ary = Array(Sheet86, Sheet83, Sheet82, Sheet81, Sheet80, Sheet39, Sheet36, Sheet33, Sheet42, Sheet77, Sheet78, Sheet79)

For i = 0 To UBound(ary)
If ary(i).Range("A2") = "z" Then ary(i).Range("F2:G42").ClearContents
Next i
End With
End Sub

EARNEST REQUEST TO ALL GURUS OF THIS FORUM TO HELP & SOLVE THE TROUBLE SINCE THE PROBLEM STILL PERSISTS ONLY WHEN D10:E10=1 & AS SOON AS F10=1, ALL PROBLEMS VANISH.
I HAD TRIED MY BEST BY REVISING THE CODE AS FAR AS I COULD
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,818
Messages
6,181,151
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