Division by zero vba ошибка

Replace all of your code: —

private sub textbox1_change()
if textbox1.value = "" then exit sub 
if textbox2.value = "" then exit sub 
textbox3.value = cdbl(textbox1.value) / cdbl(textbox2.value)
end sub 
private sub textbox2_change()
if textbox1.value = "" then exit sub
if textbox2.value = "" then exit sub
textbox3.value = cdbl(textbox1.value) / cdbl(textbox2.value)
end sub 

With this code: —

Private Sub textbox1_change()
ShowResult
End Sub
Private Sub textbox2_change()
ShowResult
End Sub
Private Sub ShowResult()
Dim Str1        As String
Dim Str2        As String
Str1 = Trim(textbox1.Value)
Str2 = Trim(textbox2.Value)
If (Str1 = "") Or (Str2 = "") Then Exit Sub
If (IsDouble(Str1) = False) Or (IsDouble(Str2) = False) Then
    textbox3.Value = "NA"
Else
    If (CDbl(Str2) = 0) Or ((CDbl(Str2) + CDbl(Str1)) = 0) Then
        textbox3.Value = "NA"
    Else
        textbox3.Value = CDbl(Str1) / CDbl(Str2)
    End If
End If
End Sub
Private Function IsDouble(ByVal StrValue As String) As Boolean
Dim DblTest As Double
On Error GoTo ErrorHandle
DblTest = CDbl(StrValue)
IsDouble = True
Exit Function
ErrorHandle:
Err.Clear
End Function

This will check for values that can’t be Double data types (i.e. a string) and bad division (i.e. error code 6 and 11).

EDIT: —
The below is walkthrough of what is happening in the above code.

The procedures textbox1_change and textbox2_change are doing the same thing so to avoid repetition of code; they both call on the one instance of that code.

Private Sub textbox1_change()
ShowResult
End Sub

Private Sub textbox2_change()
ShowResult
End Sub

After that, there is the new procedure ShowResult that holds the single instance of the code that textbox1_change and textbox2_change call upon.

Private Sub ShowResult()
Dim Str1        As String
Dim Str2        As String
Str1 = Trim(textbox1.Value)
Str2 = Trim(textbox2.Value)
If (Str1 = "") Or (Str2 = "") Then Exit Sub
If (IsDouble(Str1) = False) Or (IsDouble(Str2) = False) Then
    textbox3.Value = "NA"
Else
    If (CDbl(Str2) = 0) Or ((CDbl(Str2) + CDbl(Str1)) = 0) Then
        textbox3.Value = "NA"
    Else
        textbox3.Value = CDbl(Str1) / CDbl(Str2)
    End If
End If
End Sub

The ShowResult code does a number of checks.

First it places textbox1 into Str1 and textbox2 into Str2 and using trim on them. Trim means that leading and trailing spaces are removed. For example if the textbox1 value was » » (maybe done by copy and paste by the user) then technically it’s not empty and could cause an error.

Dim Str1        As String
Dim Str2        As String
Str1 = Trim(textbox1.Value)
Str2 = Trim(textbox2.Value)

The next check is if either value is empty then exit the procedure, much like you did before but now on a single line.

If (Str1 = "") Or (Str2 = "") Then Exit Sub

The next check calls another procedure that does a check to ensure the value could be converted to a double. for Example CDbl("Hello World!") would fail because it’s not a number to begin with. So this check gets around that potential issue, if its not a number that can be divided then output ‘NA’.

If (IsDouble(Str1) = False) Or (IsDouble(Str2) = False) Then
    textbox3.Value = "NA"

The final check is that if the second value is zero or both are zero then «NA2 is output, else the division is done and output.

If (CDbl(Str2) = 0) Or ((CDbl(Str2) + CDbl(Str1)) = 0) Then
    textbox3.Value = "NA"
Else
    textbox3.Value = CDbl(Str1) / CDbl(Str2)
End If

This is the final procedure that was called by ShowResult to check the value could be converted to a Double data type. It tried to do the conversion, if an error occurs the error is cleared and false (by default) will be returned to the caller, if there is no error then true is output.

Private Function IsDouble(ByVal StrValue As String) As Boolean
Dim DblTest As Double
On Error GoTo ErrorHandle
DblTest = CDbl(StrValue)
IsDouble = True
Exit Function
ErrorHandle:
Err.Clear
End Function

Hope this helps.

title keywords f1_keywords ms.prod ms.assetid ms.date ms.localizationpriority

Division by zero (Error 11)

vblr6.chm1011128

vblr6.chm1011128

office

3c6783d9-24a4-ef25-fdab-9e26a08e35a9

06/08/2017

medium

Division by zero (Error 11)

Division by zero isn’t possible. This error has the following cause and solution:

  • The value of an expression being used as a divisor is zero.

    Check the spelling of variables in the expression. A misspelled variable name can implicitly create a numeric variable that is initialized to zero. Check previous operations on variables in the expression, especially those passed into the procedure as arguments from other procedures.

For additional information, select the item in question and press F1 (in Windows) or HELP (on the Macintosh).

[!includeSupport and feedback]

Hello all,

When I call a function from a DLL, written in C++ and compiled in Visual Studio 2015 «Release» mode, I get the runtime error message “Run-time error ’11’: Division by zero” at the function call. However, when I compile as «Debug»,
everything runs fine.

I found out that the «Release» compilation does some name mangling with the called function, while «Debug» doesn’t do that. I found the real exported name through applying «dumpbin» on the DLL. Therefore one needs to use this alias
name in VBA’s Declare-statement in the “Release” case (see below), while for the «Debug» compilation this is not needed. Furthermore, all this is done in a 32-bit environment (Windows 7, Office 2010), if I do that in a 64-bit environment (Windows
10, Office 365), there is no problem.

Does anybody have an idea what is going on here?

Here are the details of the problem:

The DLL “Simulation.dll” is written in C++ and compiled in Visual Studio 2015. The C++ function definition is in the file “source.cpp” which doesn’t have any header file, and reads

double __declspec(dllexport) simulationCPP18(<list of 50 parameters, all either double or double*>)

Now I compile in Visual Studio 2015 “Debug” mode to produce the DLL “Simulation.dll”. I declare the DLL function in VBA through

Declare Function simulationCPP18 _
Lib "Simulation.dll" _
(<list of 50 parameters, all double, some ByVal and some ByRef>) As Double

Running the VBA macro like this, everything works fine and the correct result is reproduced.

However, when I compile in “Release” mode, this first gives the error “Can’t find DLL entry point”. I could resolve this error, by finding out the real exported function name through applying “dumpbin” on “Simulation.dll” . This showed that the function
name was mangled to “?simulationCPP18@@YGNPAN00000000N00000000000NNN00N0N00NNNNNNN@Z”. Now I used this name as function Alias in the declare statement, written as

Declare Function simulationCPP18 _
Lib "Simulation.dll" Alias “?simulationCPP18@@YGNPAN00000000N00000000000NNN00N0N00NNNNNNN@Z" _
(<list of 50 parameters, all double, some ByVal and some ByRef>) As Double

The VBA macro seems to find the entry point now, but throws the error message : “Run-time error ’11’: Division by zero” at the  point of the function call within the VBA macro, where it reads

i = simulationCPP18(<list of 50 parameters>)

Note that there is no explicit division happening at any point close to this function call, nor within the parameters.

Do you know where this different behaviour between “Debug” and “Release” compilation is coming from? Obviously, the mangled function name itself is correctly used, otherwise the Error “Can’t find DLL entry point” would prevail, right?

Thanks for any help on that.

  • Moved by

    Tuesday, June 14, 2016 5:31 PM
    c++ problem/ maybe VBA

При выполнении макросов Excel могут возникнуть ошибки, которые в VBA делят на три категории:

  • Ошибки компиляции
  • Ошибки выполнения
  • Логические ошибки (баги)

Далее мы поговорим о каждом из трёх типов ошибок VBA подробно.

Содержание

  1. Ошибки компиляции
  2. Ошибки выполнения
  3. Перехват ошибок выполнения
  4. Логические ошибки

Ошибки компиляции

Компилятор VBA рассматривает ошибки компиляции как недопустимые и выделяет их в коде ещё до того, как дело дойдёт до запуска макроса.

Если при написании кода допущена синтаксическая ошибка, то редактор VBA сигнализирует об этом немедленно: либо при помощи окна с сообщением, либо выделяя ошибку красным цветом, в зависимости от статуса режима Auto Syntax Check.

Примечание: При включённом режиме Auto Syntax Check каждый раз, при появлении в редакторе Visual Basic во введённом коде синтаксической ошибки, будет показано соответствующее сообщение. Если же этот режим выключен, то редактор VBA продолжит сообщать о синтаксических ошибках, просто выделяя их красным цветом. Опцию Auto Syntax Check можно включить/выключить в меню Tools > Options редактора Visual Basic.

В некоторых случаях ошибка компиляции может быть обнаружена при выполнении компиляции кода, непосредственно перед тем, как макрос будет выполнен. Обычно ошибку компиляции несложно обнаружить и исправить, потому что компилятор VBA даёт информацию о характере и причине ошибки.

Ошибки в Excel VBA

Например, сообщение «Compile error: Variable not defined» при попытке запустить выполнение кода VBA говорит о том, что происходит попытка использовать или обратиться к переменной, которая не была объявлена для текущей области (такая ошибка может возникнуть только если используется Option Explicit).

Ошибки выполнения

Ошибки выполнения возникают в процессе выполнения кода и приводят к остановке выполнения программы. Этот тип ошибок VBA, как правило, также не сложно обнаружить и исправить, так как сообщается информация о характере ошибки и место в коде, где произошла остановка.

Примером такой ошибки может служить попытка выполнить деление на ноль. В результате будет показано сообщение «Run-time error ’11’: Division by zero«.

Ошибки в Excel VBA

В зависимости от структуры проекта VBA, может быть предложено выполнить отладку кода (как показано на рисунке ниже). В этом случае при нажатии на кнопку Debug (в окне сообщения о необходимости отладки) будет выделена цветом строка кода, которая стала причиной ошибки VBA.

Ошибки в Excel VBA

Получив такое сообщение и видя выделенную строку кода, как в приведённом выше примере, обнаружить причину ошибки будет совсем не сложно.

В случае если код сложнее, чем в нашем примере, то, чтобы получить больше информации о причине возникновения ошибки VBA, можно проверить значения используемых переменных. В редакторе VBA для этого достаточно навести указатель мыши на имя переменной, или можно открыть окно отслеживания локальных переменных (в меню редактора View > Locals Window).

Коды различных ошибок выполнения расшифрованы на сайте Microsoft Support (на английском). Наиболее часто встречающиеся ошибки VBA перечислены в этой таблице:

5 Недопустимый вызов процедуры (Invalid procedure call)
7 Недостаточно памяти (Out of memory)
9 Индекс вне заданного диапазона (Subscript out of range)

Эта ошибка возникает при попытке обратиться к элементу массива за пределами заданного размера массива – например, если объявлен массив с индексами от 1 до 10, а мы пытаемся обратиться к элементу этого же массива с индексом 11.

11 Деление на ноль (Division by zero)
13 Несоответствие типа (Type mismatch)

Эта ошибка возникает при попытке присвоить переменной значение не соответствующего типа – например, объявлена переменная i типа Integer, и происходит попытка присвоить ей значение строкового типа.

53 Файл не найден (File not found)

Иногда возникает при попытке открыть не существующий файл.

Перехват ошибок выполнения

Не все ошибки выполнения бывают вызваны недочётами в коде. Например, ошибки VBA не удастся избежать, если для работы макроса необходимо открыть файл с данными, а этого файла не существует. В таких случаях признаком профессионализма будет перехват ошибок и написание кода VBA, который будет выполняться при их возникновении. Таким образом, вместо неприятных сбоев будет происходить изящное завершение работы макроса.

Для того, чтобы помочь справиться с возникающими ошибками, VBA предоставляет разработчику операторы On Error и Resume. Эти операторы отслеживают ошибки и направляют выполнение макроса в специальный раздел кода VBA, в котором происходит обработка ошибки. После выполнения кода обработки ошибки, работа программы может быть продолжена с того места, где возникла ошибка, или макрос может быть остановлен полностью. Далее это показано на примере.

'Процедура Sub присваивает переменным Val1 и Val2 значения,
'хранящиеся в ячейках A1 и B1 рабочей книги Data.xlsx расположенной в каталоге C:Documents and Settings

Sub Set_Values(Val1 As Double, Val2 As Double)

   Dim DataWorkbook As Workbook

   On Error GoTo ErrorHandling

      'Открываем рабочую книгу с данными

      Set DataWorkbook = Workbooks.Open("C:Documents and SettingsData")

      'Присваиваем переменным Val1 и Val2 данные из рабочей книги DataWorkbook

      Val1 = Sheets("Лист1").Cells(1, 1)
      Val2 = Sheets("Лист1").Cells(1, 2)

      DataWorkbook.Close

   Exit Sub

ErrorHandling:

   'Если файл не найден, предлагаем пользователю разместить его в
   'нужном месте и продолжить работу

   MsgBox "Рабочая книга не найдена! " & _
      "Пожалуйста добавьте книгу Data.xlsx в каталог C:Documents and Settings и нажмите OK."

   Resume

End Sub

В этом коде производится попытка открыть файл Excel с именем Data. Если файл не найден, то пользователю будет предложено поместить этот файл в нужную папку. После того, как пользователь сделает это и нажмёт ОК, выполнение кода продолжится, и попытка открыть этот файл повторится. При желании вместо попытки открыть нужный файл, выполнение процедуры Sub может быть прервано в этом месте при помощи команды Exit Sub.

Логические ошибки

Логические ошибки (или баги) возникают в процессе выполнения кода VBA, но позволяют ему выполняться до самого завершения. Правда в результате могут выполняться не те действия, которые ожидалось, и может быть получен неверный результат. Такие ошибки обнаружить и исправить труднее всего, так как компилятор VBA их не распознаёт и не может указать на них так, как это происходит с ошибками компиляции и выполнения.

Например, при создании макроса в процедуре случайно были просуммированы не те переменные, которые требовалось просуммировать. Результат будет ошибочным, но макрос будет продолжать выполняться до завершения.

Редактор Excel VBA предоставляет набор инструментов отладки, которые помогут найти и исправить логические ошибки в коде VBA. В данной статье мы не будем рассматривать подробно эти инструменты. Любознательный пользователь может найти обзор инструментов отладки VBA на сайте Microsoft Help & Support (на английском).

Оцените качество статьи. Нам важно ваше мнение:

While using VBA, you may have encountered many different errors (or bugs). These VBA errors fall into 3 categories: Compile error, Runtime error and Logical error. These VBA errors are discussed in turn below.

Compile Errors

First we will discuss the compilation error. Compile errors are one of the types of VBA errors that occur during the compilation of your code. While compiling your code, the Visual Basic Editor goes through each line of your code and since compile errors are recognized as being illegal, if the compiler runs into any compile error, it immediately highlights that line of code (or codes) as errors by the VBA compiler before your macro starts to execute.

Of course, you have to fix the compile errors before you can execute your macro. If a compilation error is an incorrectly formatted line of VBA code, the VBA editor will immediately detect and highlight this (with a yellow line) as soon as you attempt to move your cursor away from the specific line of code (or codes). Alternatively, a compile error may be detected at the time you attempt to run your macro (but before execution has started).

Here is an example of a compilation error.

compile error

As you can see from the above picture, the VBA compiler tells you the nature of this type of error. In fact, the VBA compiler also tells you how to solve a compile error by telling you what exactly you did wrong. In the above picture, you get the compile error «Block if and without End if».

This error only comes when you didn’t terminate the if condition by simply writing End If before the End Sub statement you can fix this error. Compile errors are generally easier to fix than logical and Runtime error.

Another example of a compilation error:

compile error end statement

Runtime Errors

This is a type of VBA error that occurs during the execution of your code or macro. This type of error causes the program to crash (stop abruptly). Again, just like a compilation error, runtime errors are also generally easier to solve.

The VBA compiler tells you the nature of this type of error as well as the location where your code has stopped working (this is highlighted by a yellow line).

runtime error

This is an example of a runtime error. In this example, you have written a code that tries to divide by the number zero, so you get a message box stating «Run-time error ’11: Division by zero.

Depending on the structure of your VBA project, you may be given the option to debug the code, (see below). In this case, if you click on the Debug button on the debug message box, it causes the line of code that generated the VBA error to be highlighted (in yellow) in your VBA editor.

division by zero

Thanks to the message box content and the highlighting of code, it is very easy to spot this error in the sub routine.

However, If your code is much more complex, then you can get more information on the reason for the VBA error by looking at the values of the variables in use. This can be done in the VBA editor by simply placing your mouse cursor over the variable name, or by opening the local variables window (by selecting View>Locals Window).

Logical Errors

The last type of VBA error is a logical error. Logical errors, just like runtime errors, occur during the execution of your code, but instead of crashing, they either give «abnormal» or incorrect result, or cause the macro to perform unexpected actions.

These errors are the most difficult to find and fix of the 3 errors, like it does with Compile and Runtime errors. The VBA compiler cannot find and point to these errors. These errors are caused by the incorrect «logic» that the user has written in the code (incorrect line of code).

For example, suppose you have 3 variables: A, B and C. A has value 3(A=3), b has value 4(B=4) and c has value 2(C=2). Now, you may code your macro to add variable A and C while you actually needed to add variable A and B. The result will be incorrect, but the macro will still run and not crash.

  • Django rest framework обработка ошибок
  • Divinity original sin ошибка msvcp120 dll
  • Django is not importable in this environment ошибка
  • Divinity original sin ошибка msvcp110
  • Django admin ошибка проверки csrf запрос отклонен