Type Mismatch error

carbonbased65

New Member
Joined
Nov 2, 2022
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hi, I hope you are well.

I am trying to create a simple script to dynamically list all worksheets that have the text "Include" in cell A4 but am getting a Type Mismatch error.
I think it has something to do with using ws.Name in the Worksheets() expression because if I test this using a sheet name e.g. Worksheets("Nova") it will return all sheets (as Nova has "Include" in cell A4.
This script lists all the worksheet names perfectly if I do not use the if statement to refine the output.

Any assistance you can offer to resolve this would be most appreciated.

VBA Code:
Sub list_sheets()

Dim ws As Worksheet
Dim msg As String

For Each ws In ThisWorkbook.Worksheets
    If Worksheets(ws.Name).Range("A4") = "Include" Then
       msg = msg & ws.Name & vbNewLine
    End If
Next ws

MsgBox (msg)

End Sub

Thank You
BR
-Andrew
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Welcome to the Board!

Since "ws" is already a Worksheet object, you should just be able to replace this:
VBA Code:
If Worksheets(ws.Name).Range("A4") = "Include" Then
with this:
VBA Code:
If ws.Range("A4") = "Include" Then
 
Upvote 0
Hi & welcome to MrExcel.
Which line of code gives the error?
 
Upvote 0
Hi & welcome to MrExcel.
Which line of code gives the error?
They had originally tried to highlight this line in the code:
VBA Code:
    If Worksheets(ws.Name).Range("A4") = "Include" Then
but as you know, you cannot format text when using VBA Code tags. So I cleaned up that code in the original post.

So I am guessing that is the offending line.
 
Upvote 0
You would get that error if some sheet has an error value (e.g. #DIV/0) in A4.
You might try
VBA Code:
If CStr(Worksheets(ws.Name).Range("A4")) = "Include" Then
 
Upvote 0
Solution
Welcome to the Board!

Since "ws" is already a Worksheet object, you should just be able to replace this:
VBA Code:
If Worksheets(ws.Name).Range("A4") = "Include" Then
with this:
VBA Code:
If ws.Range("A4") = "Include" Then
Thanks Joe for the object tip - appreciated.
 
Upvote 0
You would get that error if some sheet has an error value (e.g. #DIV/0) in A4.
You might try
VBA Code:
If CStr(Worksheets(ws.Name).Range("A4")) = "Include" Then
Thanks for the solution mikerickson this works perfectly !
I just checked and 1 sheet had a #ref error in it :oops:
 
Upvote 0

Forum statistics

Threads
1,214,537
Messages
6,120,096
Members
448,944
Latest member
SarahSomethingExcel100

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