席替えをくじ引きで行う:Excel VBA入門 |
スポンサードリンク | |
Sub seat02() Dim c As Range Dim myShape As Shape Dim n As Long Dim ob As Object '数式バーを非表示にします Application.DisplayFormulaBar = False '乱数発生 Dim i As Long Dim x() Dim y() Dim d '---開始値Ldと終了値Udを定数で指定 Const Ld = 1 Const Ud = 40 ' 生徒数40としていますので、ここを適切な数値に変更します。 '----使用する配列を準備する ReDim x(1 To Ud - Ld + 1) ReDim y(1 To Ud - Ld + 1) ReDim d(1 To Ud - Ld + 1, 1 To 1) Randomize '----乱数と値を配列にセットする For i = 1 To Ud - Ld + 1 x(i) = Rnd() y(i) = i + Ld - 1 Next i '----値を取り出す For i = 1 To Ud - Ld + 1 d(i, 1) = y(Application.Match(Application.Small(x, i), x, 0)) Next i 'ここまで ' nで座席数を指定 n = Ud With Worksheets("Sheet3") For Each ob In .DrawingObjects If Not Intersect(ob.TopLeftCell, .Range("B1:G20")) Is Nothing Then ob.Delete End If Next .Rows("2:10").RowHeight = 35 .Columns("B:G").ColumnWidth = 15 i = 0 For Each c In .Range("B2:G8") i = i + 1 If n < i Then Exit For Set myShape = .Shapes.AddShape( _ Type:=5, _ Left:=c.Left, _ Top:=c.Top, _ Width:=c.Width - 15, _ Height:=c.Height - 10) myShape.Name = "席" & d(i, 1) Next c .Range("A1").Select End With End Sub |
Sub 仮名を入力() Dim ob As Shape Dim i As Integer i = 0 For Each ob In Worksheets("Sheet3").Shapes If Left(ob.Name, 1) = "席" Then i = i + 1 ob.TextFrame.Characters.Text = "名前" & i End If Next ob End Sub |
Sub 配置() Dim c As Range Dim i As Long ' 数式バーが非表示名なっているので表示します。 Application.DisplayFormulaBar = True ' オートシェイプを並べ替えます。 For Each c In Worksheets("Sheet3").Range("B2:G8") i = i + 1 'エラー回避のため人数分を超えるとループから抜ける If i > 40 Then Exit For With Worksheets("Sheet3").Shapes("席" & i) .Left = c.Left .Top = c.Top End With Next c End Sub |
Sub 配置2() Dim c As Range Dim i As Long Dim waitTime As Variant Application.DisplayFormulaBar = True For Each c In Worksheets("Sheet3").Range("B12:G18") i = i + 1 'エラー回避のため人数分を超えるとループから抜ける If i > 40 Then Exit For '実行を一時停止して1つずつ移動する waitTime = Now + TimeValue("0:00:01") Application.Wait waitTime With Worksheets("Sheet3").Shapes("席" & i) .Left = c.Left .Top = c.Top End With Next c End Sub |
スポンサードリンク
PageViewCounter
Since2006/2/27