If statement problem - #div/0!

jgold20

Board Regular
Joined
Feb 4, 2018
Messages
135
Cubic feet Filing or Scheduling & CC FeesMinimum MoveCompany CodeEnter Minimum Move Amount Minimum [FONT=&quot]Price [/FONT]per[FONT=&quot]CF [/FONT]
YESYESL500#DIV/0!
N #DIV/0!
NO #DIV/0!
#DIV/0!

<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>
</tbody>
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

Highlight Duplicates
Home, Conditional Formatting, Highlight Cells, Duplicate records, OK to add pink formatting to any duplicates in selected range.
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,215,491
Messages
6,125,107
Members
449,205
Latest member
ralemanygarcia

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