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
 
As your code works quite happily for me, I don't know what to suggest.
No probs, it's a minor thing.

However, would welcome your thoughts on this thread:

 
Upvote 0

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().

Forum statistics

Threads
1,215,008
Messages
6,122,672
Members
449,091
Latest member
peppernaut

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