Validating data

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,834
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
I am trying to validate some data on my worksheet. If a user enters a value that contains a space, I want that action reversed.

Here is the code:

Code:
Option Explicit

Private Sub Worksheet_Change(ByVal Target As Range)

    If InStr(1, Target.Value, " ", 0) <> 0 Then
          
        With Application
       
            .EnableEvents = False
           
            .Undo
           
            .EnableEvents = True
       
        End With
       
    End If
                   
End Sub

It works well if you typed something like "some nonsense" or even "some " (obviously without the "").

However, if you typed "some@ nonsense", it first converts it to a hyperlink with it underlined, ie "some@ nonsense", then the undo action kicks in so reverses the string to "some@ nonsense"

What I want is the cell to be blank.

How can I amend my code to do that?


EDIT: Seems I need to set this option:

Code:
[LIST]
[*]File-> Options-> Proofing-> AutoCorrect Options…-> AutoFormat As You Type-> uncheck: Internet and network paths with hyperlinks
[/LIST]

but can that be set using code?



Thanks
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
If you want a blank cell just replace the .Undo with Target.Value=""
 
Upvote 0
If you want a blank cell just replace the .Undo with Target.Value=""Nice thought, thanks!
Nice thought, thanks!

I always immediately think of .Undo.

With some of my other programs, that doesn't even work because the stack is cleared. For those situations, I make use of the Selection_Change event, recording what the value was before the change, then reverse it.
 
Upvote 0
For what it's worth I don't get the same behaviour you are complaining about, your code clears the cell (if it was blank before)
 
Upvote 0
For what it's worth I don't get the same behaviour you are complaining about, your code clears the cell (if it was blank before)
Oh no, your code works well for this situation.

I was referring to other situations in other programs I have written, eg this thread:

 
Upvote 0
I tend to steer clear of using Undo & rely on the users doing things correctly. It's far easier.
 
Upvote 0
Not sure that makes any difference, it's a matter of educating users & trusting them. Code should be a tool to help them do their job.
 
Upvote 0
Not sure that makes any difference, it's a matter of educating users & trusting them. Code should be a tool to help them do their job.
Joking aside, one reason I want to use .undo for this particular problem is if the user types a value over an existing value.

Blanking it would erase the existing record whereas undo reverts it.
 
Upvote 0
As your code works quite happily for me, I don't know what to suggest.
 
Upvote 0

Forum statistics

Threads
1,215,514
Messages
6,125,265
Members
449,219
Latest member
daynle

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