Inconsistent Range Method!

pjmorris

Well-known Member
Joined
Aug 2, 2012
Messages
2,172
Office Version
  1. 365
Platform
  1. Windows
Hi, I am trying to collate a number of different spreadsheets using VBA to achieve a complicated layout.

The following code exert should find a string within a calculated range:

Code:
sDate2 = wbFMPPrevious.Worksheets(wsDestination.Name).Range(Cells(nPrevAvail + 2, 1), Cells(nPrevAvail + 11, 1)).Find("MD", lookat:=xlPart)

however it fails whenever the workbook identified by 'wbFMPPrevious' isn't active. To show you how I've come to this conclusion:

This code (a simplified version of the previous example):
Code:
wbFMPPrevious.Worksheets(wsDestination.Name).Range(Cells(5, 1), Cells(14, 1)).address

returns '$A$5:$A$14' which it should, but only when the last sheet I looked at was 'FMP 18-03.xls' as returned by wbFMPPrevious.name. If I look at another sheet or even simply run the process from the parent sheet, which is what I need to do, then it fails with a Run-time error '1004': Application defined or object defined error'.

I don't mind if I've made a mistake, but this apparent inconsistent behaviour is driving me insane!

Please help.

Have a good Christmas and a great New Year.

Regards
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Hi,
Try this:
Rich (BB code):
With wbFMPPrevious.Worksheets(wsDestination.Name)
  sDate2 = .Range(.Cells(nPrevAvail + 2, 1), .Cells(nPrevAvail + 11, 1)).Find("MD", lookat:=xlPart)
End Sub
See the (Red) dot symbols before the Cells, without it the Cells refer to the ActiveSheet
 
Last edited:
Upvote 0
Jonmo1 & ZVI,

Thank you both for your helpful response. This is entirely consistent with the behavior I'm seeing so will correct when back in the office.

That said it seems a bit ridiculous to have to qualify the cells element of an already qualifed .Range - no doubt it is entirely consistent with the object model, but does seem rather strange.

Hope you both have a good Christmas.

Regards
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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