473,396 Members | 1,915 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Find a single zero, special charcters and return row number

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


Expand|Select|Wrap|Line Numbers
  1. Sub test()
  2.  
  3.     Dim rng As Range, r As Range, m As Object
  4.     Set rng = Range("A1", Range("A" & Rows.Count).End(xlUp))
  5.     rng.Interior.ColorIndex = xlNone
  6.     With CreateObject("VBScript.RegExp")
  7.         .Global = True
  8.         .IgnoreCase = True
  9.         .Pattern = "([\(\)\\""!@#\$.%\^&\*\+\?~\€Ž\öôòóõ\~žŸœ¡¢£¤¥¦§®¯°±²³´µ¶·¸¹º»¼½¾¿àáâãäåæçèéêëìíîïðñòóôõö×øùúûü\-]|CLN|DG |CE | )"
  10.         For Each r In rng
  11.             If .test(r.Value) Then
  12.                 r.Interior.Color = vbYellow
  13.                 For Each m In .Execute(r.Value)
  14.                     With r.Characters(m.firstindex + 1, m.Length).Font
  15.                         .Bold = True
  16.                         .Color = vbRed
  17.                If rng Is Nothing Then
  18.         MsgBox "No violations found"
  19.         Else
  20.         MsgBox "Violation found at " & rng.Row
  21.  
  22.                           End If
  23.                     End With
  24.                 Next
  25.             End If
  26.         Next
  27.     End With
  28. End Sub
Sep 17 '15 #1
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
Expand|Select|Wrap|Line Numbers
  1. MsgBox "Violation found at " & rng.Row
should read
Line 21
Expand|Select|Wrap|Line Numbers
  1. 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.
Dec 21 '15 #2
hvsummer
215 128KB
@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:
Expand|Select|Wrap|Line Numbers
  1. Public function FindWeirdCharacter()
  2.  
  3. Dim i as integer, n as integer
  4. Dim RngArray As Variant
  5.  
  6. rngAray = Range("A1", Range("A" & Rows.Count).End(xlUp)).value
  7. Range("A1", Range("A" & Rows.Count).End(xlUp)).Interior.ColorIndex = xlNone
  8.  
  9.  for i = 33 to 254
  10.    if not (i > 47 and i < 58) and not (i > 64 and i < 123) then
  11.      for n = Lbound(RngArray) to Ubound(RngArray)
  12.        if instr(1, RngArray(n,1), chr(i),VbBinaryCompare) > 0 or Cstr(RngArray(n,1)) = "0" then
  13.          'Change Color for the cell that fullfill condition here (all special character and the alone-Zero here)
  14.          'Give Code here
  15.        end if
  16.      next n
  17.    end if
  18.  next i 
  19.  
  20. end function
  21.  
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.
Dec 25 '15 #3
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.
Jan 12 '16 #4
@hvsummer

In your post, line 10 reads-

Expand|Select|Wrap|Line Numbers
  1.  if not (i > 47 and i < 58) and not (i > 64 and i < 123) then
  2.  
I realise that you're trying to identify the alpha-numeric characters, but ascii 91 - 96 are special characters as well.
Jan 16 '16 #5
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.
Jan 16 '16 #6
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.
Jan 16 '16 #7
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....
Jan 16 '16 #8
zmbd:

codegazer is right, the 'single zero' refers just to "0" with no other text in the cell.
Jan 27 '16 #9
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
Jan 27 '16 #10
codegazer:

I do not want to remove the characters. I just need to identify and highlight the offending cells.
Jan 28 '16 #11
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...

Expand|Select|Wrap|Line Numbers
  1. Option Explicit '<<This may already be in your module
  2.  
  3. Function fncB964113(zInString As String) As Integer
  4.     Dim zRegExp As Object
  5.     Dim zMatch As Object
  6.     Dim zo As Object
  7.     Dim zTestString As String
  8.     '
  9.     'error trapping
  10.     On Error GoTo zerrtrap
  11.     '
  12.     '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
  13.     If (zInString & "") = "" Then Err.Raise Number:=(-2147221504 + 1000), Source:="fncB964113", Description:="Required input string missing"
  14.     zTestString = zInString
  15.     '
  16.     'setup the regular expressions object using late binding to avoid reference errors
  17.     Set zRegExp = CreateObject("VBScript.RegExp")
  18.     With zRegExp
  19.         .Global = True
  20.         .ignorecase = True
  21.         'will set the pattern to match later
  22.     End With
  23.     '
  24.     'Test the input string for possible matching
  25.     If Len(zTestString) > 1 Then
  26.     '
  27.     'No need to test for isolated zero test for only non-alphanumerics
  28.         With zRegExp
  29.         '
  30.         'I tried various \d \w etc... found this to be more reliable
  31.             .Pattern = "([^a-z^A-Z^0-9])"
  32.             Set zMatch = .Execute(zTestString)
  33.             fncB964113 = zMatch.Count
  34.         End With
  35.     Else
  36.     '
  37.     'Only one character in the string, so check to see if it is a zero
  38.     '> of course, one could use instr() or even a simple
  39.     '> if/then construct; however, the RegEx object is open so why not use it?
  40.         With zRegExp
  41.             .Pattern = "(0)"
  42.             Set zMatch = .Execute(zTestString)
  43.             fncB964113 = zMatch.Count
  44.         End With
  45.     End If
  46. zcleanup:
  47.     If Not zRegExp Is Nothing Then Set zRegExp = Nothing
  48.     If Not zMatch Is Nothing Then Set zMatch = Nothing
  49. Exit Function
  50. zerrtrap:
  51.     MsgBox prompt:=Err.Source & vbCrLf & Err.Number & vbCrLf & Err.Description
  52.     fncB964113 = 0
  53.     Resume zcleanup
  54. 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.
Jan 28 '16 #12
zmbd
5,501 Expert Mod 4TB
opps... forgot the address thing :)
Range.Address Property (Excel)
Example there...

and you might find how I handled ranges in this post's code useful: https://bytes.com/topic/excel/answer...el#post3799741
Jan 28 '16 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Sims | last post by:
Hi, I have a structure as follow struct sIntStructure { int m_nNumber; // // A few more variables //
6
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...
6
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
1
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...
3
by: rajamohammed | last post by:
Hi every one, Please tell me How to find No. of Digits in a number using java program?
4
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...
1
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;
0
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...
1
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
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...
0
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...
0
marktang
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,...
0
Oralloy
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,...
0
tracyyun
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.