Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 02-09-2012, 03:39 AM
tinfanide tinfanide is offline Range(Cell1,Cell2) Error on another workbook controlling some other workbook? Windows 7 64bit Range(Cell1,Cell2) Error on another workbook controlling some other workbook? Office 2010 32bit
Expert
Range(Cell1,Cell2) Error on another workbook controlling some other workbook?
 
Join Date: Aug 2011
Posts: 312
tinfanide is on a distinguished road
Default Range(Cell1,Cell2) Error on another workbook controlling some other workbook?

Code:
Sub test()

Dim book As Workbook
Dim sheet As Worksheet

Set book = Workbooks("test.xls")
Set sheet = book.Worksheets("Sheet1")

sheet.Activate

''' Application defined or Object defined error
sheet.Range(Cells(23, 2), Cells(47, 2)).Select
''' the below one didn't return an error
''' sheet.Range("B23:B47").Select

Set book = Nothing
Set sheet = Nothing
End Sub
When I run the Range(Cell1, Cell2).Select on a local worksheet, no problem.



But when I run the command on a local worksheet to select a range of cells on another worksheet, it returns the error (see the comment above)
Reply With Quote
  #2  
Old 02-09-2012, 04:08 PM
macropod's Avatar
macropod macropod is offline Range(Cell1,Cell2) Error on another workbook controlling some other workbook? Windows 7 64bit Range(Cell1,Cell2) Error on another workbook controlling some other workbook? Office 2010 32bit
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 21,956
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

Hi tinfanide,

It's because your 'Cells' references don't point to the 'sheet'.

BTW, it's not good practice to use names like 'sheet', as you can very quickly get that messed up with '.Sheet'. Furthermore, I see you're still trying to select things. You rarely need to select anything. For example:
Code:
Sub Test()
Dim xlWkBk As Workbook
Dim xlSht As Worksheet
Set xlWkBk = Workbooks("test.xls")
Set xlSht = xlWkBk.Worksheets("Sheet1")
With xlSht
  'do something, eg define a range to work on:
  With .Range(.Cells(23, 2), .Cells(47, 2))
    'do something, eg populate the defined range:
    .Value = Now
  End With
End With
Set xlSht = Nothing: Set xlWkBk = Nothing
End Sub
PS: Your question concerns Excel vba, but you posted it in the Word vba forum. I've moved it to the Excel forum.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Range(Cell1,Cell2) Error on another workbook controlling some other workbook? Shared Workbook SJT Excel 6 01-12-2012 11:33 AM
Loop through folder of workbooks and copy range to other workbook Snvlsfoal Excel Programming 3 07-29-2011 05:55 AM
Range(Cell1,Cell2) Error on another workbook controlling some other workbook? macro to transfer data from one workbook to another workbook virsojour Excel Programming 5 02-01-2011 08:58 PM
Range(Cell1,Cell2) Error on another workbook controlling some other workbook? Select a range in one one workbook while working in other workbook Slow&Steady Excel 1 02-21-2010 03:34 AM
Share Workbook cannot merge Ichigo Excel 3 01-19-2010 03:05 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:48 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft