三天的努力,工资系统初步完成,尽管界面很简陋,尽管代码冗长,尽管点击后要等一分多钟程序才运行完毕…尽管有很多不尽人意的地方,于个人而言却是突破。
想象高手的境界,可能只需要三分之一的代码量信手拈来,同时兼顾美观与人性化设计,但对我而言的第一个程序,对工作能力有实质性提升的东西,从毫无思路到成品逐渐成型过程,这感觉不可言喻。
我想学习的目的在于开拓思路。就像一个人只会加法,不知道有乘法,那么他就慢慢地被工作压的喘不出气,那么每天挤出一点时间,学习和了解本行业的知识,原来还有乘法,也许困扰许久的问题就迎刃而解。
跳出习惯并不容易。
初学vba很容易串联习惯的方式,也许写一个函数,或者万能的复制粘贴,或者用数据透视表,又或者用sql语句,很容易就能完成写大段晦涩的代码这种出力不讨好的事。但并不是所学无用,vba不是万能的,同样所有熟悉的功能也一样,也许某天突然发现,咦,这里用点vba技术和其他功能结合使用会产生意想不到的效果,达到加法与乘法结合使用妙处。
带有目标性是学习最好的捷径。
想完成什么样的目标,达到什么样的效果,在编写过程中迫使自己查阅相关资料,同样对基础知识加深理解。泛读知道它有什么功能,带目标知道怎么查到想要加深理解的内容。
先理清思路,然后用一种简化的方式编写,最后再编写代码看起来很长的程序。
思路是串联长程序的基础,看起来很长的代码,其实是一个个子程序的集合,先自己弄一种整体代码的简化版,然后一点点补充子程序,比直接写长程序要更节省时间,单纯写代码并不耗用多少时间。
由于基础数据并不规范,大段代码其实都在规整数据,从工作角度而言,基础数据的整合规范比用代码整合更省心省力。
细节处才是高手与业余的分水领。业余就是大东西做出来了,离完美还差30分,不是想不到,而是真心不会啊!
Option Explicit '
'整体思路,基础数据不太规范,先通过代码规整数据到一张表,再简单的汇总就完成,由于数据量大 _
能用数组提速尽量提速。普通循环太慢,用字典虚拟关键字的方式,把所有数据逐条写入。
Sub 工资汇总()
Application.ScreenUpdating = False '关闭屏幕更新提速
Worksheets("工资汇总").Range("a:f").ClearContents '清除工资汇总区域数据,数据更新时防错
Dim d As Object, arr, arr1, arr2, arr3, arr4, arr5, icount&
Dim n%, n1%, n2%, n3%, n4%, m%, m1%, m2%, m3%, k
Set d = CreateObject("scripting.dictionary") '声明字典
icount = Application.WorksheetFunction.CountA(Worksheets("成本数据录入").[a:a]) '将成本数据录入表的A列计算非空单元格数量,方便后续定义区域
arr = Worksheets("成本数据录入").Range("a2:b" & icount)
arr1 = Worksheets("成本数据录入").Range("ay2:az" & icount) '将需要核算的工资数据写入数组,不在一个区域所以分别写入
arr2 = Worksheets("成本数据录入").Range("ba2:bb" & icount)
arr3 = Worksheets("成本数据录入").Range("bc2:bd" & icount)
arr4 = Worksheets("成本数据录入").Range("be2:bf" & icount)
arr5 = Worksheets("成本数据录入").Range("bg2:bh" & icount)
For n = 1 To UBound(arr)
d(n) = Array(arr(n, 1), arr(n, 2), arr1(n, 1), arr1(n, 2)) '虚拟关键字,将对应数组合并写入条目
Next
For n1 = UBound(arr) + 1 To UBound(arr) * 2
m = m + 1
d(n1) = Array(arr(m, 1), arr(m, 2), arr2(m, 1), arr2(m, 2)) '逐条写入,下同不再一一标注'
Next
For n2 = UBound(arr) * 2 + 1 To UBound(arr) * 3
m1 = m1 + 1
d(n2) = Array(arr(m1, 1), arr(m1, 2), arr3(m1, 1), arr3(m1, 2))
Next
For n3 = UBound(arr) * 3 + 1 To UBound(arr) * 4
m2 = m2 + 1
d(n3) = Array(arr(m2, 1), arr(m2, 2), arr4(m2, 1), arr4(m2, 2))
Next
For n4 = UBound(arr) * 4 + 1 To UBound(arr) * 5
m3 = m3 + 1
d(n4) = Array(arr(m3, 1), arr(m3, 2), arr5(m3, 1), arr5(m3, 2))
Next
k = d.items
Worksheets("工资汇总").Range("a2:d" & d.Count) = Application.Transpose(Application.Transpose(k)) '将条目转换成数组形式写入工资汇总对应区域
Worksheets("工资汇总").[a1:f1] = Array("日期", "班组", "姓名", "效益工资", "临时工时工资", "喷漆加工工资") '加标头
Dim d1 As Object, arrr, arrr1, arrr2, arrr3, arrr4, arrr5, arrr6, arrr7, arrr8, arrr9, arrr10, arrr11
Dim arrr12, arrr13, arrr14, arrr15, arrr16, arrr17, arrr18, arrr19, arrr20, arrr21, arrr22, arrr23, arrr24, arrr25, arrr26
Dim iicount%, i%, i1%, i2%, i3%, i4%, i5%, i6%, i7%, i8%, i9%, i10%, i11%, i12%, i13%, i14%, i15%, i16%, i17%, i18%, i19%, i20%, i21%, i22%, i23%, i24%
Dim j%, j1%, j2%, j3%, j4%, j5%, j6%, j7%, j8%, j9%, j10%, j11%, j12%, j13%, j14%, j15%, j16%, j17%, j18%, j19%, j20%, j21%, j22%, j23%, j24%, j25%
Dim k1, kk, kk1
Set d1 = CreateObject("scripting.dictionary") '第二部分思路与第一部分思路大体相同
iicount = Application.WorksheetFunction.CountA(Worksheets("外协临时工时").[a:a])
arrr = Worksheets("外协临时工时").Range("a2:a" & iicount)
arrr1 = Worksheets("外协临时工时").Range("f2:f" & iicount)
arrr2 = Worksheets("外协临时工时").Range("g2:g" & iicount)
arrr3 = Worksheets("外协临时工时").Range("h2:h" & iicount)
arrr4 = Worksheets("外协临时工时").Range("i2:i" & iicount)
arrr5 = Worksheets("外协临时工时").Range("j2:j" & iicount)
arrr6 = Worksheets("外协临时工时").Range("k2:k" & iicount)
arrr7 = Worksheets("外协临时工时").Range("l2:l" & iicount)
arrr8 = Worksheets("外协临时工时").Range("m2:m" & iicount)
arrr9 = Worksheets("外协临时工时").Range("n2:n" & iicount)
arrr10 = Worksheets("外协临时工时").Range("o2:o" & iicount)
arrr11 = Worksheets("外协临时工时").Range("p2:p" & iicount)
arrr12 = Worksheets("外协临时工时").Range("q2:q" & iicount)
arrr13 = Worksheets("外协临时工时").Range("r2:r" & iicount)
arrr14 = Worksheets("外协临时工时").Range("s2:s" & iicount)
arrr15 = Worksheets("外协临时工时").Range("t2:t" & iicount)
arrr16 = Worksheets("外协临时工时").Range("u2:u" & iicount)
arrr17 = Worksheets("外协临时工时").Range("v2:v" & iicount)
arrr18 = Worksheets("外协临时工时").Range("w2:w" & iicount)
arrr19 = Worksheets("外协临时工时").Range("x2:x" & iicount)
arrr20 = Worksheets("外协临时工时").Range("y2:y" & iicount)
arrr21 = Worksheets("外协临时工时").Range("z2:z" & iicount)
arrr22 = Worksheets("外协临时工时").Range("aa2:aa" & iicount)
arrr23 = Worksheets("外协临时工时").Range("ab2:ab" & iicount)
arrr24 = Worksheets("外协临时工时").Range("ac2:ac" & iicount)
arrr25 = Worksheets("外协临时工时").Range("ad2:ad" & iicount)
arrr26 = Worksheets("外协临时工时").Range("ax2:ax" & iicount)
For i = 1 To UBound(arrr)
d1(i) = Array(arrr(i, 1), arrr1(i, 1), arrr26(i, 1))
Next
For i1 = 1 + UBound(arrr) To UBound(arrr) * 2
j = j + 1
d1(i1) = Array(arrr(j, 1), arrr2(j, 1), arrr26(j, 1))
Next
For i2 = 1 + UBound(arrr) * 2 To UBound(arrr) * 3
j1 = j1 + 1
d1(i2) = Array(arrr(j1, 1), arrr3(j1, 1), arrr26(j1, 1))
Next
For i3 = 1 + UBound(arrr) * 3 To UBound(arrr) * 4
j2 = j2 + 1
d1(i3) = Array(arrr(j2, 1), arrr4(j2, 1), arrr26(j2, 1))
Next
For i4 = 1 + UBound(arrr) * 4 To UBound(arrr) * 5
j3 = j3 + 1
d1(i4) = Array(arrr(j3, 1), arrr5(j3, 1), arrr26(j3, 1))
Next
For i5 = 1 + UBound(arrr) * 5 To UBound(arrr) * 6
j4 = j4 + 1
d1(i5) = Array(arrr(j4, 1), arrr6(j4, 1), arrr26(j4, 1))
Next
For i6 = 1 + UBound(arrr) * 6 To UBound(arrr) * 7
j5 = j5 + 1
d1(i6) = Array(arrr(j5, 1), arrr7(j5, 1), arrr26(j5, 1))
Next
For i7 = 1 + UBound(arrr) * 7 To UBound(arrr) * 8
j6 = j6 + 1
d1(i7) = Array(arrr(j6, 1), arrr8(j6, 1), arrr26(j6, 1))
Next
For i8 = 1 + UBound(arrr) * 8 To UBound(arrr) * 9
j7 = j7 + 1
d1(i8) = Array(arrr(j7, 1), arrr9(j7, 1), arrr26(j7, 1))
Next
For i9 = 1 + UBound(arrr) * 9 To UBound(arrr) * 10
j8 = j8 + 1
d1(i9) = Array(arrr(j8, 1), arrr10(j8, 1), arrr26(j8, 1))
Next
For i10 = 1 + UBound(arrr) * 10 To UBound(arrr) * 11
j9 = j9 + 1
d1(i10) = Array(arrr(j9, 1), arrr11(j9, 1), arrr26(j9, 1))
Next
For i11 = 1 + UBound(arrr) * 11 To UBound(arrr) * 12
j10 = j10 + 1
d1(i11) = Array(arrr(j10, 1), arrr12(j10, 1), arrr26(j10, 1))
Next
For i12 = 1 + UBound(arrr) * 12 To UBound(arrr) * 13
j11 = j11 + 1
d1(i12) = Array(arrr(j11, 1), arrr13(j11, 1), arrr26(j11, 1))
Next
For i13 = 1 + UBound(arrr) * 13 To UBound(arrr) * 14
j12 = j12 + 1
d1(i13) = Array(arrr(j12, 1), arrr14(j12, 1), arrr26(j12, 1))
Next
For i14 = 1 + UBound(arrr) * 14 To UBound(arrr) * 15
j13 = j13 + 1
d1(i14) = Array(arrr(j13, 1), arrr15(j13, 1), arrr26(j13, 1))
Next
For i15 = 1 + UBound(arrr) * 15 To UBound(arrr) * 16
j14 = j14 + 1
d1(i15) = Array(arrr(j14, 1), arrr16(j14, 1), arrr26(j14, 1))
Next
For i16 = 1 + UBound(arrr) * 16 To UBound(arrr) * 17
j15 = j15 + 1
d1(i16) = Array(arrr(j15, 1), arrr17(j15, 1), arrr26(j15, 1))
Next
For i17 = 1 + UBound(arrr) * 17 To UBound(arrr) * 18
j16 = j16 + 1
d1(i17) = Array(arrr(j16, 1), arrr18(j16, 1), arrr26(j16, 1))
Next
For i18 = 1 + UBound(arrr) * 18 To UBound(arrr) * 19
j17 = j17 + 1
d1(i18) = Array(arrr(j17, 1), arrr19(j17, 1), arrr26(j17, 1))
Next
For i19 = 1 + UBound(arrr) * 19 To UBound(arrr) * 20
j18 = j18 + 1
d1(i19) = Array(arrr(j18, 1), arrr20(j18, 1), arrr26(j18, 1))
Next
For i20 = 1 + UBound(arrr) * 20 To UBound(arrr) * 21
j19 = j19 + 1
d1(i20) = Array(arrr(j19, 1), arrr21(j19, 1), arrr26(j19, 1))
Next
For i21 = 1 + UBound(arrr) * 21 To UBound(arrr) * 22
j20 = j20 + 1
d1(i21) = Array(arrr(j20, 1), arrr22(j20, 1), arrr26(j20, 1))
Next
For i22 = 1 + UBound(arrr) * 22 To UBound(arrr) * 23
j21 = j21 + 1
d1(i22) = Array(arrr(j21, 1), arrr23(j21, 1), arrr26(j21, 1))
Next
For i23 = 1 + UBound(arrr) * 23 To UBound(arrr) * 24
j22 = j22 + 1
d1(i23) = Array(arrr(j22, 1), arrr24(j22, 1), arrr26(j22, 1))
Next
For i24 = 1 + UBound(arrr) * 24 To UBound(arrr) * 25
j23 = j23 + 1
d1(i24) = Array(arrr(j23, 1), arrr25(j23, 1), arrr26(j23, 1))
Next
k1 = Application.Transpose(Application.Transpose(d1.items))
For kk1 = 1 To d1.Count
Worksheets("工资汇总").Range("a" & d.Count + kk1) = k1(kk1, 1) '用循环的方式将数组写入相应单元格
Worksheets("工资汇总").Range("c" & d.Count + kk1) = k1(kk1, 2)
Worksheets("工资汇总").Range("e" & d.Count + kk1) = k1(kk1, 3)
Next
Dim d3 As Object, ar1, ar2, ar3, ar4, ar5, ar6, ar7, ar8, ar9, ar10, ar11, ar0
Dim iiicount%, p1%, p2%, p3%, p4%, p5%, p6%, p7%, p8%, p9%, p10%
Dim q2%, q3%, q4%, q5%, q6%, q7%, q8%, q9%, q10%, kkk, kkk1%
Set d3 = CreateObject("scripting.dictionary")
iiicount = Application.WorksheetFunction.CountA(Worksheets("喷漆和加工产品").[a:a])
ar1 = Worksheets("喷漆和加工产品").Range("a2:a" & iiicount)
ar2 = Worksheets("喷漆和加工产品").Range("g2:g" & iiicount)
ar3 = Worksheets("喷漆和加工产品").Range("h2:h" & iiicount)
ar4 = Worksheets("喷漆和加工产品").Range("i2:i" & iiicount)
ar5 = Worksheets("喷漆和加工产品").Range("j2:j" & iiicount)
ar6 = Worksheets("喷漆和加工产品").Range("k2:k" & iiicount)
ar7 = Worksheets("喷漆和加工产品").Range("l2:l" & iiicount)
ar8 = Worksheets("喷漆和加工产品").Range("m2:m" & iiicount)
ar9 = Worksheets("喷漆和加工产品").Range("n2:n" & iiicount)
ar10 = Worksheets("喷漆和加工产品").Range("o2:o" & iiicount)
ar11 = Worksheets("喷漆和加工产品").Range("p2:p" & iiicount)
ar0 = Worksheets("喷漆和加工产品").Range("x2:x" & iiicount)
For p1 = 1 To UBound(ar1)
d3(p1) = Array(ar1(p1, 1), ar2(p1, 1), ar0(p1, 1))
Next
For p2 = 1 + UBound(ar1) To UBound(ar1) * 2
q2 = q2 + 1
d3(p2) = Array(ar1(q2, 1), ar3(q2, 1), ar0(q2, 1))
Next
For p3 = 1 + UBound(ar1) * 2 To UBound(ar1) * 3
q3 = q3 + 1
d3(p3) = Array(ar1(q3, 1), ar4(q3, 1), ar0(q3, 1))
Next
For p4 = 1 + UBound(ar1) * 3 To UBound(ar1) * 4
q4 = q4 + 1
d3(p4) = Array(ar1(q4, 1), ar5(q4, 1), ar0(q4, 1))
Next
For p5 = 1 + UBound(ar1) * 4 To UBound(ar1) * 5
q5 = q5 + 1
d3(p5) = Array(ar1(q5, 1), ar6(q5, 1), ar0(q5, 1))
Next
For p6 = 1 + UBound(ar1) * 5 To UBound(ar1) * 6
q6 = q6 + 1
d3(p6) = Array(ar1(q6, 1), ar7(q6, 1), ar0(q6, 1))
Next
For p7 = 1 + UBound(ar1) * 7 To UBound(ar1) * 8
q7 = q7 + 1
d3(p7) = Array(ar1(q7, 1), ar8(q7, 1), ar0(q7, 1))
Next
For p8 = 1 + UBound(ar1) * 8 To UBound(ar1) * 9
q8 = q8 + 1
d3(p8) = Array(ar1(q8, 1), ar9(q8, 1), ar0(q8, 1))
Next
For p9 = 1 + UBound(ar1) * 9 To UBound(ar1) * 10
q9 = q9 + 1
d3(p9) = Array(ar1(q9, 1), ar10(q9, 1), ar0(q9, 1))
Next
For p10 = 1 + UBound(ar1) * 10 To UBound(ar1) * 11
q10 = q10 + 1
d3(p10) = Array(ar1(q10, 1), ar11(q10, 1), ar0(q10, 1))
Next
kkk = Application.Transpose(Application.Transpose(d3.items))
For kkk1 = 1 To d3.Count
Worksheets("工资汇总").Range("a" & d.Count + d1.Count + kkk1) = kkk(kkk1, 1)
Worksheets("工资汇总").Range("c" & d.Count + d1.Count + kkk1) = kkk(kkk1, 2)
Worksheets("工资汇总").Range("f" & d.Count + d1.Count + kkk1) = kkk(kkk1, 3)
Next
Application.ScreenUpdating = True '屏幕更新开启
Set d = Nothing '释放字典内存
Set d1 = Nothing
Set d3 = Nothing
End Sub