VBA实例:一键生成会议座位牌

文摘   2024-06-27 22:49   四川  

大家好,这是Excel小火箭的第46次分享,若有不妥之处,欢迎批评指正。

后台有小伙伴说:会议已经提前排好了坐次图,需要根据座次花名册,自动生成会议座位牌,以便打印粘贴,会议座次图如下:

这是一个标准的会议场地,分为左右两排,每排10个座位,分为左右两边,总之每排固定座位是10个。
今天我们便和大家一起,来探讨下大致的编程路,先看操作视频:
01
编程思路


①实际场地可能有不只左右两排,也许有10排呢,但是每个小排是独立的,即每个小排10个座位,所以我们可以考虑自行粘贴人员名单到我们的模板文件,以防止数据错误,减少定制的量;
②数据清洗,去掉空行、强制换行符等非标准格式;
③将所有数据按字数数量,读取后分别存储到字典;
④将字典中items值作为临时一维数组,分别读取到两列;(小伙伴的需求:列宽按120设置,行高按350设置,刚好可以打印成标准的座位牌)

⑤遍历表格,依次将字典值取出存放到对应表中;调整表格格式,完成。


02
VBA源码


整体来说,程序较为简单,稍微需要动点脑壳的地方可能在于:将一维数组读取到二维数据表。
本篇文章主要是针对Excel不太熟练,希望一键操作可以生成会议座次牌的会议专员们,更多的是想和大家一起探讨一些实际工作过程中可能遇到的各种状况,当然你也可以根据特殊需求,自行修改或者定制模板文件。
按照惯例,附上源码:
Sub getAllName()    '//一百以内的加减法 @公众号【Excel小火箭】    '// 2024-06-27 By Sdx孙大侠    Dim sht As Worksheet    Dim rng As Range, c As Range    Dim d2, d3, d4, d5, d6    Dim i As Integer    Dim lastRow As Long        Dim sht2 As Worksheet    Dim sht3 As Worksheet    Dim sht4 As Worksheet    Dim sht5 As Worksheet    Dim sht6 As Worksheet        Application.ScreenUpdating = False        Set sht = Worksheets("操作界面")    Set sht2 = Worksheets("二个字")    Set sht3 = Worksheets("三个字")    Set sht4 = Worksheets("四个字")    Set sht5 = Worksheets("五个字")    Set sht6 = Worksheets("五个字以上")        lngRow = getLastRow(sht)        Set rng = sht.Range("A1:J" & lngRow)
Set d2 = CreateObject("Scripting.Dictionary") Set d3 = CreateObject("Scripting.Dictionary") Set d4 = CreateObject("Scripting.Dictionary") Set d5 = CreateObject("Scripting.Dictionary") Set d6 = CreateObject("Scripting.Dictionary") i = 1 For Each c In rng Rem 数据清洗 c.Value = Replace(c.Value, Chr(10), "") c.Value = Replace(c.Value, Space(1), "") c.Value = Replace(c.Value, Space(2), "") c.Value = Replace(c.Value, Space(3), "") c.Value = Replace(c.Value, Space(4), "") If Len(c.Value) = 2 Then d2(i) = c.Value If Len(c.Value) = 3 Then d3(i) = c.Value If Len(c.Value) = 4 Then d4(i) = c.Value If Len(c.Value) = 5 Then d5(i) = c.Value If Len(c.Value) = 6 Then d6(i) = c.Value i = i + 1 Next     Rem 数据读取写入 Call getNamesToSht(sht2, d2) Call getNamesToSht(sht3, d3) Call getNamesToSht(sht4, d4) Call getNamesToSht(sht5, d5) Call getNamesToSht(sht6, d6) Call GetFormatSht(sht2) Call GetFormatSht(sht3) Call GetFormatSht(sht4) Call GetFormatSht(sht5) Call GetFormatSht(sht6) d2.RemoveAll d3.RemoveAll d4.RemoveAll d5.RemoveAll d6.RemoveAll Set sht = Nothing Set sht2 = Nothing Set sht3 = Nothing Set sht4 = Nothing Set sht5 = Nothing Set sht6 = Nothing Application.ScreenUpdating = TrueEnd Sub
Sub getNamesToSht(sht As Worksheet, d)'// 2024-06-27 适用于座次排列'// By Szl孙大侠@Excel小火箭 Dim arr Dim numRow As Long Dim numCol As Long Dim numRows As Long, numCols As Long numRows = (d.Count / 2) + 1 numCols = 2 Dim cellRow As Long, cellCol As Long Dim index As Long index = 0 For cellRow = 1 To numRows For cellCol = 1 To numCols sht.Cells(cellRow, cellCol).Value = arr(index) index = index + 1 If index > UBound(arr) + 1 Then Exit For End If Next cellCol Next cellRow End Sub

希望本期文章对你有所启发,VBA源代码已同步上传,欢迎下载。



推荐阅读

VBA实例:自动关闭Msgbox

VBA实例:字典的详解与应用


-END-

客官都看到这儿了
记得点赞加关注哦
下次更新时间,周日21:35

Excel小火箭
一个专注于Excel技巧分享的公众号