Hi All!,
Can you please help me with below situation?
I have written some VBA codes and running them for the same kind of activities on some fixed integer values, for example ---> 45, 90, 91, 92, 93, 94, 95, 95, 96, 100.
I am able to run For...Next loop from 90 to 96, but unable to cover the other numbers like 45 and 100 with it, so I have to write & run the same codes separately for these two.
I felt that Array can handle this situation, so tired to google, but not got the success.
Below are the codes in two methods for example:
Option Explicit
Sub MyArrayy()
' Need to run For...Next loop for below values
' Values - 45, 90, 91, 92, 93, 94, 95, 95, 96, 100
Dim a, b, c As Integer, i As Integer
Dim iArr(45 To 45, 90 To 96, 100 To 100) As Integer
For i = LBound(iArr) To UBound(iArr)
a = Application.WorksheetFunction.RandBetween(1, 100)
b = Application.WorksheetFunction.RandBetween(1, 100)
c = a + b
MsgBox "For...Loop value is " & i & vbCrLf & a & "+" & b & " = " & c, vbInformation
Next i
Erase iArr()
End Sub
Option Explicit
Sub MyArrayy()
' Need to run For...Next loop for below values
' Values - 45, 90, 91, 92, 93, 94, 95, 95, 96, 100
Dim a, b, c As Integer, i As Integer
Dim iArr() As Variant
iArr = Array(45, 90, 91, 92, 93, 94, 95, 96, 100)
For i = LBound(iArr) To UBound(iArr)
a = Application.WorksheetFunction.RandBetween(1, 100)
b = Application.WorksheetFunction.RandBetween(1, 100)
c = a + b
MsgBox "For...Loop value is " & i & vbCrLf & a & "+" & b & " = " & c, vbInformation
Next i
Erase iArr()
End Sub
Thanks in advance!
Bookmarks