11.08.2013 Views

Excel's Formula - sisman

Excel's Formula - sisman

Excel's Formula - sisman

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

656<br />

or<br />

Part VI: Developing Custom Worksheet Functions<br />

Do<br />

[instructions]<br />

[Exit Do]<br />

[instructions]<br />

Loop [Until condition]<br />

The following example demonstrates the first syntax of the Do Until loop. This example makes<br />

the code a bit clearer because it avoids the negative comparison required in the Do While<br />

example.<br />

Function RowOfLargest4(c)<br />

NumRows = Rows.Count<br />

MaxVal = Application.Max(Columns(c))<br />

r = 1<br />

Do Until Cells(r, c) = MaxVal<br />

r = r + 1<br />

Loop<br />

RowOfLargest4 = r<br />

End Function<br />

Finally, the following function is the same procedure but is rewritten to use the second syntax of<br />

the Do Until loop:<br />

Function RowOfLargest5(c)<br />

NumRows = Rows.Count<br />

MaxVal = Application.Max(Columns(c))<br />

r = 0<br />

Do<br />

r = r + 1<br />

Loop Until Cells(r, c) = MaxVal<br />

RowOfLargest5 = r<br />

End Function<br />

The On Error statement<br />

Undoubtedly, you’ve used one of Excel’s worksheet functions in a formula and discovered that<br />

the formula returns an error value (for example, #VALUE!). A formula can return an error value in<br />

a number of situations, including these:<br />

You omitted one or more required argument(s).<br />

An argument was not the correct data type (for example, text instead of a value).<br />

An argument is outside of a valid numeric range (division by zero, for example).

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!