另外,vba代码中存在溢出错误

时间:2018-01-16 15:52:54

标签: excel-vba vba excel

我是VBA的新手,请帮我解决以下问题。

在面对OVERFLOW错误时运行

total = 0
employee = InputBox("Enter the employee Name")

For Each sheet In Worksheets
  For i = 2 To 13

    If sheet.Cells(i, 2).Value = employee Then
       total = sheet.Cells(i, 3).Value + total
    End If
   Next i
Next sheet

1 个答案:

答案 0 :(得分:1)

您可能会溢出total变量,因为它可能默认为存储的INTEGER,最多只能容纳两个字节(-32768到32767)。

而是使用可容纳更多数据的类型声明您的变量:

Dim total as Long
total = 0
employee = InputBox("ENter the employee Name")

For Each sheet In Worksheets
        For i = 2 To 13

        If sheet.Cells(i, 2).Value = employee Then

               total = sheet.Cells(i, 3).Value + total

        End If

   Next i
Next sheet