**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:office:office" /><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

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
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
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
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
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
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
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,224,596
Messages
6,179,807
Members
452,944
Latest member
2558216095

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