# Some Trouble With If Then Else Statement

#### Do Vo Anh

##### New Member
I have some trouble when i used this code to run.. Can i someone give me some ideas to improve it? Thanks for your attention.
Sub Code()
Dim i As Integer
Dim a As Double
Dim Atan As Double
i = 43
If Cells(i, 1).Value = 0 Then
Cells(i, 6).Value = 0
Else
Do While Cells(i, 1).Value <> vbNullString
Cells(i, 6).Value = (Atn(-0.36 / (Cells(i, 1).Value * (-1.375))) / 2) * (180 / 3.141592654)
i = i + 1
Loop
End If
End Sub

### Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
What trouble are you getting?

The code. It's not show the value of formula next to Else function

Do you mean the zero is not being put in the E column?
When I run the code, all the values; the zero and the ATN formulas get written down the F column.

Even when the code had error " division by zero" you didnt see the value in a43 is 0.

I'm sorry. I still don't understand what your issue is?

When I recreated your sheet in my own sample file and I ran your code, cell F43 was a zero and the other values down the F column calculated fine based on your formula. So I'm not sure why you are refering to the #DIV/0! error as this is not the output of the macro?

If your first value is 0 you never increment the row variable 'i' so you only process the first row. I think your If... Then block should be inside the Do... While loop.

I think you hit the nail on the head there Rory.... (stupidme!!)

Taking Rory's keen observation, this code should work.

Code:
Sub Code()
Dim i As Integer
Dim a As Double
Dim Atan As Double
i = 43
Do While Cells(i, 1).Value <> vbNullString
If Cells(i, 1).Value = 0 Then
Cells(i, 6).Value = 0
Else
Cells(i, 6).Value = (Atn(-0.36 / (Cells(i, 1).Value * (-1.375))) / 2) * (180 / 3.141592654)
End If
i = i + 1
Loop
End Sub

Replies
8
Views
524
Replies
1
Views
184
Replies
3
Views
622
Replies
2
Views
303
Replies
3
Views
1K

1,196,152
Messages
6,013,752
Members
441,781
Latest member
Gian Carlos

### We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.

### Which adblocker are you using?

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

### Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

### Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back