Some Trouble With If Then Else Statement

Do Vo Anh

New Member
Joined
May 28, 2015
Messages
6
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

SjsgQw.png
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
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.
 
Upvote 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?
 
Upvote 0
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.
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,385
Messages
6,119,209
Members
448,874
Latest member
b1step2far

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.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

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

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

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
Back
Top