Solved

For each cells error

Posted on 2013-05-12
13
272 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
  • 3
13 Comments
 
LVL 47

Expert Comment

by:Martin Liss
ID: 39159951
Works fine for me.
0
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 39159953
What is the error?

By the way, this:

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

can be:

WS.Range("A1:J150")

Kevin
0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 39159954
What error do you get?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:joyacv2
ID: 39159957
error 2015
0
 
LVL 1

Author Comment

by:joyacv2
ID: 39159959
if a change .value by .text, all ok, but i don't know why?
0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 39159962
Are any of your sheets protected or hidden?
What is WS.Name when the error occurs?
Is there anything unusual about that sheet?
0
 
LVL 81

Expert Comment

by:zorvek (Kevin Jones)
ID: 39159963
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 47

Expert Comment

by:Martin Liss
ID: 39159967
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)
ID: 39159969
He didn't say anything about line 11. He said this line invoked an error:

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

Kevin
0
 
LVL 47

Expert Comment

by:Martin Liss
ID: 39159973
You're right, sorry.
0
 
LVL 1

Author Comment

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

Accepted Solution

by:
zorvek (Kevin Jones) earned 500 total points
ID: 39159990
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)
ID: 39159991
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

749 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