Ron99,
Sample raw data:
Excel 2007 |
---|
|
---|
| A |
---|
1 | 123456 |
---|
2 | 456789 |
---|
3 | 147896 |
---|
4 | 158496 |
---|
5 | 123456 |
---|
6 | |
---|
|
---|
If we search for a valid positive number, like, 123456, we get:
Excel 2007 |
---|
|
---|
| A |
---|
1 | 123456 |
---|
2 | 456789 |
---|
3 | 147896 |
---|
4 | 158496 |
---|
5 | 123456 |
---|
6 | |
---|
|
---|
If you were to enter anything but a positive number you would get an error message, and, the macro would terminate.
And, the macro will only test the used range in column A, not all cells in column A.
Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).
1. Copy the below code, by highlighting the code and pressing the keys
CTRL +
C
2. Open your workbook
3. Press the keys
ALT +
F11 to open the Visual Basic Editor
4. Press the keys
ALT +
I to activate the Insert menu
5. Press
M to insert a Standard Module
6. Where the cursor is flashing, paste the code by pressing the keys
CTRL +
V
7. Press the keys
ALT +
Q to exit the Editor, and return to Excel
8. To run the macro from Excel, open the workbook, and press
ALT +
F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.
Code:
Option Explicit
Sub ColorMyNumber()
' hiker95, 04/10/2013
' http://www.mrexcel.com/forum/excel-questions/696141-macro-help.html
Dim a As Variant, my As Long, i As Long, n As Long
On Error Resume Next
my = Application.InputBox("Enter No")
On Error GoTo ErrorExit
If my <= 0 Then GoTo ErrorExit
a = Cells(1).CurrentRegion
For i = 1 To UBound(a, 1)
If a(i, 1) = my Then
Cells(i, 1).Interior.ColorIndex = 6
n = n + 1
End If
Next i
If n = 0 Then
MsgBox "The macro did not find any numbers!"
Else
MsgBox "The macro found " & n & " numbers."
End If
Exit Sub
ErrorExit:
MsgBox "You did not enter a valid positive number - macro terminated!"
End Sub
Before you use the macro with Excel 2007 or newer, save your workbook, Save As, a macro enabled workbook with the file extension
.xlsm
Then run the
ColorMyNumber macro.