How can I exit a If statement loop unless just the selected cells are changed?

cizwiz

New Member
Joined
Feb 26, 2020
Messages
2
Office Version
  1. 2010
Platform
  1. Windows
just wondering how i can exit a if statement loop
Code:
If (Range("A1") = "One") And (Range("A2") = "full") Then

  Rows("56:83").EntireRow.Hidden = True

  Range("A2") = "(Change Form Type)"

  Range("A3") = "One"

  Range("A1:J1").Select

Elseif (Range("A1") = "Two") And (Range("A2") = "Half") Then

  Rows("56:83").EntireRow.Hidden = False

  Range("A2") = "(Change Form Type)"

  Range("A3") = "Two"

  Range("A1:J1").Select

End If

I have this above however whenever i edit a cell that is not A1 or A2 the screen will flick and i assume it just runs the if statement again as the condition is still true is there a way i can stop this from happening unless A1 or A2 are changed

Tia Dan
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
How are you running that?
Could you please supply the full code?
 
Upvote 0
How are you running that?
Could you please supply the full code?
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Target, Range("A1:A2")) Is Nothing Then

    If (Range("A1") = "One") And (Range("A2") = "full") Then
        Rows("56:83").EntireRow.Hidden = True
        Range("A2") = "(Change Form Type)"
        Range("A3") = "One"
        Range("A1:J1").Select
    ElseIf (Range("A1") = "Two") And (Range("A2") = "Half") Then
        Rows("56:83").EntireRow.Hidden = False
        Range("A2") = "(Change Form Type)"
        Range("A3") = "Two"
        Range("A1:J1").Select
    End If

End If
End Sub

I've been making some edits to it but it doesn't work at all now doesn't error just doesn't do anything
 
Upvote 0
Dan

That code will be triggered when you make a change anywhere on the sheet.

However unless that change is made in either A1 or A2 then the code won't actually do anything, it'll skip immediately to End Sub after the If.
 
Upvote 0
I'm surprised that it doesn't crash Xl, as you have written a permanent loop.
How about
VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
   If Target.CountLarge > 1 Then Exit Sub
   If Not Intersect(Target, Range("A1:A2")) Is Nothing Then
      On Error GoTo Xit
      Application.EnableEvents = False
      If Range("A1") = "One" And Range("A2") = "full" Then
         Rows("56:83").EntireRow.Hidden = True
         Range("A2") = "(Change Form Type)"
         Range("A3") = "One"
         Range("A1:J1").Select
      ElseIf Range("A1") = "Two" And Range("A2") = "Half" Then
         Rows("56:83").EntireRow.Hidden = False
         Range("A2") = "(Change Form Type)"
         Range("A3") = "Two"
         Range("A1:J1").Select
      End If
      Application.EnableEvents = True
   End If
Exit Sub
Xit:
Application.EnableEvents = True
MsgBox "Oops something went wrong"
End Sub
 
Upvote 0
Fluff

Where's the permanent loop?:unsure:
 
Upvote 0
Apologies, not a permanent loop, but will trigger again, if either If statement is true as A2 is being changed.
 
Upvote 0

Forum statistics

Threads
1,214,958
Messages
6,122,475
Members
449,087
Latest member
RExcelSearch

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