席替えを自動で行う(SortedListクラスを使う):Excel VBA入門 |
スポンサードリンク | |
Sub seat01() Dim c As Range, ob As Object Dim myShape As Shape Dim i As Long, n As Long ' nで座席数(生徒数)を指定 n = Application.WorksheetFunction.CountA(Worksheets("Sheet2").Range("B:B")) - 1 With Worksheets("Sheet1") ' B1:G20の範囲のオブジェクトを削除 For Each ob In .DrawingObjects If Not Intersect(ob.TopLeftCell, .Range("B1:G20")) Is Nothing Then ob.Delete End If Next ' 行高さ、列幅を指定 .Rows("1:10").RowHeight = 35 .Columns("B:G").ColumnWidth = 15 ' 座席を描画する、描画したオートシェイプの名前は「座席」+数字としています 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 = "座席" & i myShape.TextFrame.Characters.Text = myShape.Name Next c ' 教卓を描画しています Set myShape = .Shapes.AddShape( _ Type:=5, _ Left:=.Range("D1").Left + 30, _ Top:=.Range("D1").Top, _ Width:=.Range("D1").Width, _ Height:=.Range("D1").Height - 10) myShape.Name = "教卓" myShape.TextFrame.Characters.Text = myShape.Name .Range("A1").Select End With End Sub |
Sub seki_B1() Dim c As Range, i As Long Dim DataList As Object Set DataList = CreateObject("System.Collections.SortedList") Randomize For Each c In Worksheets("Sheet2").Range("B2:B41") ' 名前のリスト範囲 DataList.Item(Rnd()) = c.Value Next For i = 0 To DataList.Count - 1 '0から始まっていますので-1としてあります Worksheets("Sheet1").Shapes("座席" & i + 1).TextFrame.Characters.Text = DataList.GetByIndex(i) Next i Set DataList = Nothing End Sub |
スポンサードリンク
PageViewCounter
Since2006/2/27