I need a macro to identify special characters and single zero. Then highlight those rows and return with the row number in a pop up window.
Example of list in column A:
98%kl
45tds$
123450
0
CE 123
This is what I have so far, but I can't find the way to highlight rows with a single zero ("0") and can't make it return the row numbers
Any help will be appreciated - Sub test()
-
-
Dim rng As Range, r As Range, m As Object
-
Set rng = Range("A1", Range("A" & Rows.Count).End(xlUp))
-
rng.Interior.ColorIndex = xlNone
-
With CreateObject("VBScript.RegExp")
-
.Global = True
-
.IgnoreCase = True
-
.Pattern = "([\(\)\\""!@#\$.%\^&\*\+\?~\€Ž\öôòóõ\~žŸœ¡¢£¤¥¦§®¯°±²³´µ¶·¸¹º»¼½¾¿àáâãäåæçèéêëìíîïðñòóôõö×øùúûü\-]|CLN|DG |CE | )"
-
For Each r In rng
-
If .test(r.Value) Then
-
r.Interior.Color = vbYellow
-
For Each m In .Execute(r.Value)
-
With r.Characters(m.firstindex + 1, m.Length).Font
-
.Bold = True
-
.Color = vbRed
-
If rng Is Nothing Then
-
MsgBox "No violations found"
-
Else
-
MsgBox "Violation found at " & rng.Row
-
-
End If
-
End With
-
Next
-
End If
-
Next
-
End With
-
End Sub
12 3684 zmbd 5,501
Expert Mod 4TB
Jomar153:
I just spotted this thread, are you still working on this issue?
Do you need the cell or the entire row highlighted (cell would be easier)?
Do you need to highlight something with two zeros, for example: "x00x" or "0x0x", or just a cell containing only a single "0"
Can we simplify your search to strings that only contain alphanumerics less the number 0, ([a-z][A-Z][1-9])?
what about spaces "x x" or "xxxx xx" etc...?
Do you really want a single message box with all of the row numbers in one go, or do you really need a message box each time the offending cell is located?
Line 21 - MsgBox "Violation found at " &
rng .Row
should read
Line 21 - MsgBox "Violation found at " &
r .Row
Line 17: If rng Is Nothing Then
I think you are testing the wrong object here... and if you are testing for an empty cell then this isn't the proper syntax/function.
@jomar153: Welcome to Bytes.com
I'm not familiar to your aproach with "VBScript.RegExp" (NeoPA - Mod - has explained it, but I still confusing)
So I give your my approach, another way to determind special character and the Zero alone, base on ACSII code: -
Public function FindWeirdCharacter()
-
-
Dim i as integer, n as integer
-
Dim RngArray As Variant
-
-
rngAray = Range("A1", Range("A" & Rows.Count).End(xlUp)).value
-
Range("A1", Range("A" & Rows.Count).End(xlUp)).Interior.ColorIndex = xlNone
-
-
for i = 33 to 254
-
if not (i > 47 and i < 58) and not (i > 64 and i < 123) then
-
for n = Lbound(RngArray) to Ubound(RngArray)
-
if instr(1, RngArray(n,1), chr(i),VbBinaryCompare) > 0 or Cstr(RngArray(n,1)) = "0" then
-
'Change Color for the cell that fullfill condition here (all special character and the alone-Zero here)
-
'Give Code here
-
end if
-
next n
-
end if
-
next i
-
-
end function
-
As I can see, your range from A1 and Xlup only in Column A mean your rng only in Column A that start from A1 then the rows position will be the var "n", but if you find a lot of row, and each row turnon Msgbox, you'll deal with lot of msgbox. That would be annoying.
so I suggest only change colour for the violent cells and you filter data by color to fix them or do whatever you want.
zmbd:
I need to identify cells containing any of the following:
special characters
spaces
cell containing only a single "0"
I don't need the entire row highlighted, a highlighted cell would be just fine. I prefer a message box each time the offending cell is located and activate it so that I can also see numbers listed before and after the offending cell.
@hvsummer
In your post, line 10 reads- - if not (i > 47 and i < 58) and not (i > 64 and i < 123) then
-
I realise that you're trying to identify the alpha-numeric characters, but ascii 91 - 96 are special characters as well.
zmbd 5,501
Expert Mod 4TB
ok,
still not 100% clear on the "single zero"
Highlight a cell with this "xxxxx0xxxxx" a single zero anywhere within the text; however, do not highlight a cell that contains two or more zeros anywhere within the text either as adjacent or separated entries within the text.
I take it that the 'single zero' refers just to "0" with no other text in the cell, as referred to in the example data.
zmbd 5,501
Expert Mod 4TB
codegazer: Let us not guess here... too many times have we guessed and then played the "20 questions game."
Sorry, today, I do not have time to play 20 questions... indeed, working in the lab, I usually have 10 to 15 minute blocks of time to help; thus, playing 20 questions just lengthens the time it takes to get to solution....
zmbd:
codegazer is right, the 'single zero' refers just to "0" with no other text in the cell.
One other question springs to mind.
Do you just want to identify and highlight cells with errors?
You then still have these cells to deal with, but you could also remove the problem characters at the same time as the scan if you wanted.
This link appears to show a very similar search. http://www.devguru.com/technologies/vbscript/14108
codegazer:
I do not want to remove the characters. I just need to identify and highlight the offending cells.
zmbd 5,501
Expert Mod 4TB
I leave the actual application up to OP; however, this is my take:
>Open VBE [Alt][F11]
>Insert a standard module
>Copy and paste the following code block in to the module
>Open the immediate pane/window [Ctrl][G]
>In the immediate pane type the following: ?fncB64113("StringHere")
Replace the "StringHere" with whatever you want to test
The number of matching characters will be returned, and in this case displayed in the immediate pane.
i.e. ?fncB64113("StringHere") = 0 ?fncB64113("String%%Here") = 2 ?fncB64113("0") = 1 ?fncB64113("String0Here") = 0 ?fncB64113("1") = 0
etc... - Option Explicit '<<This may already be in your module
-
-
Function fncB964113(zInString As String) As Integer
-
Dim zRegExp As Object
-
Dim zMatch As Object
-
Dim zo As Object
-
Dim zTestString As String
-
'
-
'error trapping
-
On Error GoTo zerrtrap
-
'
-
'With the argument being a string data type, this shouldn't be an issue; however, just incase someone changes the datatype to varient to handle null values
-
If (zInString & "") = "" Then Err.Raise Number:=(-2147221504 + 1000), Source:="fncB964113", Description:="Required input string missing"
-
zTestString = zInString
-
'
-
'setup the regular expressions object using late binding to avoid reference errors
-
Set zRegExp = CreateObject("VBScript.RegExp")
-
With zRegExp
-
.Global = True
-
.ignorecase = True
-
'will set the pattern to match later
-
End With
-
'
-
'Test the input string for possible matching
-
If Len(zTestString) > 1 Then
-
'
-
'No need to test for isolated zero test for only non-alphanumerics
-
With zRegExp
-
'
-
'I tried various \d \w etc... found this to be more reliable
-
.Pattern = "([^a-z^A-Z^0-9])"
-
Set zMatch = .Execute(zTestString)
-
fncB964113 = zMatch.Count
-
End With
-
Else
-
'
-
'Only one character in the string, so check to see if it is a zero
-
'> of course, one could use instr() or even a simple
-
'> if/then construct; however, the RegEx object is open so why not use it?
-
With zRegExp
-
.Pattern = "(0)"
-
Set zMatch = .Execute(zTestString)
-
fncB964113 = zMatch.Count
-
End With
-
End If
-
zcleanup:
-
If Not zRegExp Is Nothing Then Set zRegExp = Nothing
-
If Not zMatch Is Nothing Then Set zMatch = Nothing
-
Exit Function
-
zerrtrap:
-
MsgBox prompt:=Err.Source & vbCrLf & Err.Number & vbCrLf & Err.Description
-
fncB964113 = 0
-
Resume zcleanup
-
End Function
Written as a function, this should be really easy to integrate in to the existing logic... speaking of logic, one could change the fncB964113 from integer to Boolean and then return true or false instead of the count... up to the end user. ;-)
This has been tested and works within Office2013, all applications.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Sims |
last post by:
Hi,
I have a structure as follow
struct sIntStructure
{
int m_nNumber;
//
// A few more variables
//
|
by: Sami |
last post by:
Problem:
Social Security number being used in a database. First problem is
that it will not permit numbers beginning in zero to be entered - it
sees it as a null or empty space from what I can...
|
by: Tark Siala |
last post by:
hi
where i can find single view of any Sub or Function
in VS.Net 2003 (like vb6)?
--
Best Regards
Tark M. Siala
|
by: Phil Endecott |
last post by:
Dear Postgresql experts,
According to the documentation for MOVE, it returns the number of rows
that it has moved over. It seems to me that this is true for MOVE
FORWARD n, but not for MOVE...
|
by: rajamohammed |
last post by:
Hi every one,
Please tell me How to find No. of Digits in a number using java program?
|
by: bruce24444 |
last post by:
I'm working on a database created in Access 97 which has a form which tracks claims made for damages. When the person reports the claim, a form is used to enter Date Reported, Claim Number, Loss...
|
by: atulskulkarni |
last post by:
Hello All,
I am trying to extract text from the HTML using the following code,
use strict;
use HTML::Parser 3.00 ();
my %inside;
my $p;
|
by: zephyrus360 |
last post by:
This is about a technique to find the mod of a very large integer with a normal small integer.
I recently encountered this problem when I needed to compute the modulus of a very large number with...
|
by: firasoo |
last post by:
hi everyone
please I want to help me in finding function return number weekend in month with access database according to calendar in computer
for example if month is june the function will return...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
| | |