excel VBA 编程

绝地灬酷狼 2023-06-02 07:50 114阅读 0赞
  1. Dim cuttent_columns As Integer
  2. Dim care_repeat As Integer
  3. Private Sub Workbook_Open()
  4. Dim i As Integer
  5. Dim j As Integer
  6. '确定cuttent_columns的值
  7. For i = 1 To 100
  8. If Cells(1, i) = "" Then
  9. For j = i To 100
  10. If Cells(1, j) = "" Then
  11. Else
  12. cuttent_columns = j - 1
  13. 'MsgBox "init cuttent_columns"
  14. 'MsgBox cuttent_columns
  15. Exit Sub
  16. End If
  17. Next
  18. End If
  19. Next
  20. End Sub
  21. Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
  22. On Error Resume Next '跳过异常
  23. Application.ScreenUpdating = False '关闭屏幕更新
  24. If care_repeat = 1 Then
  25. care_repeat = 0
  26. 'MsgBox "防止正反馈"
  27. Exit Sub
  28. End If
  29. If Target.Row > 51 Then Exit Sub
  30. 'MsgBox "cuttent_columns"
  31. 'MsgBox cuttent_columns
  32. 'MsgBox "Target.Row"
  33. 'MsgBox Target.Row
  34. 'MsgBox "Target.Target.Column"
  35. 'MsgBox Target.Column
  36. Dim null_flag As Integer
  37. Dim i As Integer
  38. null_flag = 0
  39. If Cells(Target.Row, 2) = 0 Or Cells(Target.Row, 2) = "" Then
  40. Else
  41. For i = 2 To cuttent_columns
  42. If Cells(Target.Row, i) = "" Then
  43. care_repeat = 1
  44. Cells(Target.Row, i) = Cells(Target.Row, 2)
  45. care_repeat = 1
  46. Cells(Target.Row, 2) = 0
  47. null_flag = 1
  48. Exit For
  49. End If
  50. Next
  51. If null_flag = 0 Then
  52. cuttent_columns = cuttent_columns + 1
  53. care_repeat = 1
  54. ActiveSheet.Columns(cuttent_columns).Insert
  55. care_repeat = 1
  56. Cells(Target.Row, cuttent_columns) = Cells(Target.Row, 2)
  57. care_repeat = 1
  58. Cells(Target.Row, 2) = 0
  59. End If
  60. End If
  61. '计算总和
  62. Dim littel_total As Integer
  63. Dim littel_total_num As Integer
  64. Dim big_total_num As Integer
  65. littel_total = cuttent_columns + 1
  66. littel_total_num = 0
  67. big_total_num = 0
  68. '计算小总和
  69. For i = 3 To cuttent_columns
  70. littel_total_num = littel_total_num + Cells(Target.Row, i)
  71. Next
  72. care_repeat = 1
  73. Cells(Target.Row, littel_total) = littel_total_num
  74. '判断
  75. If Cells(Target.Row, littel_total) > Cells(53, 2) Then
  76. care_repeat = 1
  77. Cells(Target.Row, littel_total + 1) = "金额超过阈值"
  78. Else
  79. care_repeat = 1
  80. Cells(Target.Row, littel_total + 1) = ""
  81. End If
  82. '计算大总和
  83. For i = 2 To 51
  84. big_total_num = big_total_num + Cells(i, littel_total)
  85. Next
  86. care_repeat = 1
  87. Cells(52, littel_total) = big_total_num
  88. Application.ScreenUpdating = True '恢复屏幕更新
  89. End Sub

  

转载于:https://www.cnblogs.com/mingyunrangwozoudaoxianzai/p/11604175.html

发表评论

表情:
评论列表 (有 0 条评论,114人围观)

还没有评论,来说两句吧...

相关阅读

    相关 C#调用Excel VBA

    近日的一系列工作是做网站的营运维护,因此做了大量的支持工具。有Excel中写VBA的,也有直接C\做的工具。有时需要在C\中执行Excel VBA宏,甚至有时还需要在

    相关 Excel VBA 语句集

    定制模块行为 (1) Option Explicit ‘强制对模块内所有变量进行声明      Option Private Module ‘标记模块为私有,仅对同一工