XLS+VBA
모든 행 값을 랜덤으로 섞기
쵸코난
2024. 3. 1. 01:37
엑셀에서 모든 행의 값을 섞어주는 코드입니다.
빨간 버튼은 행 전체를 순서를 바꿔주고
파란 버튼은 현재 열에 한해서 내부의 셀값을 섞어줍니다.
Option Explicit
'// mix the entire row
Sub Shuffle()
Dim sht As Worksheet
Dim Rng As Range
Dim lastRow As Long, l As Long, r As Long
Dim t As Variant
Randomize
Application.ScreenUpdating = False
Set sht = ActiveSheet
lastRow = sht.Cells(sht.Rows.Count, "A").End(xlUp).Row
For l = 1 To lastRow
r = CLng(Rnd * (lastRow - 1)) + 1 '// 1...(LastRow)
t = sht.Rows(l).Value
sht.Rows(l).Value = sht.Rows(r).Value
sht.Rows(r).Value = t
Next l
Application.ScreenUpdating = True
End Sub
'// mix the cells only in the current column
Sub ShuffleColumn()
Dim sht As Worksheet
Dim Target As Range, Rng As Range
Dim t As String
Dim r As Long, ccol As Long, lastRow As Long
Randomize
Set sht = ActiveSheet
ccol = ActiveCell.Column
lastRow = sht.Cells(sht.Rows.Count, ccol).End(xlUp).Row
Set Target = sht.Range(sht.Cells(1, ccol), sht.Cells(lastRow, ccol))
For Each Rng In Target
r = CLng(Rnd * (Target.Count - 2)) + 1 '//1...(Count-1)
t = Rng.Text
Rng = Target(r).Text
Target(r) = t
Next Rng
End Sub
실행 화면:
>> 용도
- 단어리스트 섞기
- 문장 순서 섞기
- 순서 추첨
- 당첨자 무작위 추첨
- 조원 추첨
>>예시 파일