VBA 脚本



Sub test()
Dim sheet1 As Worksheet
Set sheet1 = ThisWorkbook.Worksheets("计件")
If TypeName(sheet1.Cells(437, 8).Value) = "Double" Then
MsgBox (TypeName(sheet1.Cells(437, 8).Value))
End If
MsgBox (TypeName(sheet1.Cells(437, 8).Value))
End Sub
Sub compute_salary()




Dim sheet1 As Worksheet
Set sheet1 = ThisWorkbook.Worksheets("计件")


Dim row_num, n As Integer
n = 3
Do Until IsEmpty(sheet1.Cells(n, 1).Value)
n = n + 1
Loop
n = n - 1                                                                  'the rows of the valid data  from 1
Debug.Print ("total_record:" & n)                                                                         ' veriry the total row
                                                                           
Dim m(10000, 2) As String
If n > 10000 Then
   MsgBox ("the total row is bigger then 10000,please slice it apart")
   Exit Sub
End If


If n = 3 Then
MsgBox ("no data to aggregate,program will exit")
Exit Sub
End If
                                                                            'load the name and pay into array
Dim i, j As Integer
i = 3
j = 0                                                                       ' j is the actual unhidden row  from 0
Do Until i > n
   If sheet1.Rows(i).Hidden = False Then
       m(j, 0) = sheet1.Cells(i, 2).Value                        'the employee name
       If TypeName(sheet1.Cells(i, 8).Value) = "Double" Then
              m(j, 1) = sheet1.Cells(i, 8).Value                        'the employee pay  should change later A1
       ElseIf TypeName(sheet1.Cells(i, 8).Value) = "Empty" Then
              m(j, 1) = 0
       Else
              MsgBox ("the row:" & i & " The num: " & 8 & " is not numeric please verify the cell type ")
              Exit Sub
       End If
       j = j + 1
   End If
   i = i + 1
Loop
i = i - 1
j = j - 1
Debug.Print ("unhidden record:" & j)


'Debug.Print ("i:" & i)
'Debug.Print ("j:" & j)
'Debug.Print ("name:" & m(j, 0) & " value:" & m(j, 1))


'find the total employee




Dim k1, k2 As Integer
Dim employee_name(300) As String ' the total employee should less then 300
k1 = 0
k2 = 0
k3 = 0
flag = 0






Do Until k1 > j
 Do Until flag = 1 Or k2 > 300
    If employee_name(k2) = m(k1, 0) Then                                'have duplicate name
       flag = 1
    End If
    k2 = k2 + 1
 Loop
 If flag = 0 Then
    employee_name(k3) = m(k1, 0)
    k3 = k3 + 1
 End If
 flag = 0
 k1 = k1 + 1
 k2 = 0
Loop


k3 = k3 - 1                                                       'actual employee total  from 0
Dim actual_employee_total As Integer
actual_employee_total = k3


Debug.Print ("actual_employee_total: " & actual_employee_total)




'compute salary
Dim salary(300) As Double
k1 = 0
k2 = 0
total_salary = 0


Do Until k1 > actual_employee_total
  Debug.Print ("k1:" & k1)
  Do Until k2 > n
    If m(k2, 0) = employee_name(k1) Then
       total_salary = total_salary + m(k2, 1)
       Debug.Print ("name:" & m(k2, 0) & " pay:" & m(k2, 1))
    End If
    k2 = k2 + 1
  Loop
  salary(k1) = total_salary
  total_salary = 0
  k1 = k1 + 1
  k2 = 0
Loop


' print the result


 For temp = 0 To actual_employee_total Step 1
    Debug.Print ("temp:" & temp)
    Debug.Print (employee_name(temp) & ":" & salary(temp))
 Next temp
 
 ' fill the salary in the sheet
 Dim sheet2 As Worksheet
Set sheet2 = ThisWorkbook.Worksheets("个人金额")




For i = 0 To actual_employee_total Step 1
    For j = 4 To 304 Step 1
        If sheet2.Cells(j, 2).Value = employee_name(i) Then
           sheet2.Cells(j, 7).Value = salary(i)
           Debug.Print ("match j:" & j & "salary:" & salary(i))
        End If
    Next j
Next i
 




Debug.Print (sheet2.Cells(j, 7).Value)
End Sub
VBA是什么   直到90年代早期,使应用程序自动化还是充满挑战性的领域.对每个需要自动化的应用程序,人们不得不学习一种不同的自动化语言.例如:可以用EXCEL的宏语言来使EXCEL自动化,使用WORD BASIC使WORD自动化,等等.微软决定让它开发出来的应用程序共享一种通用的自动化语言--------Visual Basic For Application(VBA),可以认为VBA是非常流行的应用程序开发语言VASUAL BASIC 的子集.实际上VBA是"寄生于"VB应用程序的版本.VBA和VB的区别包括如下几个方面:   1. VB是设计用于创建标准的应用程序,而VBA是使已有的应用程序(EXCEL等)自动化   2. VB具有自己的开发环境,而VBA必须寄生于已有的应用程序.   3. 要运行VB开发的应用程序,用户不必安装VB,因为VB开发出的应用程序是可执行文件(*.EXE),而VBA开发的程序必须依赖于它的"父"应用程序,例如EXCEL.   尽管存在这些不同,VBA和VB在结构上仍然十分相似.事实上,如果你已经了解了VB,会发现学习VBA非常快.相应的,学完VBA会给学习VB打下坚实的基础.而且,当学会在EXCEL中用VBA创建解决方案后,即已具备在WORD ACCESS OUTLOOK FOXPRO PROWERPOINT 中用VBA创建解决方案的大部分知识.   * VBA一个关键特征是你所学的知识在微软的一些产品中可以相互转化.   * VBA可以称作EXCEL的"遥控器".   VBA究竟是什么?更确切地讲,它是一种自动化语言,它可以使常用的程序自动化,可以创建自定义的解决方案.    此外,如果你愿意,还可以将EXCEL用做开发平台实现应用程序. …… …… …… …… …… …… 欢迎使用 VBScript 语言参考 通过对信息进行分组,可以使您方便地研究 Visual Basic 脚本的各个主题。 可以在“字母顺序关键字列表”中找到 VBScript 语言的所有主题。如果只需要查看某个主题(例如对象),则有对该主题进行详细说明的章节可供查阅。 如何操作呢?单击左边任意一个标题,即可显示该标题所包含的项目列表。从该列表中选择要查看的主题。打开所选主题之后,就能够很容易地链接到其他相关章节。 好了,现在就开始行动,赶快进去看一看吧!学习几个语句,研究几种方法,或者熟悉几个函数。您会发现 VBScript 语言的功能是多么强大。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值