Results 1 to 5 of 5

Assign fixed values to array / For...Next loop

Threaded View

  1. #1
    Forum Contributor
    Join Date
    08-12-2010
    Location
    Excel World
    MS-Off Ver
    Excel 2013, 2019 & O365
    Posts
    214

    Thumbs up Assign fixed values to array / For...Next loop

    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!
    Last edited by SunOffice; 12-18-2011 at 05:48 PM. Reason: Added anothe method which I applied for above.
    Excelforum is Completely Awesome! True learning with Live Examples & Best Techniques!!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1