用vba编个程序可以实现。
将一个案例分享给大家。程序页面如下:
部分代码如下:
Private Sub CommandButton4_Click()
'开始抽奖
Dim zb As String, dj As String, rs As Integer
Dim SARR(1 To 5000, 1 To 2) '存放本次抽奖的候选人清单 1-姓名 2-电话号码
'Dim lsARR '存放最近100次的候选人
Dim ZZ1 As Integer, ZZ2 As Integer, ZZ3 As Integer
'Dim jgarr
Dim ysARR(1 To 3, 1 To 3) As Integer '三种颜色参数
Dim zjZD '仅存放姓名+半角分号(;)+4位尾号
Dim myName As String
Dim hxRs As Integer, ZJRS As Integer '候选人数,中奖人数
Const lsRs = 100 '存放100位候选人
Set zjZD = CreateObject("scriptingdictionary")
'ReDim jgarr(1 To ZJRS) As Long
A = 0 '
ysARR(1, 1) = 255: ysARR(1, 2) = 250: ysARR(1, 3) = 0
ysARR(2, 1) = 255: ysARR(2, 2) = 10: ysARR(3, 3) = 10
ysARR(3, 1) = 255: ysARR(3, 2) = 250: ysARR(3, 3) = 0
'清空颜色
For I = 1 To 15
myName = "TextBox" & I
Set xx = MeControls(myName)
xxBackColor = RGB(255, 255, 255)
xxForeColor = RGB(255, 215, 0)
xxFontSize = 10
xxBackStyle = 0
ZZ3 = ZZ3 - 1
If ZZ3 = 0 Then ZZ3 = 15
Next I
zb = ComboBox1Value
dj = ComboBox2Value
ZJRS = ComboBox3Value '中奖人数
'读取还可抽取人数
With Sheets("中奖人数设定")
For I = 3 To 8
If Cells(I, 2) = zb Then Exit For
Next I
For j = 9 To 11
If Cells(2, j) = dj Then Exit For
Next j
kcqrs = Cells(I, j) '可抽取人数
End With
If ZJRS = 0 Or ZJRS > kcqrs Or ZJRS > 15 Then
MsgBox ("抽奖人数设置不正确!")
Exit Sub
End If
ReDim jgarr(1 To ZJRS, 1 To 2)
'读取候选人 放入sarr
Select Case zb
Case "A"
lh = 2
Case "B"
lh = 5
Case "C"
lh = 8
Case "D"
lh = 11
Case "E"
lh = 14
Case "F"
lh = 17
End Select
hxRs = 0
With Sheets("人员清单")
HH = 3
Do While Cells(HH, lh) <> ""
If Cells(HH, lh + 2) = "" Then '检查是否中奖,已经中奖的不得参与摇奖
hxRs = hxRs + 1
SARR(hxRs, 1) = Cells(HH, lh)
SARR(hxRs, 2) = Cells(HH, lh + 1)
End If
HH = HH + 1
Loop
End With
ZZ1 = 0: ZZ2 = 0: ZZ3 = 0
upperbound = hxRs
lowerbound = 1
'1-11:中奖人数和候选人数一样时,单独做一个循环
If ZJRS < hxRs Then GoTo 200
'一样时
Do While True
For ZZ2 = 1 To hxRs
myName = "TextBox" & ZZ2
Set xx = MeControls(myName)
xxText = SARR(ZZ2, 1) & Chr(10) & Right(SARR(ZZ2, 2), 4)
Next ZZ2
DoEvents '释放程序控制权,允许其他事件
Sleep (5) '延时ms
DoEvents '释放程序控制权,允许其他事件
If A = 1 Then GoTo 300
Loop
200:
Do While True
100:
SJS = Int((upperbound - lowerbound + 1) Rnd + lowerbound)
MYKEY = Trim(SARR(SJS, 1)) & ";" & Trim(Right(SARR(SJS, 2), 4))
If zjZDEXISTS(MYKEY) Then
ZZ1 = ZZ1 + 1
If ZZ1 > 10000 Then
MsgBox ("数据异常!!!")
Exit Sub
End If
GoTo 100
End If
'ZZ1 = ZZ1 + 1
'If ZZ1 = 101 Then ZZ1 = 1
ZZ2 = ZZ2 + 1
If ZZ2 = ZJRS + 1 Then ZZ2 = 1
'ZZ3 = ZZ3 + 1
'If ZZ3 = 4 Then ZZ3 = 1
'lsARR(ZZ1) = sjs
myName = "TextBox" & ZZ2
Set xx = MeControls(myName)
'xxText = Left(SARR(SJS, 2), 3) & "XXXX" & Right(SARR(SJS, 2), 4)
xxText = SARR(SJS, 1) & Chr(10) & Right(SARR(SJS, 2), 4)
zjZDRemoveAll
For I = 1 To ZJRS
myName = "TextBox" & I
Set xx = MeControls(myName)
If xxText <> "" Then
MYKEY2 = qczf(Left(xxText, InStr(xxText, Chr(10)) - 1)) & ";" & Right(xxText, 4)
zjZDAdd MYKEY2, I
End If
Next I
'xxBackColor = RGB(ysARR(ZZ3, 1), ysARR(ZZ3, 2), ysARR(ZZ3, 3))
DoEvents '释放程序控制权,允许其他事件
Sleep (5) '延时ms
DoEvents '释放程序控制权,允许其他事件
300:
If A = 1 Then
For I = 1 To ZJRS
myName = "TextBox" & I
Set xx = MeControls(myName)
xxBackColor = RGB(ysARR(1, 1), ysARR(1, 2), ysARR(1, 3))
xxForeColor = RGB(0, 0, 255)
xxFontSize = 20
xxBackStyle = 1
'ZZ3 = ZZ3 - 1
'If ZZ3 = 0 Then ZZ3 = 15
Next I
Exit Sub
End If
Loop
End Sub
Private Sub CommandButton5_Click()
A = 1
End Sub
Private Sub CommandButton6_Click() '记录中奖信息
Dim zjZD
Dim ZJRS
Dim zjArr
zb = ComboBox1Value '组别
dj = ComboBox2Value '等级
ZJRS = ComboBox3Value '中奖人数
Set zjZD = CreateObject("scriptingdictionary")
'遍历文本框,获取中奖的电话号码
For I = 1 To ZJRS
myName = "TextBox" & I
Set xx = MeControls(myName)
ARR = Split(xxText, Chr(10))
MYTEXT = qczf(ARR(0)) & ";" & qczf(ARR(1))
zjZDAdd MYTEXT, I
xxText = ""
xxBackColor = RGB(255, 255, 255)
Next I
Select Case zb
Case "A"
lh = 2
Case "B"
lh = 5
Case "C"
lh = 8
Case "D"
lh = 11
Case "E"
lh = 14
Case "F"
lh = 17
End Select
With Sheets("人员清单")
For I = 3 To Cells(10000, lh)End(xlUp)Row
'SARR(SJS, 1) & Chr(10) & Right(SARR(SJS, 2), 4)
'mytext = Left(Cells(I, lh + 1)Text, 3) & Right(Cells(I, lh + 1)Text, 4)
MYTEXT = qczf(Cells(I, lh)Text) & ";" & qczf(Cells(I, lh + 1)Text)
If zjZDEXISTS(MYTEXT) Then
Cells(I, lh + 2) = dj
End If
Next I
End With
End Sub
Private Sub Frame2_Click()
xxx = 1
End Sub
Private Sub UserForm_Initialize()
Dim xstr(1 To 6) As String '保存每列的数据
Dim ystr(1 To 3) As String
Dim zstr(1 To 15) As Integer '
xstr(1) = "A"
xstr(2) = "B"
xstr(3) = "C"
xstr(4) = "D"
xstr(5) = "E"
xstr(6) = "F"
ComboBox1List = xstr
ystr(1) = "一等奖"
ystr(2) = "二等奖"
ystr(3) = "三等奖"
ComboBox2List = ystr
For I = 1 To 15
zstr(I) = I
Next I
ComboBox3List = zstr
ComboBox3Value = 15
End Sub
我用多线程写的,你可以参考下。
系统界面图如下:(注:重置功能为清除已经中奖号码,回归1-100都可以中奖状态。)
代码:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SystemThreadingThread g_Thread = null;
List<int> g_list = new List<int>();
int g_Num = 0;
delegate void g_delegate();
/// <summary>
/// 开始
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
if(g_listCount==100)
{
MessageBoxShow("所有数字已使用,请重置!");
return;
}
button3Enabled = true;
Random r = new Random();
g_delegate dele= ChangeTxt;
g_Thread = new SystemThreadingThread(() =>
{
while(thisIsDisposed==false)
{
g_Num = rNext(1, 100);
if(g_listContains(g_Num))
{
g_Num = 1;
}
while(g_listContains(g_Num))
{
g_Num++;
}
thisInvoke(dele);
SystemThreadingThreadSleep(50);
}
});
g_ThreadStart();
}
void ChangeTxt()
{
label2Text = g_NumToString();
}
/// <summary>
/// 停止
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
//if(g_ThreadThreadState==SystemThreadingThreadStateRunning)
{
g_ThreadAbort();
g_listAdd(ConvertToInt32(label2Text));
}
button3Enabled = false;
}
/// <summary>
/// 重置
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
g_listClear();
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
SystemEnvironmentExit(0);
}
}
我写给你,如果是本专业,以后要多多学习。JDK 16
import javaawtGridLayout;
import javaawteventMouseEvent;
import javaawteventMouseListener;
import javautilIterator;
import javautilRandom;
import javautilSet;
import javautilTreeSet;
import javaxswingJButton;
import javaxswingJFrame;
import javaxswingJTextField;
public class LuckyDrawn {
private JFrame f = new JFrame("Lucky Drawn");
private JButton btn = new JButton("Drawn");
private JTextField result = new JTextField("Result");
private static final int MIN = 1;
private static final int MAX = 30;
private static final int RESULT_COUNT = 7;
public LuckyDrawn(){
fadd(btn);
fadd(result);
resultsetEditable(false);
btnaddMouseListener(new MouseListener(){
public void mouseClicked(MouseEvent e) {
Random rand = new Random();
Set<Integer> set = new TreeSet<Integer>();
while(setsize() < RESULT_COUNT){
setadd(new Integer(randnextInt(MAX)+ 1));
}
StringBuffer sb = new StringBuffer("Lucky numbers are: ");
for (Iterator iter = setiterator(); iterhasNext();) {
sbappend(((Integer) iternext())intValue())append(", ");
}
resultsetText(sbsubstring(0, sblength() - 2)toString());
fpack();
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
});
fsetLayout(new GridLayout(2, 1));
fsetVisible(true);
fsetLocation(500, 200);
fpack();
fvalidate();
fsetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);
}
public static void main(String[] args) {
new LuckyDrawn();
}
}
16年年会抽奖网上有人对公司的抽奖结果又偏见,于是全员进行了抽奖代码的review,好像是爱奇艺公司的,下面用python来实现一个抽奖程序。
主要功能有
1从一个csv文件中读入所有员工工号
2将这些工号初始到一个列表中
3用random模块下的choice函数来随机选择列表中的一个工号
4抽到的奖项的工号要从列表中进行删除,以免再次抽到
初级版
这个比较简单,缺少定制性,如没法设置一等奖有几名,二等奖有几名
import csv#创建一个员工列表emplist = []#用with自动关闭文件with open('c://empscsv') as f:
empf = csvreader(f) for emp in empf:
emplistappend(emp)
print("进行一等奖抽奖,共有一名")import random#利用random模块的chice函数来从列表中随机选取一个元素e1 = randomchoice(emplist)#将中奖的员工从列表中剔除emplistremove(e1)
print('一等奖得主的号码是 %s' % e1)
print('进行三个二等奖的号码抽奖')
e2_1 = randomchoice(emplist)
emplistremove(e2_1)
e2_2 = randomchoice(emplist)
emplistremove(e2_2)
e2_3 = randomchoice(emplist)
emplistremove(e2_3)
print('获得3个二等奖是 %s %s %s',(e2_1,e2_2,e2_3))#下面依次类推可以设置三等奖的抽奖123456789101112131415161718192021222324
改进版
上面的那个初级版,假如要设置个三等奖一百名那么将要重新维护几百行代码,下面用比较高级点的办法实现
我们考虑用面向对象来实现,设计一个抽奖类,类中包含一个属性(号码来源),一个方法:产生所有抽奖层次指定个数的抽奖号码。
用到如下知识点:
1 csv模块部分函数用法
2 sys模块读取输入
3 random模块函数choice函数用法
4 列表和字典元素的添加、删除
6 for循环中range用法
7 类和面向对象
8 字符打印,print中的计算
9open中with
#!/usr/bin/python#coding=utf-8import csvimport sysimport random
reload(sys)
syssetdefaultencoding('utf8')#coding=utf-8print("开始进行抽奖")#定义个抽奖类,功能有输入抽奖级别和个数,打印出每个级别的抽奖员工号码class Choujiang:
#定义scv文件路径
def __init__(self,filepath):
selfempfile = filepath def creat_num(self):
emplist = [] with open(selfempfile) as f:
empf = csvreader(f) for emp in empf:
emplistappend(emp)
print('共有%s 人参与抽奖' % len(emplist))
levels = int(input('抽奖分几个层次,请输入:')) #定义一个字典
level_dict = {} for i in range(0,levels):
print('请输入当前获奖层次 %s 对应的奖品个数' % ( i + 1))
str_level_dict_key = sysstdinreadline()
int_level_dict_key = int(str_level_dict_key)
level_dict[i] = int_level_dict_key #循环完成后抽奖层次字典构造完毕
#进行抽奖开始
print('抽奖字典设置为: %s' % level_dict) for i in range(0,len(level_dict)):
winers = [] #产生当前抽奖层次i对应的抽奖个数
for j in range(0,int(level_dict[i])): #利用random模块中的choice函数从列表中随机产生一个
winer = randomchoice(emplist)
winersappend(winer)
emplistremove(winer)
print('抽奖层次 %s 下产出的获奖人员有:' % (i + 1 ))
print(winers)#类功能定义完毕,开始初始化并使用if __name__ == '__main__':
peoples = Choujiang('c://empscsv')
peoplescreat_num()1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
该段程序在python 26 以上及 3中均可以运行,运行结果如下图:
Python 343 (v343:9b73f1c3e601, Feb 24 2015, 22:44:40) [MSC v1600 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information>>> ================================ RESTART ================================>>> 开始进行抽奖
共有24790 人参与抽奖
抽奖分几个层次,请输入:2请输入当前获奖层次 1 对应的奖品个数1请输入当前获奖层次 2 对应的奖品个数3抽奖字典设置为: {0: 1, 1: 3}
抽奖层次 1 下产出的获奖人员有:
[['张三19826']]
抽奖层次 2 下产出的获奖人员有:
[['张三18670'], ['张三23235'], ['张三15705']]>>> 1234567891011121314151617
以上就是关于EXCEL 抽奖 如何 多次抽奖不重复全部的内容,包括:EXCEL 抽奖 如何 多次抽奖不重复、vb.net 多次滚动抽奖的程序代码、用java编写一个彩票抽奖程序等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)