I am creating a macro in VBA which finds values in one sheet and uses them to populate array MyArr(11,4) and then does calculation. Find and populating part works great, problem is the latter part.
.Range("C2").Value = MyArr(11, 4) / MyArr(11, 1)
.Range("D2").Value = (MyArr(9, 4) + MyArr(10, 4) + MyArr(11, 4)) / (MyArr(9, 1) + MyArr(10, 1) + MyArr(11, 1))
.Range("E2").Value = (MyArr(0, 4) + MyArr(1, 4) + MyArr(2, 4) + MyArr(3, 4) + MyArr(4, 4) + MyArr(5, 4) + MyArr(6, 4) + MyArr(7, 4) + MyArr(8, 4) + MyArr(9, 4) + MyArr(10, 4) + MyArr(11, 4)) / (MyArr(0, 1) + MyArr(1, 1) + MyArr(2, 1) + MyArr(3, 1) + MyArr(4, 1) + MyArr(5, 1) + MyArr(6, 1) + MyArr(7, 1) + MyArr(8, 1) + MyArr(9, 1) + MyArr(10, 1) + MyArr(11, 1))
This is the fragment of code in question. It causes Overflow error (6). I know this error happens due to one of the values in array being empty. Is there an easy way to stop using empty values but still execute the code for the filled ones? So if for example MyArr(9,4) is empty it would still execute the 2nd line of code because there are values in (10, 4), (11, 4) with omitting (9,4).
If the rest of the code is needed please inform me.
MyArris declared. If everything is implicitly typed, Specifically which instruction is overflowing? The 2nd one? What doesTypeName(MyArr(9,4))say, and what kind of values are involved? See VBA data types - like @JohnColeman said, we don't need the rest of the code, just enough to know what we're looking at.On Error Resume Nextby itself isn't adequate and is even harmful (as @Mat'sMug points out). The only disciplined way to use it is if you explicitly checkErr.Numberafter statements that can cause errors and handle them as needed. A more reliable way is to use the constructOn Error GoTo err_handlerwhereerr_handleris a label for a block of error handling code. See this: cpearson.com/excel/ErrorHandling.htm