VBA for alternate result

dgavin

Active Member
Joined
Feb 16, 2005
Messages
302
Is there a code or formula i can use which enter 1 or 2 alternatley each time my macro runs e.g.

when marcro runs, if worksheet 2 E20= 1 then worksheet 2 E20 = 2

when macro runs again, if worksheet 2 E20= 2 then worksheet 2 E20 = 1

when macro runs again, if worksheet 2 E20= 1 then worksheet 2 E20 = 2

and so on.

Thanks
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Try this:-
Code:
With Sheets("Sheet2").Range("E20")
.Value = IIf(.Value = 1 Or .value = "", 2, 1)
End With
 
Upvote 0
Try the following (may need to tweak syntax):


first macro runs then...

Code:
iInitial_Value = Sheets("Worksheet2").Range("E20").Value
 
Select Case iInitial_Value
Case 1
Sheets("Worksheet2").Range("E20").Value = 2
Case 2
Sheets("Worksheet2").Range("E20").Value = 1
End Select
 
Upvote 0
I have changed it to this

With ws2.Range("E4")
.Value = IIf(.Value = 1 Or .value = "", 2, 1)
End With

but need a further change.....

if D34 = 1 and E4 = 1 then E4 = 2

but if D34 = 1 and E4 = 2 then E4 = 1

Thanks
 
Upvote 0
Is D34 guaranteed to =1 at all times? If not there are at least three possibilities that your if statement requires.

If you require more than three options, select case is probably the easier syntax to work with.

dM/
 
Upvote 0
Code:
With Sheets("Worksheet2")
 
If .Range("E4").Value = 1 and .Range("D4").Value = 1  then 
    .Range("E4").Value = 2
Elseif .Range("E4").Value = 2 and .Range("D4").Value = 1  then
 
Upvote 0
sorry pulled the trigger too quickly!
Code:
With Sheets("Worksheet2")

If .Range("E4").Value = 1 and .Range("D4").Value = 1 then 
       .Range("E4").Value = 2
Elseif .Range("E4").Value = 2 and .Range("D4").Value = 1 then 
       .Range("E4").Value = 1
Else
 
Upvote 0
sorry pulled the trigger too quickly again! I'm sed to indenting with a tab!
again, you may need to play with this, e.g. exit if may be one word, but VBE should help here.

Code:
With Sheets("Worksheet2")
 If .Range("E4").Value = 1 and .Range("D4").Value = 1 then 
  .Range("E4").Value = 2
 Elseif .Range("E4").Value = 2 and .Range("D4").Value = 1 then 
  .Range("E4").Value = 1
 Else 
  Exit if
 End if
End With
 
Upvote 0

Forum statistics

Threads
1,224,592
Messages
6,179,789
Members
452,942
Latest member
VijayNewtoExcel

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