How to avoid open the code page?

marlonsaveri

Board Regular
Joined
Jan 28, 2011
Messages
68
When an error occurs, I use "On error goto" to solve, to prevent code page appears. However, sometime it doesn't works and a 1004 Error appears.
In my case:
1004 error, It is not possible get Average property from WorksheetFunction

WorksheetFunction.Average use to work ok; but, sometimes (I don't know why) the code stops run.

1st. How can I make a check like this?
Code:
IF "error does not happen with average function", averagefunction.
Else, don't call average.
Because we don't want code page is showed.

2nd. Actually, this is the code. I dont understand it neither; why my labels dont shows decimal numbers, just entires?

Code:
 maxi = Format(WorksheetFunction.max(interval), "##0.0000")
    mini = Format(WorksheetFunction.min(interval), "##0.0000")
    myAverage = Format(WorksheetFunction.Average(interval), "##0.0000")
 
    LabelMax.Caption = "Max: " & maxi
    LabelMin.Caption = "Min: " & mini
    LabelMed.Caption = "Av: " & myAverage
 
Last edited:

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.
You say that you are using On Error Goto ... but you're not showing that in your code. And you don't show the maxi mini and myAverage declarations ... what have they been declared as?
 
Upvote 0
They should be declared as Double. (Longs are still integers.)
 
Upvote 0
Also, although it doesn't error, I don't think assigning the return of Format will do anything.

Rich (BB code):
Sub exa()
Dim i As Double
    
    
    i = Format(12, "##0.0000")
    MsgBox i
    
    MsgBox Format(i, "##0.0000")
    
    i = Format(12.333, "##0.0000")
    MsgBox i
    
    MsgBox Format(i, "##0.0000")
    
End Sub
 
Upvote 0
They are receiving a formatted value; they should be declared as String.
 
Upvote 0
They are receiving a formatted value; they should be declared as String.

So, the best way is to declare as string?

However, "interval" is a range of cells, so, I could not use this, could I?
Code:
 Format(WorksheetFunction.max(interval), "##0.0000")

or
Code:
For Each mycell on interval
   If IsNumeric(mycell) Then
       if max < val(mycell) Then
          max = val(mycell)
       End If
   End If
Next

When I tried:
Code:
    maxi = Format(WorksheetFunction.max(interval), "##0.0000")
    mini = Format(WorksheetFunction.min(interval), "##0.0000")
    medi = Format(WorksheetFunction.Average(interval), "##0.0000")
 
    LabelMax.Caption = "Max: " & val(maxi)
    LabelMin.Caption = "Min: " & val(mini)
    LabelMed.Caption = "Av: " & val(medi)
'maxi, mini, medi as string
A error happens when there's nothing in the group of cells (= interval) or when there's something different of a number. Error 5; invalid argument or invalid procedure call.

For example, if interval are empty cells or if there is a letter, parentesis, etc inside it, the code doesn't run. Error 5 (max, min and average) or Error 1004 (just to average)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,507
Messages
6,179,183
Members
452,893
Latest member
denay

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