Solved

For each cells error

Posted on 2013-05-12
13
267 Views
Last Modified: 2013-05-12
Hi,

I got a error in the following code in the line "if cell.value = "READINGS BY" then". Any idea?

Sub READINGS()


Dim WS As Worksheet
Dim cell As Range



For Each WS In ThisWorkbook.Sheets

For Each cell In Sheets(WS.Index).Range("A1:J150").Cells
If cell.Value = "READINGS BY" Then
MsgBox ""
Exit Sub



MsgBox WS.Index
Exit Sub
End If


Next cell

Next WS

End Sub

Open in new window

0
Comment
Question by:joyacv2
  • 5
  • 5
  • 3
13 Comments
 
LVL 45

Expert Comment

by:Martin Liss
Comment Utility
Works fine for me.
0
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
Comment Utility
What is the error?

By the way, this:

Sheets(WS.Index).Range("A1:J150")

can be:

WS.Range("A1:J150")

Kevin
0
 
LVL 45

Expert Comment

by:Martin Liss
Comment Utility
What error do you get?
0
 
LVL 1

Author Comment

by:joyacv2
Comment Utility
error 2015
0
 
LVL 1

Author Comment

by:joyacv2
Comment Utility
if a change .value by .text, all ok, but i don't know why?
0
 
LVL 45

Expert Comment

by:Martin Liss
Comment Utility
Are any of your sheets protected or hidden?
What is WS.Name when the error occurs?
Is there anything unusual about that sheet?
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
Comment Utility
You probably have some errors in the target worksheets. Use the Text property to avoid any problems. It works in your case since you are comparing to a text string.

Kevin
0
 
LVL 45

Expert Comment

by:Martin Liss
Comment Utility
You said it was line 11 causing the problem, not line 12 so I'm confused.
0
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
Comment Utility
He didn't say anything about line 11. He said this line invoked an error:

"if cell.value = "READINGS BY" then"

Kevin
0
 
LVL 45

Expert Comment

by:Martin Liss
Comment Utility
You're right, sorry.
0
 
LVL 1

Author Comment

by:joyacv2
Comment Utility
but, why using .text solves the problem?
0
 
LVL 81

Accepted Solution

by:
zorvek (Kevin Jones) earned 500 total points
Comment Utility
Because the Text property ignores errors and returns the value in the cell as displayed. When you use the Value property the cell value is interpreted as it is stored internally. If the cell contains an error it will generate an error in many cases.

Kevin
0
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
Comment Utility
If you insist on using the Value property then you will have to test for errors in your code:

If Not IsError(Cell.Value) Then

Kevin
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
The view will learn how to download and install SIMTOOLS and FORMLIST into Excel, how to use SIMTOOLS to generate a Monte Carlo simulation of 30 sales calls, and how to calculate the conditional probability based on the results of the Monte Carlo …
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now