+ Reply to Thread
Results 1 to 5 of 5

Random Numbers

Hybrid View

kAustin79 Random Numbers 11-08-2007, 11:01 PM
mikerickson How about a UDF. Put... 11-08-2007, 11:37 PM
Myles Austin, See also: Sub... 11-12-2007, 12:19 AM
Myles Austin, Disregard my... 11-12-2007, 08:17 PM
lecxe Hi This is a formula... 11-13-2007, 07:34 AM
  1. #1
    Forum Contributor
    Join Date
    11-11-2005
    Posts
    267
    Austin,

    Disregard my original code. I cobbled it together in haste and allowed for only 2 criteria circumspection checks instead of four. The following fixes the problem.

    Sub AdjacentUniqueRandNos()
    MsgBox "No 2 adjacent cells are equal" & vbCrLf & "but duplicates may exist in the Range"
    With Application
      .Calculation = xlCalculationManual
      .ScreenUpdating = False
    End With
    
    Range("A1:L12").Clear
    
    Rows(1).Insert
    Columns("a").Insert
    
    For i = 2 To 12
        For j = 2 To 12
            Do Until Cells(i, j) <> Cells(i, j).Offset(0, 1) And Cells(i, j) <> Cells(i, j).Offset(1, 0) And Cells(i, j) <> Cells(i, j).Offset(-1, 0) And Cells(i, j) <> Cells(i, j).Offset(0, -1)
              Randomize
              Cells(i, j) = Int(10 + Rnd * 90)
            Loop
        Next
    Next
    
    Rows(1).Delete
    Columns("a").Delete
    
    
    With Application
      .Calculation = xlCalculationAutomatic
      .ScreenUpdating = True
    End With
    End Sub
    [By the way, if you want 2 digit numbers in every cell, the Worksheet formula =RAND*99 may yield single digit numbers 0 to 9 as wells as 10 to 98. Note that, 99 will be excluded as the RAND() function has the range 0 to less than 1 . The correct WorkSheet formula should be Int(10+Rand()*90) and in VBA Int(10+Rnd*90) or if you have AnalysisToolPack installed =RANDBETWEEN(10 ,99).
    HTH
    Myles

    ...constantly looking for the smoother pebble while the whole ocean of truth lies before me.

  2. #2
    Valued Forum Contributor
    Join Date
    10-15-2007
    Location
    Home
    MS-Off Ver
    Office 2010, W10
    Posts
    373
    Hi

    This is a formula solution.

    Leave Row 1 and Column A empty (or you can write text, but not numbers).

    In B2:

    =SMALL(IF((ROW($10:$99)<>A1)*(ROW($10:$99)<>B1)*(ROW($10:$99)<>A2),ROW($10:$99)),1+INT((90-IF((ROW(B2)=ROW($B$2))*(COLUMN(B2)=COLUMN($B$2)),0,IF((ROW(B2)=ROW($B$2))+(COLUMN(B2)=COLUMN($B$2)),1,3)))*RAND()))

    Copy down and accross

    This in an array formula, you have to confirm it with CTRL+SHIFT+ENTER.


    Remark: You say that the cells must have 2 digit numbers. I interpreted it as numbers from 10 till 99.

    HTH
    lecxe
    Last edited by lecxe; 11-13-2007 at 07:37 AM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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