-1

I write "WWW 12345" (or "WWW 12345" or "WWW12345" or "www 12345") in a cell. When I press enter (or arrows), I want to see in a cell only digits (e.g 12345).

In vba, I tried with Workbook_SelectionChange, but doesn't works. I tried to create a custom format cell, but it is available only for digits.

How can I make this? I'm not programmer, so I try to find a simple solution (that I can understand it).

Community
  • 1
  • 1
colombo2003
  • 121
  • 9

1 Answers1

1

Use this in conjunction with the onlyDigits function or regex function from link.

Private Sub Worksheet_Change(ByVal Target As Range)
    Target.Value = onlyDigits(Target.Value)
End Sub

You could add a check to make sure this is only called when data in a certain column is change vs the entire sheet.

Michael Russo
  • 442
  • 6
  • 14
  • Update: when I want to delete a entered value, MS Excel has stopped working and close it. "A problem caused the program to stop working correctly. Windows will close the program and notify you if a solution is available". I tried to put this line "If Len(s) > 0 Then" (and End if to end) before For line in the onltDigits function from link, but doesn't work. How can I fix that? Please help! Thx. – colombo2003 Aug 11 '16 at 11:40
  • Try putting the condition in the `Worksheet_Change` using Target.Value. – Michael Russo Aug 11 '16 at 12:12
  • SOLVED! But new I have another little problem! I want to validate the entered values to prevent duplicated. "www123" is converted in "123" (which is ok), but when I entered "123" nothing happened (I would like to see the message set in Data Validate) – colombo2003 Aug 11 '16 at 12:46
  • Well after your null check, you could grab the converted value, loop through the cells to see if it already exists, if not let it set `Target.Value`. Otherwise you can use a message box to show the user that the value exists already. – Michael Russo Aug 11 '16 at 12:50