If Cell Value Greater Than -1 Exit Sub

pwill

Active Member
Joined
Nov 22, 2015
Messages
406
Hi can anyone help with vba code?

I have a Sub named 'My Data'

I have values in cells D3 and D4 Sheet2 and want to call a Sub named 'DataValues' then exit sub but only when D3 = D4 greater than -1

eg

(A) - if D3 value is 1 and D4 value is 3 then I want Sub 'My Data' to call Sub 'DataValues' then exit Sub.

but

(B) - if D3 value is 1 and D4 value is 2 then I just want to end the IF statement and continue with the Sub 'My Data'

(C)- if D3 value is 7 and D4 value is 5 then I just want to end the IF statement and continue with the Sub 'My Data'

The code I have works with the line highlighted in red when (A) is true but still calls Sub 'DataValues' when (B) and (C) is true.

Code:
Sub MyData()


    Dim FromSht As Worksheet: Set FromSht = Sheet1
    Dim ToSht As Worksheet: Set ToSht = Sheet2
    Dim lRowW As Long

    
    lRowW = FromSht.Cells(Rows.Count, "W").End(xlUp).Row
        
        With ToSht
                If Range("D4") = "" Then
                    GoTo Continue
                End If
            [COLOR=#ff0000]If Range("D3") = Range("D4") > -1 Then[/COLOR]
                [COLOR=#ff0000]Call DataValues[/COLOR]
                Exit Sub
            End If
        End With
    
Continue:
        If lRowW = 2 Then
            FromSht.Range("W2").ClearContents
                    Range("D4").ClearContents
                    Exit Sub
                Else:
                    If lRowW = 1 Then
                    Range("D3").ClearContents
                Exit Sub
            End If
        End If
End Sub


Any help would be apprieciated

Regards

pwill
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Maybe
Code:
If Range("D4").Value > Range("D3").Value + 1 Then
 
Upvote 0
Thanks Fluff,

Both solutions work when (A) is true in my example but still continues to call DataValues when (B) and (C) are true?

really struggling with this one think its got me beat?

pwill
 
Upvote 0
Which sheet are those ranges on? At the moment your code is looking at the active sheet.
 
Upvote 0
Hi, I'm wondering if I have the terminology correct in the title? Maybe it should be 'If Cell Value less Than -1 Exit Sub'
 
Upvote 0
In that case try
Code:
Sub MyData()
   
   
   Dim FromSht As Worksheet: Set FromSht = Sheet10
   Dim ToSht As Worksheet: Set ToSht = Sheet2
   Dim lRowW As Long
   
   
   lRowW = FromSht.Cells(Rows.Count, "A").End(xlUp).Row
   
   With ToSht
      If[COLOR=#ff0000] .[/COLOR]Range("D4") = "" Then
         GoTo Continue
      End If
      If [COLOR=#ff0000].[/COLOR]Range("D4").Value > [COLOR=#ff0000].[/COLOR]Range("D3").Value + 1 Then
         Call DataValues
         Exit Sub
      End If
   End With
   
Continue:
   If lRowW = 2 Then
      FromSht.Range("W2").ClearContents
      [COLOR=#ff0000]ToSht[/COLOR].Range("D4").ClearContents
      Exit Sub
   Else:
      If lRowW = 1 Then
         [COLOR=#ff0000]ToSht[/COLOR].Range("D3").ClearContents
         Exit Sub
      End If
   End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,227
Messages
6,170,853
Members
452,361
Latest member
d3ad3y3

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