Re-enter space in a cell if user accidently deletes it

J

James

I have a column than I input names in. Each cell of that column already has
a space entered in it, so that autocomplete will function. However, if a
user deletes an entry in the column, he deletes the space also. I tried the
following sub in the worksheet change event to solve this problem:

If Not Intersect(Target, wshDatabase.Range("Names")) Is Nothing Then If
Target.Value = "" Then Target.Value = " "

It works fine unless the user selects more than one cell when he deletes
data, in which case I get a "type mismatch" error. How do I correct my sub
so it works with a multi cell selection?
 
M

mp

James said:
I have a column than I input names in. Each cell of that column already has
a space entered in it, so that autocomplete will function. However, if a
user deletes an entry in the column, he deletes the space also. I tried the
following sub in the worksheet change event to solve this problem:

If Not Intersect(Target, wshDatabase.Range("Names")) Is Nothing Then If
Target.Value = "" Then Target.Value = " "

It works fine unless the user selects more than one cell when he deletes
data, in which case I get a "type mismatch" error. How do I correct my sub
so it works with a multi cell selection?

is Target a range?
then wouldn't it just be
For Each c in TargetRange If Len( c.Value)=0 Then
c.Value = " "
End If
Next c
 
D

Don Guillett Excel MVP

I have a column than I input names in. Each cell of that column already has
a space entered in it, so that autocomplete will function. However, if a
user deletes an entry in the column, he deletes the space also. I tried the
following sub in the worksheet change event to solve this problem:

If Not Intersect(Target, wshDatabase.Range("Names")) Is Nothing Then If
Target.Value = "" Then Target.Value = " "

It works fine unless the user selects more than one cell when he deletes
data, in which case I get a "type mismatch" error. How do I correct my sub
so it works with a multi cell selection?

First line of event
if target.count>1 then exit sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top