Type Mismatch Error - VBA Code

ADSkinner

New Member
Joined
Aug 18, 2018
Messages
19
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

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
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.

Forum statistics

Threads
1,214,646
Messages
6,120,718
Members
448,986
Latest member
andreguerra

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top