Hello all,
As I am sure many of us do, I have found a code to accomplish what I am looking for. To clear formats, and then with a msg box, find in a range all that match my criteria and highlight. Although, the issue I am having now is a type mismatch error on this specific line
Could anyone offer some assistance as I am fairly new to VBA and still learning. I have attached my entire code down below.
Thank you
As I am sure many of us do, I have found a code to accomplish what I am looking for. To clear formats, and then with a msg box, find in a range all that match my criteria and highlight. Although, the issue I am having now is a type mismatch error on this specific line
Code:
Set FoundCell = myRange.Find(what:=fnd, after:=LastCell)
Could anyone offer some assistance as I am fairly new to VBA and still learning. I have attached my entire code down below.
Code:
Sub NameRange_Add()Dim cell As Range
Dim RangeName As String
Dim CellName As String
Dim fnd As String, FirstFound As String
Dim FoundCell As Range, rng As Range
Dim myRange As Range, LastCell As Range
RangeName = "Dowelings"
CellName = "C8:K27,M8:T15,AR18:BD25,BC26:BD37,BG8:BI37,Q40:U41,Z40:Z41,AC40:AC41,AF40:AF41,AI40:AI41,AK42:AL49,AN40:AX41,AN42:AV43,AQ44:AT45,AZ40:BJ41,BE42:BJ43"
Set cell = Worksheets("DOWELING-MAP").Range(CellName)
ThisWorkbook.Names.Add Name:=RangeName, RefersTo:=cell
Range("C8:K27,M8:T15,AR18:BD25,BC26:BD37,BG8:BI37,Q40:U41,Z40:Z41,AC40:AC41,AF40:AF41,AI40:AI41,AK42:AL49,AN40:AX41,AN42:AV43,AQ44:AT45,AZ40:BJ41,BE42:BJ43").Select
With Selection.Interior
.Pattern = xlNone
.TintAndShade = 0
.PatternTintAndShade = 0
End With
'PURPOSE: Highlight all cells containing a specified values
'SOURCE: www.TheSpreadsheetGuru.com
'What value do you want to find?
fnd = InputBox("I want to hightlight cells containing...", "Highlight")
'End Macro if Cancel Button is Clicked or no Text is Entered
If fnd = vbNullString Then Exit Sub
Set myRange = ActiveSheet.Range(CellName)
Set LastCell = myRange.Cells(myRange.Cells.Count)
Set FoundCell = myRange.Find(what:=fnd, after:=LastCell)
'Test to see if anything was found
If Not FoundCell Is Nothing Then
FirstFound = FoundCell.Address
Else
GoTo NothingFound
End If
Set rng = FoundCell
'Loop until cycled through all unique finds
Do Until FoundCell Is Nothing
'Find next cell with fnd value
Set FoundCell = myRange.FindNext(after:=FoundCell)
'Add found cell to rng range variable
Set rng = Union(rng, FoundCell)
'Test to see if cycled through to first found cell
If FoundCell.Address = FirstFound Then Exit Do
Loop
'Highlight Found cells yellow
rng.Interior.Color = RGB(255, 255, 0)
'Report Out Message
MsgBox rng.Cells.Count & " cell(s) were found containing: " & fnd
Exit Sub
'Error Handler
NothingFound:
MsgBox "No cells containing: " & fnd & " were found in this worksheet"
End Sub
Thank you