制作原因
最近女朋友太多,想随机抽选几个女朋友约会,遂制作该表格。
功能
1. 不重复滚动随机抽选三名女朋友
2. 检测来不了的女朋友,再次抽选填充该位置
未完成功能
1. 将抽选的女朋友,生成审批单报老板审阅。
2. 记录抽选记录
界面如下图

vba代码
Sub delay(T As Single)
Dim time1 As Single
time1 = Timer
Do
DoEvents
Loop While Timer - time1 < T
End Sub
Sub 清除名单()
Range("A4:G65536").ClearContents
End Sub
Sub 开始()
If Sheet1.Buttons(Application.Caller).Caption = "停止抽选" Then
Sheet1.Buttons(Application.Caller).Caption = "开始抽选"
ElseIf Sheet1.Buttons(Application.Caller).Caption = "开始抽选" Then
Sheet1.Buttons(Application.Caller).Caption = "停止抽选"
Call 开始抽选
End If
End Sub
Sub 开始抽选()
Sheet2.Range("T:BZ").ClearContents
hang = Sheet2.Range("B65536").End(xlUp).Row
helpRow = 1
For dataRow = 3 To hang
flag = 0
selectCon = condition()
If InStr(1, Sheet2.Cells(dataRow, "F"), selectCon) > 0 Or Sheet2.Cells(dataRow, "F") = "" Then
flag = flag + 1
End If
If flag > 0 Then
helpRow = helpRow + 1
Sheet2.Range("A" & dataRow & ":G" & dataRow).Copy Sheet2.Cells(helpRow, "AA")
Sheet2.Cells(helpRow, "AH") = dataRow
End If
Next
break = 1
If IsEmpty(Sheet1.Range("A4").Value) Then
sdatarow = 4
ElseIf IsEmpty(Sheet1.Range("A5").Value) Then
sdatarow = 5
ElseIf IsEmpty(Sheet1.Range("A6").Value) Then
sdatarow = 6
Else
Sheet1.[按钮 1].Caption = "开始抽选"
Sheet1.[按钮 1].Enabled = False
MsgBox prompt:="抽选名单已满三人!", Buttons:=vbOKOnly + vbInformation, Title:="提示"
Exit Sub
End If
Do
If Sheet1.Buttons(Application.Caller).Caption = "开始抽选" Then Exit Do
AAA:
delay (0.01)
randomNum = Application.RandBetween(2, helpRow)
hang = Sheet2.Cells(randomNum, "AH")
If break = 20 Then
Sheet1.[按钮 1].Caption = "开始抽选"
Sheet1.[按钮 1].Enabled = False
MsgBox prompt:="没有更多可满足条件的专家抽选了", Buttons:=vbExclamation, Title:="提示"
Exit Do
End If
If sdatarow = 4 Then
If Sheet2.Cells(hang, "B") = Cells(sdatarow + 1, "B") Or Sheet2.Cells(hang, "B") = Cells(sdatarow + 2, "B") Then
break = break + 1
GoTo AAA
End If
ElseIf sdatarow = 5 Then
If Sheet2.Cells(hang, "B") = Cells(sdatarow - 1, "B") Or Sheet2.Cells(hang, "B") = Cells(sdatarow + 1, "B") Then
break = break + 1
GoTo AAA
End If
ElseIf sdatarow = 6 Then
If Sheet2.Cells(hang, "B") = Cells(sdatarow - 1, "B") Or Sheet2.Cells(hang, "B") = Cells(sdatarow - 2, "B") Then
break = break + 1
GoTo AAA
End If
End If
delay (0.1)
Sheet1.Range("A" & sdatarow & ":G" & sdatarow).Interior.Color = 10079487
Sheet2.Range("A" & hang & ":G" & hang).Copy Sheet1.Range("A" & sdatarow)
Loop
sdatarow = sdatarow + 1
Sheet2.Columns("AA").Resize(, 10).Delete
End Sub
Sub AddWorksheet()
Dim flag As Boolean
For Each Sheet In Sheets
If Sheet.Name = Date & "抽选结果" Then
flag = True
Exit For
End If
Next
If flag = False Then
Sheets.Add After:=Sheets(Sheets.count)
ActiveSheet.Name = Date & "抽选结果"
End If:
Sheets("专家库").Range("J5:P10").Copy
With Sheets(Date & "抽选结果").Range("A1")
.PasteSpecial xlPasteColumnWidths
.PasteSpecial xlPasteAll
End With
Sheets(Date & "抽选结果").Cells(1, "A") = Date & "获选名单"
ThisWorkbook.Save
End Sub
Function condition()
Dim count As Integer
Set lst = Sheet1.[下拉框 5]
cValue = lst.List(lst.Value)
condition = cValue
End Function
excel下载地址
https:
感谢@小小涩郎 |