|
|
|
|
|
|
Excel
|
Loop through selection and act based on cell value
|
|
Ease of Use
|
Easy
|
Version tested with
|
2000
|
Submitted by:
|
lucas
|
Description:
|
Select an area on your spreadsheet. Run this macro and if it meets certain criteria the cell is highlighted.
|
Discussion:
|
You wish to see all cells that contain a certain value. This script will highlight those cells for you. If you don't make a selection, it will check every cell with a value. Or using the Alternate script, you can change all values that meet the criteria to a different value.
|
Code:
|
instructions for use
|
Place this code In a standard module:
Option Explicit
Option Compare Text
Sub Check_Values_1()
On Error Resume Next
Dim CurCell As Range
Dim Heading As String
Dim Prompt As String
Dim Criteria As Variant
Dim Color As Long
Dim lRows As Long
Dim lCols As Long
Dim lAllCells As Long
lRows = ActiveSheet.Rows.Count
lCols = ActiveSheet.Columns.Count
lAllCells = lRows * lCols
If Selection.Cells.Count = lAllCells Then
MsgBox "To check the entire sheet, please select only one cell", 64
Exit Sub
End If
Heading = "Enter Criteria"
Prompt = "Enter the value you want to find and highlight."
Color = 6
Criteria = InputBox(Prompt, Heading)
If Criteria = "" Then
Exit Sub
ElseIf IsNumeric(Criteria) Then
Criteria = CLng(Criteria)
ElseIf IsDate(Criteria) Then
Criteria = CDate(Criteria)
Else
Criteria = CStr(Criteria)
End If
If Selection.Cells.Count > 1 Then
For Each CurCell In Selection
If CurCell.Value = Criteria Then CurCell.Interior.ColorIndex = Color
Next CurCell
Else
For Each CurCell In ActiveSheet.UsedRange
If CurCell.Value = Criteria Then CurCell.Interior.ColorIndex = Color
Next CurCell
End If
End Sub
|
How to use:
|
- Open the Visual Basic Editor by going to tools-Macro's-Visual Basic Editor or use Alt-F11
- On the toolbar of the Visual Basic Editor, go to insert - module
- In the module pane paste the code above.
- Close the Visual Basic Editor By clicking the X in the upper right corner or go to File-Close
|
Test the code:
|
- Select the area in this example file of C4-D5 and run the macro by going to file-Macro's-Macro and select the Check_Values_1 macro and click on run.
- The cells with a value of 5 will be highlighted. Remember in this example you must select the range first.
- The alternate code will allow you to set the range so you don't have to select a range and shows how to replace one value with another.
|
Sample File:
|
Act_ Based_on_Cell_Value.zip 10.6KB
|
Approved by mdmackillop
|
This entry has been viewed 377 times.
|
|