ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Finding outside set range (https://www.excelbanter.com/excel-programming/416143-finding-outside-set-range.html)

Sue

Finding outside set range
 
I am using Excel 2000 and have the following code:
With Worksheets("Data Lists").Range(WorkRange)
Set MyEdit = Cells.Find(AccCode, , xlValues, xlWhole, xlByColumns)
End With
This does find the AccCode in the WorkRange BUT it also finds the AccCode
outside the WorkRange. Why? How can I stop that?
Sue

Bob Bridges[_2_]

Finding outside set range
 
You're probably thinking that the With header limits everything that happens
inside it to the Range you specified. But that isn't how it works.

The With statement makes available a sort of default parent object inside
that section - but only AVAILABLE. So your With Worksheets("Data
Lists").Range(WorkRange) allows you, inside that section, to refer to
".Cells.Count" (notice the leading dot), and it'll tell you how many cells
are in Worksheets("Data Lists").Range(WorkRange).Cells.Count, or to
".Font.Bold" and it'll give you True or False depending on whether
Worksheets("Data Lists").Range(WorkRange).Font.Bold is True or False. The
leading dot specifies that the parent object for that term is to be whatever
you specified in the With statement.

But you never used a leading dot in any term in that section; you said "Set
MyEdit = Cells.Find(AccCode, , xlValues, xlWhole, xlByColumns)". Therefore
it did the Find inside the range defined by Cells with no leading dot; Access
assumes the default parent object for Cells is ActiveSheet, I believe, so it
looked in the whole active worksheet. What you want, I take it, is "Set
MyEdit = .Find(AccCode, , xlValues, xlWhole, xlByColumns)", where ".Find"
means "Worksheets("Data Lists").Range(WorkRange).Find". You see?

--- "Sue" wrote:
I am using Excel 2000 and have the following code:
With Worksheets("Data Lists").Range(WorkRange)
Set MyEdit = Cells.Find(AccCode, , xlValues, xlWhole, xlByColumns)
End With
This does find the AccCode in the WorkRange BUT it also finds the AccCode
outside the WorkRange. Why? How can I stop that?



All times are GMT +1. The time now is 10:05 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com