**Macro with IF conditions**

lhk201

New Member
Joined
Feb 27, 2011
Messages
35
Is it possible to create a macro that follows the following conditions?<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
<o:p> </o:p>
If any cell within the range a6:h200 is selected then that cell will have the word “no” entered into it. This should happen only when the cell is blank.<o:p></o:p>
<o:p> </o:p>
If the cell is not blank and it is selected within the same range then the cell should now be cleared.<o:p></o:p>

Thanks in advance for any help at all regarding this matter.
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Try this: right click the sheet tab, select View Code and paste in

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Intersect(Target, Range("A6:H200")) Is Nothing Then Exit Sub
If Target.Value = "" Then
    Target.Value = "no"
Else
    Target.ClearContents
End If
End Sub
 
Upvote 0

MrKowz

Well-known Member
Joined
Jun 30, 2008
Messages
6,653
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
Try:

Code:
Public Sub lhk201()
If Not Intersect(ActiveCell, Range("A6:H200")) Is Nothing Then
    If ActiveCell.Value = "" Then
        ActiveCell.Value = "No"
    Else
        ActiveCell.ClearContents
    End If
End If
End Sub
 
Upvote 0

lhk201

New Member
Joined
Feb 27, 2011
Messages
35
Hello!

Thanks for the very prompt response!

Neither of these currently seem to work. I am entering it into Module 1 is that the correct place?

Thanks :)
 
Upvote 0

lhk201

New Member
Joined
Feb 27, 2011
Messages
35
Ah it works when I actually run the macro, is there a way to make it automatic?
 
Upvote 0

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Ah it works when I actually run the macro, is there a way to make it automatic?


My code will run automatically if you put it in the worksheet's code module.
 
Upvote 0

lhk201

New Member
Joined
Feb 27, 2011
Messages
35
Ah thanks VOG it works! I got a bit excited and jumped the gun there.

Just one more question, when I highlight a group of cells within that range the macro there is a runtime error 13 type mismatch.

Is there a way of disabling selecting a group of cells?
 
Upvote 0

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
Try this

Rich (BB code):
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Target.Count > 1 Then Exit Sub
If Intersect(Target, Range("A6:H200")) Is Nothing Then Exit Sub
If Target.Value = "" Then
    Target.Value = "no"
Else
    Target.ClearContents
End If
End Sub
 
Upvote 0

lhk201

New Member
Joined
Feb 27, 2011
Messages
35
Perfect, thanks very much.

One more thing! If I wanted the range to be a6:h200, a1:b3 and j4:j9 for example?

Thank you!!!
 
Upvote 0

Forum statistics

Threads
1,191,215
Messages
5,985,314
Members
439,956
Latest member
venky2002

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
Top