If statement problem - #div/0!

jgold20

Board Regular
Joined
Feb 4, 2018
Messages
135
[TABLE="width: 598"]
<colgroup><col width="91" style="width: 68pt;"><col width="99" style="width: 74pt;"><col width="89" style="width: 67pt;"><col width="99" style="width: 74pt;"><col width="123" style="width: 92pt;"><col width="97" style="width: 73pt;"></colgroup><tbody>[TR]
[TD="class: xl68, width: 91, align: center"]Cubic feet[/TD]
[TD="class: xl73, width: 99, align: center"] Filing or Scheduling & CC Fees[/TD]
[TD="class: xl71, width: 89, align: center"]Minimum Move[/TD]
[TD="class: xl73, width: 99, align: center"]Company Code[/TD]
[TD="class: xl73, width: 123, align: center"]Enter Minimum Move Amount [/TD]
[TD="class: xl75, width: 97, align: center"]Minimum [FONT=&quot]Price [/FONT]per[FONT=&quot]CF [/FONT][/TD]
[/TR]
[TR]
[TD="class: xl69, bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=ACB9CA]#ACB9CA[/URL] , align: center"] [/TD]
[TD="class: xl70, bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=ACB9CA]#ACB9CA[/URL] , align: center"]YES[/TD]
[TD="class: xl72, width: 89, bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=ACB9CA]#ACB9CA[/URL] , align: center"]YES[/TD]
[TD="class: xl76, width: 99, align: center"]L[/TD]
[TD="class: xl74, width: 123, bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=ACB9CA]#ACB9CA[/URL] , align: center"]500[/TD]
[TD="class: xl77, align: center"]#DIV/0![/TD]
[/TR]
[TR]
[TD="class: xl66, align: center"][/TD]
[TD="class: xl67"][/TD]
[TD="class: xl66, align: center"][/TD]
[TD="class: xl76, width: 99, align: center"]N[/TD]
[TD="class: xl74, width: 123, bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=ACB9CA]#ACB9CA[/URL] , align: center"] [/TD]
[TD="class: xl77, align: center"]#DIV/0![/TD]
[/TR]
[TR]
[TD="class: xl65"][/TD]
[TD="class: xl64"][/TD]
[TD="class: xl66, align: center"][/TD]
[TD="class: xl76, width: 99, align: center"]NO[/TD]
[TD="class: xl74, width: 123, bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=ACB9CA]#ACB9CA[/URL] , align: center"] [/TD]
[TD="class: xl77, align: center"]#DIV/0![/TD]
[/TR]
[TR]
[TD="class: xl65"][/TD]
[TD="class: xl64"][/TD]
[TD="class: xl66, align: center"][/TD]
[TD="class: xl76, width: 99, align: center"] [/TD]
[TD="class: xl74, width: 123, bgcolor: [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=ACB9CA]#ACB9CA[/URL] , align: center"] [/TD]
[TD="class: xl77, align: center"]#DIV/0![/TD]
[/TR]
</tbody>[/TABLE]
In the above example, when cubic feet is 0, it returns a #div/0! error. The statement I am using is =IF(G3="YES",(I3/E3),0) for minimum move. If I use the drop down and change minimum move to NO, it returns a 0. I have tried everything I could think of and still receive the #div/0!. Is there any way to avoid the error. I understand why it is happening
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
I MANAGED TO GET THE CODE IN - IT IS NOT EXECUTING

d34983f4-9fa8-4edc-ac0c-8fa614ce895d
 
Upvote 0
Sequence of events
1) Right click
2) View Code
3) Copy and paste supplied code
a) Drop-down changes to worksheet
b) Drop-down changes to change
4) Close VBA editor
5) In worksheet
a)Drop-down in G3 is changed to yes
b) J3 - J6 calculate price correctly
c) E9 - E12 remain blank ( they are not = to J3 - J6)
 
Upvote 0
Is your drop-down a Data Validation Box, or are you use ActiveX or other Form controls?

Let's see if your Macro is being invoked/called properly. Temporarily add a few message boxes to the code, like shown below:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim cell As Range
    
[COLOR=#ff0000]    MsgBox "Macro is invoked", vbOKOnly, "Check1"[/COLOR]

'   Check to see if cell G3 updated
    If Target.Address = Range("G3").Address And Target = "Yes" Then
[COLOR=#ff0000]        MsgBox "Range G3 update to Yes", vbOKOnly, "Check2"[/COLOR]
        For Each cell In Range("J3:J6")
            cell.Offset(6, -5).Value = cell.Value
        Next cell
    End If
        
End Sub
Any manual change to your sheet should cause the first message to be displayed.
Changing G3 to "Yes" should cause the second message box to also be displayed.

If neither of those is happening, that macro is not being called to run.
 
Upvote 0
Change to NO
a) Check1 macro invoked - OK
Changed to YES
a) Check1 macro invoked - OK

SAME MESSAGE FOR BOTH
 
Upvote 0
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    Dim cell As Range
    
[COLOR=#ff0000]    MsgBox "Macro is invoked", vbOKOnly, "Check1"[/COLOR]

'   Check to see if cell G3 updated
    If Target.Address = Range("G3").Address And Target = "Yes" Then
[COLOR=#ff0000]        MsgBox "Range G3 update to Yes", vbOKOnly, "Check2"[/COLOR]
        For Each cell In Range("J3:J6")
            cell.Offset(6, -5).Value = cell.Value
        Next cell
    End If
        
End Sub
I realize you were just adding to the code the OP posted, but I have a couple of comments regarding the code...

1) As written, the code will error out if a multi-cell paste operation is performed on the sheet. The If..And..Then statement needs to be broken into two separate If..Then tests, first for the cell address, then for the "Yes" value.

2) The For..Next loop can be replaced by a single line of code.

3) I disabled events while the cells are being copied so that the copy process does not trigger the Change event again.

Here is the modified code which implements the above three comments...
Code:
Private Sub Worksheet_Change(ByVal Target As Range)

  Dim cell As Range
  
  MsgBox "Macro is invoked", vbOKOnly, "Check1"

'   Check to see if cell G3 updated
  [B][COLOR="#0000FF"]If Target.Address = Range("G3").Address Then[/COLOR][/B]
    [B][COLOR="#0000FF"]If Target.Value = "Yes" Then[/COLOR][/B]
      MsgBox "Range G3 update to Yes", vbOKOnly, "Check2"
      [B][COLOR="#006400"]Application.EnableEvents = False[/COLOR][/B]
      [B][COLOR="#FF0000"]Range("E9:E12").Value = Range("J3:J6").Value[/COLOR][/B]
      [B][COLOR="#006400"]Application.EnableEvents = True[/COLOR][/B]
    [B][COLOR="#0000FF"]End If[/COLOR][/B]
  [COLOR="#0000FF"][B]End If[/B][/COLOR]
        
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,185
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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