You are here:

Excel/Special characters


Hi Stuart Resnick

I am beginner and try to control some special characters.

I just need to check the particular range has any special character or not. If there is any, it should not allow me to go further. The below coding is working, but it warns me TWICE and third time it allows me to go further. I am missing something here. Can you help me here...

I have used this coding into module, and calling this procedure in my other procedure using like (call ChrChk)...

Please note that I am controlling any range, but for "F" coloumn. The range is received through user input in M1, M2 cells.

Hope i m making it clear now.

Sub ChrChk()
Dim r As Range
With Sheets("Sheet2") For Each r In Range("F" & .Range("M1").Value & ":F" & .Range("M2").Value)

If r.Value Like "*[!0-9,a-z,.,@]*" Then
MsgBox "Special Characters '*[!0-9,a-z,.,@]* ' are not allowed", vbCritical, "Special character"

End If


End With

End Sub




ANSWER: If the procedure finds a special character, you want it to show message and then stop... you don't want it to continue looking for more special characters in the range.

Therefore, after the MsgBox line of code, the next line should be:

Exit Sub

---------- FOLLOW-UP ----------

QUESTION: Perfect. It worked and I could also recall it now. However, i get another problem. The next action is not performing...The below is entire coding tells you what i am trying to do here...

If WorksheetFunction.CountIf(.Range("P" & .Range("M1").Value & ":P" & .Range("M2").Value), ">50") > 0 Then
       JV = MsgBox("Hey...the 'Line Item text length' is more than 50", vbCritical, "Text length check")
For Each r In Range("F" & .Range("M1").Value & ":F" & .Range("M2").Value)
         If r.Value Like "*[!0-9,a-z,.,@]*" Then
         MsgBox "Doc.Head Text-Special Characters '*[!0-9,a-z,.,@]* ' are not allowed", vbCritical, "Special character"
         Exit Sub
         End If

ElseIf Range("M1").Value = 0 Or Range("M2").Value = 0 Then
       JV = MsgBox("Hey....the Data range is empty. You cannot generate Journal", vbCritical, "Journal Range Check")


Can you guide me please, i do again some mistake here..

Thanks in advance,


The code you sent isn't correct. Look at the structure of your IF lines:

If ...
Else ...
ElseIf ...

So you must clean that up.

Then: if, after the MsgBox row, you want to stop looking for special characters, but CONTINUE with other lines of code in the procedure, then instead of Exit Sub, you should use Exit For.
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here


All Answers

Answers by Expert:

Ask Experts


Stuart Resnick


I can answer questions relating to MS Excel formulas, or to programming with vba (Visual Basic for Applications) in the Excel environment. Please follow the following guidelines: your question should focus on one specific issue you want to learn. It's beyond the scope of this free service for me to create entire projects or complex vba solutions for you from scratch. You should be able to do most of the work yourself, and come here when you need help with a specific point you're stuck on. ALWAYS include a simple, concrete example illustrating what you want to learn. Explain this example in detail in the text of your question (what data is in which cells of which sheets, etc). Be very precise about the results you want, using this sample to make the logic clear. Always keep these examples SIMPLE. Never e.g. use 18 worksheets in your example if using 2 or 3 will do. Never use ranges like AI567:BB865 if using a range like A1:B3 will do. Thanks.


As a consultant, I've designed Excel tools since the 90s, working for the Federal Reserve Bank, AT&T, and (currently) Gap Inc.

My only "education" comes from 2 decades of doing spreadsheet/programming work, with major SF Bay Area corporations such as AT&T, Federal Reserve Bank, and Gap Inc.

©2017 All rights reserved.