Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8508273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:07:00+00:00 2026-06-11T03:07:00+00:00

I have one excel sheet where user will enter meta data in each cell.

  • 0

I have one excel sheet where user will enter meta data in each cell. What I need to do is write a macro which will allow only alpha numeric characters and comma.

I wrote a macro for test purpose which wouldn’t allow special characters. But after I entered one valid character in cell it’s allowing my disallowed characters. I don’t know how to rectify it.

My code:

Private Sub WorkBook_Open()
       MsgBox "Running the Disable_Keys() Macro"
       Call ThisWorkbook.Disable_Keys
End Sub

Sub MyMsg()
    MsgBox "Press Another Key"
End Sub

Sub Disable_Keys()

     Dim I As Long
     Dim KeysArray As Variant
     Dim Key As Variant

     KeysArray = Array("@", "!", "~", "#", "$", "&", "|", "\", ":", "*", "_", "-", "=", "'", ";", "<", ">", "?", "/", "'", ":")

     For Each Key In KeysArray
          Application.OnKey Key, "ThisWorkbook.MyMSg"
     Next Key
  End Sub 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T03:07:01+00:00Added an answer on June 11, 2026 at 3:07 am

    This will check for if there is something that is not 0-9,a-z or a comma
    If you want to exclude specific characters, then the .Pattern would be what you change
    If it finds something, then the cell is invalid, and it will clear the cell.

    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim TestCell
    Dim RE As Object
    Dim REMatches As Object
    
    Set RE = CreateObject("vbscript.regexp")
    With RE
        .MultiLine = False
        .Global = False
        .IgnoreCase = True
        .Pattern = "[^0-9A-Z,]"
    End With
    
    For Each TestCell In Target.Cells
        Set REMatches = RE.Execute(TestCell.Value)
        If REMatches.Count > 0 Then
            MsgBox "Invalid:" & TestCell.Address & "-" & TestCell.Value
            TestCell.Value = ""
        End If
    Next
    
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am importing Excel sheet into DataGridView.I have one of the cell has date
I'm having trouble with some Excel data validation. On one sheet, I have a
I have 12 excel files, each one with lots of data organized in 2
I have an Excel workbook with a number of features: One main user-facing sheet
Consider an Excel sheet with one column of bad data. We have a table
I have an Excel sheet with two column, one is a number , and
I have an excel sheet that has several headers. One of the header is
Excel 2003 Question: I'd like to have one cell represent a range of cells.
I have an Excel file with one column which is filled with numbers. I
I have a set of data which needs to be imported from a excel

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.