[[20070914215234]] 『任意の範囲の数字の抽出』(糸瀬) ページの最後に飛ぶ

[ 初めての方へ | 一覧(最新更新順) | 全文検索 | 過去ログ ]

 

『任意の範囲の数字の抽出』(糸瀬)

    A  B  C  D  E  F  G
 1 22 47 16 41 10 35  4
 2  5 23 48 17 42 11 29
 3 30  6 24 49 18 36 12
 4 13 31  7 25 43 19 37
 5 38 14 32  1 26 44 20
 6 21 39  8 33  2 27 45
 7 46 15 40  9 34  3 28

上記のような7*7の魔法陣を作りました。
そこで、何か押したら数字が点滅かセルに色付けしてみたいと思います。
ランダムにセルを選択して、色を付けるにはどうしたら良いのでしょうか?

できたら、1〜10位の連続選択(抽出)ができたらと思います。

数当てゲームをしたいので宜しくお願いします。

Excel2003
WindowsXp


 おもしろそうですね^^ノ
 政策メンバーにくわえてくださぁぁいノ

 詳しいルールをおしえてくださいなぁ〜♪

 回答じゃなくてすいません(Null)

 条件付書式ではどうでしょうか。

 H8 セルに
 =INT(RAND()*49)+1

 A1:G7 に数値があるとして、全体を選択し
 数式が =A1=$H$8
 として、適当な書式を設定しておけば
 F9 キーを押す毎にランダムなセルが選ばれると思います。

 ただ、他のセルを変更しても数値が変わってしまうので、
 それがいやなら自動計算をオフにするか、VBA を使用するか
 でしょうか。
 (Mook)

 ビンゴを想像したのですが・・・
 I〜P列を作業列として使用します。使う時は非表示にでもして見えなくします(^^;)
 運が悪ければ同一番号が発生するかもしれませんが・・・
 TEST1で7*7の表を作成し、TEST2である番号のセルを塗りつぶします。 (Hatch)
Sub test1()
Dim i As Integer
    Range("A1:G7").Interior.ColorIndex = xlNone
    Range("A:P").ClearContents
    Range("A1:G7").Formula = "=INDIRECT(""J""&COLUMN()+(ROW()-1)*7)"
For i = 1 To 49
        Randomize
        Cells(i, 9).Value = Rnd 
    Next i
    Range("J1:J49").Formula = "=RANK(I1,$I$1:$I$49)"
End Sub
Sub test2()
Dim i As Integer, CK
    Range("A1:G7").Interior.ColorIndex = xlNone
    Range("A1:G7").Font.ColorIndex = xlColorIndexAutomatic
    Range("K:P").ClearContents
    For i = 1 To 49
        Randomize
        Cells(i, 11).Value = Rnd 
    Next i
    Range("L1:L49").Formula = "=RANK(K1,$K$1:$K$49)"
    For i = 1 To 49
        MsgBox "今回の数字は " & Range("L" & i).Value & " です"
        test3 Range("L" & i).Value
        test4
        If Range("P1").Value = "○" Or Range("P2").Value = "○" Or _
            Range("P3").Value = "○" Or Range("P4").Value = "○" Then
            MsgBox "ビンゴ!!"
            Exit Sub
        End If
    Next i
End Sub
Sub test3(c As Integer)
Dim e
    For Each e In Range("A1:G7")
        If c = e.Value Then
            e.Interior.ColorIndex = 6
            e.Font.ColorIndex = 4

            Range("M" & e.Column).Value = Range("M" & e.Column).Value + 1
            Range("N" & e.Row).Value = Range("N" & e.Row).Value + 1
            If e.Column = e.Row Then Range("O1").Value = Range("O1").Value + 1
            If e.Column + e.Row = 8 Then Range("O2").Value = Range("O2").Value + 1
            Exit Sub
        End If
    Next e
End Sub
Sub test4()
Dim i As Integer
    For i = 1 To 7
        If Range("M" & i).Value = 7 Then
            Range("P1").Value = "○"
            Range(Cells(1, i), Cells(7, i)).Interior.ColorIndex = 3
        End If

        If Range("N" & i).Value = 7 Then
            Range("P2").Value = "○"
            Range(Cells(i, 1), Cells(i, 7)).Interior.ColorIndex = 3
        End If
    Next i
        If Range("O1").Value = 7 Then
            Range("P3").Value = "○"
            Range("A1,B2,C3,D4,E5,F6,G7").Interior.ColorIndex = 3
        End If
        If Range("O2").Value = 7 Then
            Range("P4").Value = "○"
            Range("A7,B6,C5,D4,E3,F2,G1").Interior.ColorIndex = 3
        End If
End Sub

ありがとうございました!
=INT(RAND()*49)+1を使ってみたら、やはり同じ数字が連続で出やすかったです。(だからと言ってrank関数との併用は私には難しいので・・・)

VBAは大変参考になりました。10個以上の数字を選ぶときはどこをいじればいいのでしょうか?ビンゴしなくて…。

(糸瀬)


 こんなのも試してください。

 Sub test()
 Dim a, b(), i As Long, ii As Long, n As Long, mySize As Long
 mySize = Int(Application.InputBox("ビンゴのサイズ(行数)を指定してください。",type:=1))
 If mySize < 2 Then Exit Sub
 ReDim a(1 To mySize ^ 2.4, 1 To 3)
 Randomize
 For i = 1 To UBound(a,1)
     a(i,1) = i : a(i,3) = Rnd
 Next
 VSortMA a, 1, UBound(a,1), 3
 ReDim b(1 To mySize, 1 To mySize)
 For i = 1 To mySize
     For ii = 1 To mySize
         n = n + 1
         b(i,ii) = a(n,1) : a(n,2) = Cells(i,ii).Address(0,0)
 Next ii, i
 With Range("a1")
     .CurrentRegion.Clear
     .Resize(mySize, mySize).Value = b
 End With
 For i = 1 To UBound(a,1) : a(i,3) = Rnd : Next
 VSortMA a, 1, UBound(a,1), 3
 For i = 1 To UBound(a,1)
    If a(i,2)<>"" Then
        Range(a(i,2)).Interior.ColorIndex = 3
        b(Range(a(i,2)).Row, Range(a(i,2)).Column) = "N/A"
        If (i >= UBound(b,1)) * (Linea(b)) Then
            MsgBox "Linea!"
            Exit For
        End If
     End If
     MsgBox i & " 番目の数字は " & a(i,1) & " です。" & vbLf & IIf(a(i,2)="","該当なし。","")
 Next
 End Sub

 Private Function Linea(ary) As Boolean
 Dim i As Long, ii As Long, flg1 As Boolean, flg2 As Boolean, x, y
 flg1 = True : flg2 = True : ii = UBound(ary,1)
 For i = 1 To UBound(ary,1)
     If ary(i,i) <> "N/A" Then
         flg1 = False
     Else
         flg1 = IIf(flg1 = True, True, False)
     End If
     If ary(i,ii) <> "N/A" Then
         flg2 = False
     Else
         flg2 = IIf(flg2 = True, True, False)
     End If
     ii = ii - 1
     If flg1=False And flg2=False Then Exit For
 Next
 If (flg1=True) + (flg2 = True) Then Linea = True : Exit Function
 For i = 1 To UBound(ary,1)
     With WorksheetFunction
         x = .Transpose(.Index(ary,i,0))
         y = .Index(ary,0,i)
     End With
     On Error Resume Next
     If UBound(Filter(x,"N/A",True)) = UBound(x) -1 Then
         Linea = True : Exit function
     End If
     If UBound(Filter(y,"N/A",True)) = UBound(y) -1 Then
         Linea = True : Exit Function
     End If
 Next
 End Function

 Private Sub VSortMA(ary, LB, UB, ref)
 Dim M As Variant, i As Long, ii As Long, iii As Long, temp As Variant
 i = UB : ii = UB
 M = ary(Int((LB+UB)/2),ref)
 Do While ii <= i
     Do While ary(ii, ref) < M
         ii = ii + 1
     Loop
     Do While i > M
         i = i - 1
     Loop
     If ii <= i Then
         For iii = LBound(ary,2) To UBound(ary,2)
            temp = ary(ii,iii) : ary(ii,iii) = ary(i,iii) : ary(i,iii) = temp
         Next
         i = i - 1 : ii = ii + 1
     End If
 Loop
 If LB < i Then VSortMA ary, LB, i, ref
 If ii < UB Then VSortMA ary, ii, UB, ref
 End Sub
 (seiya)
 Linea 編集 10:13

 面白そうなんでよせてくらはい。
     (弥太郎)
 '-----------------------
Sub itose()
    Dim i As Integer, n As Integer, idx As Integer, tbl

    Randomize
    idx = InputBox("なんぼ拾いまひょ?", , 10)
    Cells(1, 1).Resize(7, 7).Interior.ColorIndex = xlNone
    ReDim tbl(1 To 49, 1 To 1)
    For i = 1 To 49
        tbl(i, 1) = Rnd
    Next i
        For i = 1 To idx
        n = Application.Match(WorksheetFunction.Large(tbl, i), tbl, 0)
        Cells(IIf(n Mod 7, n Mod 7, 7), IIf(n Mod 7, n \ 7 + 1, n \ 7)).Select
        For j = 1 To 400
            Selection.Interior.ColorIndex = 1
            Selection.Interior.ColorIndex = 3
        Next j
    Next i
 End Sub

 不要な行があったので削除11:15

 ビンゴするまで継続するように前のコードを変更しました。
 RND()の括弧は要らないのか・・・(^^;) (修正しときました) (Hatch)

 なるほろ〜
 さういう事でしたか^^
 ほなら
    (弥太郎)
 Sub ビンゴ()
    Dim i As Integer, n As Integer, tbl, tbl_1, waittime As Date, flag As Boolean
    Dim j As Integer, ho As Date, mi As Date, se As Date, rng1 As Range, rng2 As Range

    Randomize
    Cells(1, 1).Resize(7, 7).Interior.ColorIndex = xlNone
    tbl_1 = Cells(1, 1).Resize(7, 7)
    ReDim tbl(1 To 49, 1 To 1)
    Set rng1 = Application.Union(Range("a1"), Range("b2"), Range("c3"), Range("d4"), _
                            Range("e5"), Range("f6"), Range("g7"))
    Set rng2 = Application.Union(Range("a7"), Range("b6"), Range("c5"), Range("d4"), _
                            Range("e3"), Range("f2"), Range("g1"))
    For i = 1 To 49
        tbl(i, 1) = Rnd
    Next i
    For i = 1 To 49
        n = Application.Match(WorksheetFunction.Large(tbl, i), tbl, 0)
        Cells(IIf(n Mod 7, n Mod 7, 7), IIf(n Mod 7, n \ 7 + 1, n \ 7)).Select
        For j = 1 To 400
            Selection.Interior.ColorIndex = xlNone
            Selection.Interior.ColorIndex = 5
        Next j
        Selection.ClearContents
        If WorksheetFunction.Count(Cells(Selection.Row, 1).Resize(, 7)) = 0 Then _
            Cells(Selection.Row, 1).Resize(, 7).Interior.ColorIndex = 3: flag = True
        If WorksheetFunction.Count(Cells(1, Selection.Column).Resize(7)) = 0 Then _
            Cells(1, Selection.Column).Resize(7).Interior.ColorIndex = 3: flag = True
        If WorksheetFunction.Count(rng1) = 0 Then rng1.Interior.ColorIndex = 3: flag = True
        If WorksheetFunction.Count(rng2) = 0 Then rng2.Interior.ColorIndex = 3: flag = True
        If flag Then Exit For
        ho = Hour(Now)
        mi = Minute(Now)
        se = Second(Now) + 1
        waittime = TimeSerial(ho, mi, se)
        Application.Wait waittime
    Next i
    Cells(1, 1).Resize(7, 7) = tbl_1
    MsgBox "びんご〜〜!", vbExclamation
End Sub


 ししょう・・・勝手に"びんご〜〜!"といって終わりますよ〜
 (セル選択の過程が見えないからどこか一行/一列選択しただけのように見えます)  (Hatch)

 あれ、さういえばそうでんなぁ(笑
 同時に複数のビンゴが成立することもありますしなぁ。
 ていせぇしてみました。
 そうそう、元データが欲しい時は、Hatchはんのtest1を実行してからにしませう。(笑
         (弥太郎)

 元データが必要だったんですね・・・コードを見ずに試して即反応してしまいました。m(_ _)m
 > 同時に複数のビンゴが成立することもありますしなぁ。
 4Line・・・何回実行したら発生するんだろう(冷汗)  (Hatch)

 「7*7」で
 「ランダムにセルを選択して、色を付ける」
 のは、こんなのはどうですか?
Sub koreha1()
    Dim irr As Long, irc As Long, mcr As Long, mcc As Long
    Dim bc As Long, nbc1 As Long, nbc2 As Long, i As Long
    Dim tn, tbl

    tn = InputBox("実行回数は?", , 10)
    If tn = "" Then Exit Sub

    Cells(1, 1).Resize(7, 7).Interior.ColorIndex = xlNone
    ReDim tbl(1 To 8, 1 To 8)

    Do Until gtn = tn * 1 Or mcr = 1000
    irr = Int(Rnd * (8 - 1) + 1)
        If tbl(irr, 8) = 7 Then
            mcr = mcr + 1
        Else
            Do Until mcc = 1000
                irc = Int(Rnd * (8 - 1) + 1)
                    If tbl(irr, irc) = "" Then
                        tbl(irr, irc) = 1
                        Cells(irr, irc).Interior.ColorIndex = 3
                        tbl(irr, 8) = tbl(irr, 8) + 1
                        tbl(8, irc) = tbl(8, irc) + 1
                        gtn = gtn + 1
                        Exit Do
                    End If
                mcc = mcc + 1
            Loop
            mcc = 0
        End If
    Loop
    mcr = 0

    If gtn <> tn * 1 Then
        MsgBox "再度実行して下さい"
        Exit Sub
    Else
        For i = 1 To 7
            bc = bc + IIf(tbl(8, i) = 7, 1, 0) + IIf(tbl(i, 8) = 7, 1, 0)
            nbc1 = nbc1 + tbl(i, i)
            nbc2 = nbc2 + tbl(i, 8 - i)
        Next i

        i = bc + IIf(nbc1 = 7, 1, 0) + IIf(nbc2 = 7, 1, 0)
        If i > 0 Then
            MsgBox i & "列ビンゴ"
            Else
            MsgBox "ビンゴ無し"
        End If
    End If
End Sub

 (HANA)


  セルの値 ASD()に格納
For  i = 1 To 7
  For  n= 1 To 7
      ASD(n+(i-1)*7)= Cells(i, n)
  Next n
 Next i

 1回目 rand    1〜49の数字を   
ASD(1〜49)  AAA(1)に格納  『ASD(1〜49)でから得た数字』
2回目 rand    1〜48の数字を   
ASD(1〜48)  『ASD(1〜49)でから得た数字』を除く  AAA(2)に格納
・
・
もし AAA()に0以外の数字があれば除く

 提示された表から ASD(1〜49)に格納された表から選択
	A	1回目  5	2回目  8	3回目  8	
1	22	1		1		1	
2	5	2		2		2	
3	30	3		3		3	
4	13	4		4		4	
5	38	5	38				
6	21	6		5		5	
7	46	7		6		6	
ASD(8)	47	8		7		7	
ASD(9)	23	9		8	23		
ASD(10)	6	10		9		8	6
ASD(11)	31	11		10		9	
	14	12		11		10	
	39	13		12		11	
	15	14		13		12
	16	15		14		13
	48	16		15		14
	24	17		16		15
	7	18		17		16
	32	19		18		17
	8	20		19		18
	40	21		20		19
	41	22		21		20
randで同じ数字がでても次の数字を拾い出す
ではどうでしょう
 (rand)


 作業列を使わないで配列に置き換えてみました。
 細かくみていないのでムダがありそうですけど・・・
 TEST31で7*7の表を作成、TEST32でビンゴ開始になっています。
 Msgboxをコメントアウトしてますので自動でビンゴまで進むはず・・・

 # 表を3つ作成して、どれがビンゴするかに変更してみました・・・
  9/17 13:00頃 変更しました(Hatch)
Sub test31()
Dim i As Integer, j As Integer, k As Integer
Dim a(48, 2), b(7, 7)
Dim a1, a2
    Range("K1").Value = 0
    For k = 1 To 3
        For i = 0 To 48
            Randomize
            a(i, 1) = Rnd: a(i, 2) = i + 1
        Next i
        For i = 48 To 1 Step -1
            For j = 0 To i - 1
                If a(j, 1) > a(j + 1, 1) Then
                    a1 = a(j, 1): a2 = a(j + 1, 1)
                    a(j, 1) = a2: a(j + 1, 1) = a1
                    a1 = a(j, 2): a2 = a(j + 1, 2)
                    a(j, 2) = a2: a(j + 1, 2) = a1
                End If
            Next j
        Next i
        For i = 0 To 48
            b(Int(i / 7), i Mod 7) = a(i, 2)
        Next i
        With Cells(8 * (k - 1) + 1, 1).Resize(7, 7)
            .Value = b
            .Font.ColorIndex = xlColorIndexAutomatic
            .Interior.ColorIndex = xlNone
        End With
    Next k
    Range("H1").Select
End Sub
Sub test32()
Dim i As Integer, j As Integer, k As Integer, ii As Integer
Dim a(48, 2) As Variant, b(3, 7, 7) As Variant, myLine2(3, 1, 1)
Dim myLine(3) As Integer, ck(3) As Integer, MyFlag As Integer
Dim a1 As Integer, a2 As Integer, e As Range
Range("K1").Value = Range("K1").Value + 1
    Range("A:G").Interior.ColorIndex = xlNone
    Range("A:G").Font.ColorIndex = xlColorIndexAutomatic
'    Range("I:P").ClearContents
    Randomize
    For i = 0 To 48
        a(i, 1) = Rnd: a(i, 2) = i + 1
    Next i
    For i = 48 To 1 Step -1
        For j = 0 To i - 1
            If a(j, 1) > a(j + 1, 1) Then
                a1 = a(j, 1): a2 = a(j + 1, 1)
                a(j, 1) = a2: a(j + 1, 1) = a1
                a1 = a(j, 2): a2 = a(j + 1, 2)
                a(j, 2) = a2: a(j + 1, 2) = a1
            End If
        Next j
    Next i
    For i = 0 To 48
'        MsgBox "今回の数字は " & a(i, 2) & " です"
Range("J1").Value = a(i, 2)
        For k = 1 To 3
            For Each e In Cells(8 * (k - 1) + 1, 1).Resize(7, 7)
                If a(i, 2) = e.Value Then
                    e.Interior.ColorIndex = 6
                    e.Font.ColorIndex = 4
                    b(k, e.Row - 8 * (k - 1), e.Column) = 1
                    Exit For
                End If
            Next e
        Next k
'---Check
        For k = 1 To 3
            For ii = 1 To 7
            ck(k) = 0

                For j = 1 To 7  '---列のcheck
                        ck(k) = ck(k) + b(k, j, ii)
                        If ck(k) = 7 Then
                            myLine(k) = myLine(k) + 1
                            myLine2(k, 1, 0) = ii
                            Range(Cells(1 + 8 * (k - 1), ii), Cells(1 + 8 * (k - 1) + 6, ii)).Interior.ColorIndex = 3
                          End If
                Next j
            Next ii
            For ii = 1 To 7
            ck(k) = 0
                For j = 1 To 7  '---行のCheck
                        ck(k) = ck(k) + b(k, ii, j)
                        If ck(k) = 7 Then
                            myLine(k) = myLine(k) + 1
                            myLine2(k, 1, 1) = ii
                            Range(Cells(ii + 8 * (k - 1), 1), Cells(ii + 8 * (k - 1), 7)).Interior.ColorIndex = 3
                        End If
                Next j
            Next ii
            ck(k) = 0
                For j = 1 To 7  '---左右斜めのCheck
                        ck(k) = ck(k) + b(k, j, j)
                        If ck(k) = 7 Then
                            myLine(k) = myLine(k) + 1
                            myLine2(k, 1, 0) = 8
                            For ii = 1 To 7
                                Cells(ii + 8 * (k - 1), ii).Interior.ColorIndex = 3
                            Next ii
                       End If
                Next j
                ck(k) = 0
                For j = 1 To 7  '---右左斜めのCheck
                        ck(k) = ck(k) + b(k, j, 8 - j)
                        If ck(k) = 7 Then
                            myLine(k) = myLine(k) + 1
                            myLine2(k, 1, 1) = 8
                             For ii = 1 To 7
                                Cells(ii + 8 * (k - 1), 8 - ii).Interior.ColorIndex = 3
                            Next ii
                        End If
               Next j
        Next k
            For k = 1 To 3
                If myLine2(k, 1, 0) > 0 Or myLine2(k, 1, 1) Then
                MsgBox " ビンゴ!!" & vbCrLf & vbCrLf & "No." & k & "が" _
                    & vbCrLf & vbCrLf & myLine(k) & "Lineです", vbExclamation
                MyFlag = 1
                End If
            Next k
            If MyFlag = 1 Then Exit Sub
    Next i
End Sub


 実行回数を設定して実行したら、勝手に選出して
 結果を表示させるのでは、「くじ」の様な感覚はありますが
 「ビンゴ」としての楽しみがあまりないので
 どうなのかと思いましたが、
 選出作業に立ち会えるのなら、この限りではないのかな?
 使用方法はどうなのですかね。

 前回に引き続き、
 ○セルの数字を見ている訳ではないので
  入力はなくて良いですが、セルの値を表示するので
  何かが入力されたA1:G7の枠を用意。(文字でも可)
 ○セルを1つ選出する毎にメッセージボックスが表示されるので
  [ OK ]で、次のセルを選択。
 ○ビンゴになった場合
   [  中止  ]・・・マクロの終了
   [ 再試行 ]・・・最初からやり直し
   [  無視  ]・・・続いてセルを選出
  の行動が選択出来ます。
Sub koreha2()
    Dim irr As Long, irc As Long, gtn As Long
    Dim bc As Long, nbc1 As Long, nbc2 As Long, i As Long
    Dim MSG As String
    Dim kn, tbl

    Cells(1, 1).Resize(7, 7).Interior.ColorIndex = xlNone
    ReDim tbl(1 To 8, 1 To 8)
    Randomize

    Do Until gtn = 49
        irr = Int(Rnd * (8 - 1) + 1)
        irc = Int(Rnd * (8 - 1) + 1)
        If tbl(irr, irc) = "" Then
            MsgBox Cells(irr, irc).Value
            tbl(irr, irc) = 1
            Cells(irr, irc).Interior.ColorIndex = 3

            tbl(irr, 8) = tbl(irr, 8) + 1
            tbl(8, irc) = tbl(8, irc) + 1
            If irr = irc Then
                nbc1 = nbc1 + 1
            End If
            If irr + irc = 8 Then
                nbc2 = nbc2 + 1
            End If
            gtn = gtn + 1

            If tbl(irr, 8) = 7 Then
                Cells(irr, 1).Resize(, 7).Interior.ColorIndex = 36
                MSG = MSG & irr & "行目" & Chr(13)
            End If
            If tbl(8, irc) = 7 Then
                Cells(1, irc).Resize(7).Interior.ColorIndex = 36
                MSG = MSG & irc & "列目" & Chr(13)
            End If
            If nbc1 = 7 Then
                For i = 1 To 7
                Cells(i, i).Interior.ColorIndex = 36
                Next
                MSG = MSG & "斜め" & Chr(13)
            End If
            If nbc2 = 7 Then
                For i = 1 To 7
                Cells(i, 8 - i).Interior.ColorIndex = 36
                Next
                MSG = MSG & "斜め" & Chr(13)
            End If
            If MSG <> "" Then
                kn = MsgBox(MSG & Chr(13) & "ビンゴ", vbAbortRetryIgnore + vbExclamation)
            End If

            Select Case kn
                Case 3
                    Exit Sub
                Case 4
                    Call koreha2
                Case 5
                    If tbl(irr, 8) = 7 Then
                        Cells(irr, 1).Resize(, 7).Interior.ColorIndex = 35
                    End If
                    If tbl(8, irc) = 7 Then
                        Cells(1, irc).Resize(7).Interior.ColorIndex = 35
                    End If
                    If nbc1 = 7 Then
                        For i = 1 To 7
                            Cells(i, i).Interior.ColorIndex = 35
                        Next
                    End If
                    If nbc2 = 7 Then
                        For i = 1 To 7
                            Cells(i, 8 - i).Interior.ColorIndex = 35
                        Next
                    End If
                    MSG = ""
                    If nbc1 = 7 Then
                        nbc1 = 0
                    End If
                    If nbc2 = 7 Then
                        nbc2 = 0
                    End If
            End Select

        End If
    Loop
End Sub

 「koreha1」は、行がそろう確率と列がそろう確率が
 違いますね・・・。(汗)
 こちらでは、修正しました。

 (HANA)


 HANAさん
 Rnd 関数は Randomize と一緒に使用しないと、ブックを再度開いたとき
 まったく同じ値が返されてしまうのでお気をつけて...
 Rand 関数 RandBetween 関数も同じです。(Randomizeと共用できませんが...)
 ちなみに、Randomaize Statement はループの中に記述する必要はありません。
 Rnd関数の出現前にあるだけでよいです。
 (seiya)

 seiyaさん。
 何度か仰って居られるたびに、「何のこと??」
 とよく分かっていなかったのですが・・・・
 分かりました!
 (気合いが足りなかったのですかね。)

 Randomize 入れておきます。
 密かに質問しようかと思っていたのです。
 ありがとうございました。

 (HANA)

 seiyaさん
 >Rand 関数 RandBetween 関数も同じです。
 これは、ワークシート関数の事ですよね?

 1.計算方法を手動にする
 2.C1:C5に =RAND() を入力。
 3.次のコードを作成
Sub test()
    Dim i
    For i = 1 To 5
        Cells(i, 2) = RND()
    Next i
    Cells(1, 1) = Cells(1, 1) + 1
End Sub

 実行手順
 1.ブックを開く
 2.マクロ実行
 3.再計算
 4.データを他のブックへ貼り付け
 5.保存して終了
 →1へ戻る

 上記手順を3回実行した結果
	[A]	[B]	[C]		[A]	[B]	[C]
  [1]	1	0.706 	0.135 	  [1]	2	0.706 	0.553 
  [2]		0.533 	0.712 	  [2]		0.533 	0.598 
  [3]		0.580 	0.984 	  [3]		0.580 	0.963 
  [4]		0.290 	0.183 	  [4]		0.290 	0.545 
  [5]		0.302 	0.435 	  [5]		0.302 	0.533 

	[A]	[B]	[C]				
  [1]	3	0.706 	0.616 				
  [2]		0.533 	0.624 				
  [3]		0.580 	0.651 				
  [4]		0.290 	0.713 				
  [5]		0.302 	0.931 				

 B列は、ブックを開いてコードを実行しても
 同じ値が表示されますが、C列は
 毎回違う値が表示されるようなのですが
 私が読み違えているのですか?

 (HANA)

 1) そのとおりです。ワークシート関数のことです。

 2) そうでしたか、私はそのような検証まではしたことがありませんでした...
    Rand/RandBetween関数では確か、すべて同じになったと記憶していましたので
    Rnd関数も同じとばっかり...
    どうやら、違っていたようですね、失礼しました。
 (seiya)

 seiyaさん。
  今までは、ワークシート関数でのみ
  実行していたので、分かって居なかったみたいです。
  その様になっている結果をなかなか信用出来ない方なので(笑)
  また教えて下さいね。
  ありがとうございました。

 (HANA)

 HANAさん、
 勿論、私のわかる範囲でならいつでも...
 (seiya)

コメント返信:

[ 一覧(最新更新順) ]


YukiWiki 1.6.7 Copyright (C) 2000,2001 by Hiroshi Yuki. Modified by kazu.