Type Mismatch error question

julhs

Active Member
Joined
Dec 3, 2018
Messages
471
Office Version
  1. 2010
Platform
  1. Windows
I need help to resolve the “Type Mismatch” error.
Posting this is a bit of a cop out!! But having spent last few hours trying to solve it myself, I turn to my trusted source for help!!
Code is this:-
VBA Code:
Sub MeUsedRangetest()
Dim lastRow As Long, firstrow As Long
Dim rng As Range
Dim sht As Worksheet
       Set sht = ThisWorkbook.ActiveSheet
       Set rng = sht.Range("T:T").Find(what:="CF rules", LookIn:=xlValues, LookAt:=xlWhole).Row
       Set rng = sht.Range("T:T").Find(what:="Cash Paid", LookIn:=xlValues, LookAt:=xlWhole).Row

          firstrow = sht.Range("T:T").Find(what:="CF rules", LookIn:=xlValues, LookAt:=xlWhole).Row
          lastRow = sht.Range("T:T").Find(what:="Cash Paid", LookIn:=xlValues, LookAt:=xlWhole).Row

      MsgBox "Range is " & rng.Address

End Sub

Getting Compile error = Type Mismatch, on this line:
VBA Code:
Set rng = sht.Range("T:T").Find(what:="CF rules", LookIn:=xlValues, LookAt:=xlWhole).Row
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
I hope you need something like this:
VBA Code:
Sub MeUsedRangetest()
Dim lastRow As Long, firstrow As Long
Dim rng As Range
Dim sht As Worksheet
       Set sht = ThisWorkbook.ActiveSheet
       firstrow = sht.Range("T:T").Find(what:="CF rules", LookIn:=xlValues, LookAt:=xlWhole).Row
       lastRow = sht.Range("T:T").Find(what:="Cash Paid", LookIn:=xlValues, LookAt:=xlWhole).Row
       Set rng = sht.Range("T" & firstrow & ":T" & lastRow)
       MsgBox "Range is " & rng.Address

End Sub
 
Upvote 0
Solution
VBA Code:
Set rng = sht.Range("T:T").Find(what:="CF rules", LookIn:=xlValues, LookAt:=xlWhole).Row
       Set rng = sht.Range("T:T").Find(what:="Cash Paid", LookIn:=xlValues, LookAt:=xlWhole).Row
remove .row from these 2 lines
 
Upvote 0
Type Mismatch is a runtime error, not a compile error.

The result of the expression on the right side of the "=" is the row number of the cell that is found. You are trying to assign a number to rng, which is a Range.

If you drop ".Row" this will work by setting rng to the cell that was found.
 
Upvote 0
Also you are setting rng twice in a row, so the first one is thrown away. It would help for you to describe what you want this code to do.
 
Upvote 0
Shingamilight
Removing the .row, results in “Run time Error 91, Object variable or With block not set”

Jeff
I’m trying to return to a message box the range coordinates of Col T, between;
VBA Code:
firstrow = sht.Range("T:T").Find(what:="CF rules", LookIn:=xlValues, LookAt:=xlWhole).Row
and
VBA Code:
lastRow = sht.Range("T:T").Find(what:="Cash Paid", LookIn:=xlValues, LookAt:=xlWhole).Row
 
Upvote 0
Sorry vtito, but your suggestion is exactly what I posted, so same Mismatch error
 
Last edited:
Upvote 0
Sorry vtito, but your suggestion is exactly what I posted, so same Mismatch error
But absolutely not
This rows deleted:
Set rng = sht.Range("T:T").Find(what:="CF rules", LookIn:=xlValues, LookAt:=xlWhole).Row
Set rng = sht.Range("T:T").Find(what:="Cash Paid", LookIn:=xlValues, LookAt:=xlWhole).Row
This row added:
Set rng = sht.Range("T" & firstrow & ":T" & lastRow)
Just copy and try my code.
 
Upvote 0
Try this
VBA Code:
Sub MeUsedRangetest()
Dim lastRow As Long, firstrow As Long
Dim rng As Long
Dim sht As Worksheet
Set sht = ThisWorkbook.ActiveSheet
        firstrow = sht.Range("T:T").Find(what:="CF rules", LookIn:=xlValues, LookAt:=xlWhole).Row
        lastRow = sht.Range("T:T").Find(what:="Cash Paid", LookIn:=xlValues, LookAt:=xlWhole).Row

         
      MsgBox "First row is " & firstrow & vbNewLine & "Last row is " & lastRow
     

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,896
Messages
6,175,262
Members
452,627
Latest member
KitkatToby

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