MrExcel Message Board

Go Back   MrExcel Message Board > Question Forums > Excel Questions

Excel Questions All Excel/VBA questions - formulas, macros, pivot tables, general help, etc. Please post to this forum in English only.

Reply
 
Thread Tools Display Modes
Old Mar 27th, 2002, 12:37 PM   #1
RET79
Board Regular
 
Join Date: Mar 2002
Location: England, UK.
Posts: 526
Default

application.workbooks.worksheets.range.offset.select

This particular line of code is annoying me lately, the VBE doesn't always like it. I tend to make statements like this at the beginning of my programs

Set result = application.thisworkbook.worksheets("results")

Set Source = application.workbooks("SourceFile.xls").worksheets("data")

But, when later in the program I do this sort of thing:

result.range("A1:A4").Offset(1,0). Select

Source.Select


Then it doesn't work! Why is this the case? Sometimes they only work if I am on that active sheet/book only. This is very annoying, any help appreciated.

RET79
RET79 is offline   Reply With Quote
Old Mar 27th, 2002, 12:55 PM   #2
NateO
Legend
 
NateO's Avatar
 
Join Date: Feb 2002
Location: Minneapolis, Mn, USA
Posts: 9,704
Default

Error 1004 eh? Might want to try selecting the sheet first, like:

Sub no1004()
result = Worksheets("sheet2").Name
Sheets(result).Select
[a1:a4].Offset(1, 0).Select
End Sub

Hope this helps.
__________________
Regards,
Nate Oliver
Microsoft Excel MVP
Nate's Excel Blog
NateO is offline   Reply With Quote
Old Mar 27th, 2002, 12:56 PM   #3
RET79
Board Regular
 
Join Date: Mar 2002
Location: England, UK.
Posts: 526
Default

To start answering my own question.... I tried this just now

Dim dest As Object
Dim source As Object
Set dest = Application.ThisWorkbook.Worksheets("Sheet2")
Set source = Application.Workbooks("test.xls").Worksheets("Sheet1")

and then did stuff like this

source.activate
Range("A20:C20 = a

and this works fine.

However, is using this "activate" thing the best way to code? Every time I want to select a sheet to do something to it, I have to do result.activate first, which is a bit of a pain. Why should I have to do this when I have already defined the workbook and sheet at the beginning? Why can't I just write

result.range("Q34") = 4

when i do that I get error messages if result is not the current active sheet.

Thanks.

RET79
RET79 is offline   Reply With Quote
Old Mar 27th, 2002, 04:25 PM   #4
Adcb
 
Join Date: Mar 2002
Posts: 12
Default

You can't select a range without activating/selecting the sheet first.

But ... why do any selecting?
You can do your thing to the range without having to select anything.
Adcb is offline   Reply With Quote
Old Mar 27th, 2002, 09:13 PM   #5
Ivan F Moala
MrExcel MVP
 
Ivan F Moala's Avatar
 
Join Date: Feb 2002
Location: Auckland, New Zealand
Posts: 4,209
Default

Quote:
On 2002-03-27 11:37, RET79 wrote:
application.workbooks.worksheets.range.offset.select

This particular line of code is annoying me lately, the VBE doesn't always like it. I tend to make statements like this at the beginning of my programs

Set result = application.thisworkbook.worksheets("results")

Set Source = application.workbooks("SourceFile.xls").worksheets("data")

But, when later in the program I do this sort of thing:

result.range("A1:A4").Offset(1,0). Select

Source.Select


Then it doesn't work! Why is this the case? Sometimes they only work if I am on that active sheet/book only. This is very annoying, any help appreciated.

RET79
Using Select with your Object coding will
only work if the workbook is the active workbook.
Try using Activate instead ie. replace all
your Select with Activate. Using the keyword
Activate for a Worksheet Object is the
equivalent to clicking the sheet’s tab.


eg using your code, which by the way is a good way to reference the Workbooks.....

application.workbooks.worksheets.range.offset.Activate

Set result = application.thisworkbook.worksheets("results")

Set Source = application.workbooks("SourceFile.xls").worksheets("data")

But, when later in the program I do this sort of thing:

result.range("A1:A4").Offset(1,0).Activate

Source.Activate


Ivan
Ivan F Moala is offline   Reply With Quote
Old Mar 27th, 2002, 09:41 PM   #6
Colo
MrExcel MVP
 
Colo's Avatar
 
Join Date: Mar 2002
Location: Kobe, Japan
Posts: 1,420
Default

Hello. Just my opinion...
I do not like use "Activate method" or "Select method" except when absolutely required.

One of the reasons is that execution speed is slow.
Another reason , "Select or Activate" is not the purpose.
The purpose is write a value to a cell.

if Workbook("test.xls") is open, I think I do not have to "Select" or "Activate".
An error does not come out in my environment.
Regards,

Sub sample()
Dim dest As Object
Dim source As Object, a As String
Set dest = Application.ThisWorkbook.Worksheets("Sheet2")
Set source = Application.Workbooks("test.xls").Worksheets("Sheet1")
a = "sample"
'The following code works even if "test.xls" is not activeworkbook
source.Range("A20:C20").Value = a
End Sub
Colo is offline   Reply With Quote
Old Mar 27th, 2002, 09:53 PM   #7
Ivan F Moala
MrExcel MVP
 
Ivan F Moala's Avatar
 
Join Date: Feb 2002
Location: Auckland, New Zealand
Posts: 4,209
Default

Quote:
On 2002-03-27 20:41, Colo wrote:
Hello. Just my opinion...
I do not like use "Activate method" or "Select method" except when absolutely required.

One of the reasons is that execution speed is slow.
Another reason , "Select or Activate" is not the purpose.
The purpose is write a value to a cell.

if Workbook("test.xls") is open, I think I do not have to "Select" or "Activate".
An error does not come out in my environment.
Regards,

Sub sample()
Dim dest As Object
Dim source As Object, a As String
Set dest = Application.ThisWorkbook.Worksheets("Sheet2")
Set source = Application.Workbooks("test.xls").Worksheets("Sheet1")
a = "sample"
'The following code works even if "test.xls" is not activeworkbook
source.Range("A20:C20").Value = a
End Sub
Colo I agree 100%....the example was in answer to the original Question and not about the best method to use as you suggest.

Ivan
Ivan F Moala is offline   Reply With Quote
Old Mar 28th, 2002, 01:09 AM   #8
Dave Hawley
Banned
 
Join Date: Feb 2002
Posts: 1,582
Default

Agree completetly with Colo and Ivan, get away from selecting, select, activate and active. BUT if you really must just use:

Application.Goto result.range("A1:A4").Offset(1,0)



Dave Hawley is offline   Reply With Quote
Old Mar 28th, 2002, 04:13 PM   #9
RET79
Board Regular
 
Join Date: Mar 2002
Location: England, UK.
Posts: 526
Default

Thanks guys, I agree 100% about not liking to use activate or select, this is one reason I posted this message in the first place.

Could there be something wrong with my Excel 'environment' that I am having difficulties with such an easy task? It is very annoying to waste so much time trying to figure out why application.workbooks("file.xls").worksheets("Sheet1").Range etc. does not work even though file is open and so on. I am convinced that perhaps some setting needs changing on excel, or perhaps someone at work has some built in macro on startup that means that it won't accept this sort of method.

If anyone has any hints as to how I woudl check this out then please let me know.
RET79 is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT -4. The time now is 07:17 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
All contents Copyright 1998-2012 by MrExcel Consulting.
diabetic desserts recipes recipes Diabetic Soups Holiday Pizza Recipes Popcorn Recipes Recipes For Microwave Pasta Recipes Casserole Recipes Chili Recipes Curry Recipes Crockpot Recipes Apples Recipes Bread Recipes Vegetarian Recipes Vegetable recipes Desserts Recipes Appetizers Ethnic Recipes Meat Dishes Barbecue Recipes Sauces Recipes Marinade Recipes Low Fat Recipes Frugal Gourmet Kitchen Classics Recipes On The Grill Cook Books Seafood Recipes Cajun Recipes Breads Low Fat Low Fat Breads Bread Machine Recipes Yeast Breads Quick Breads Fat Free Vegetarian Salad Recipes Eggplant Recipes Radish Recipes Tomato Recipes Jalapeno Recipes Potato Recipes Lettuce Recipes Cabbage Recipes Beans Ambrosia Recipes Biscotti Recipes Desserts Low Fat Cookie Recipes Cheesecake Recipes Cake Recipes Pie Recipes Muffin Recipes Custard Recipes Best Appetizers Appetizers Low Fat Salsa Recipes Dip Recipes International Recipes Afghan Recipes Alaska Recipes French Recipes German Recipes Greek Recipes Italian Recipes Spanish Recipes Thai Recipes Korean Recipes Chinese Recipes Mexican Recipes Indian Recipes Beef Recipes Pork Pork & Ham Pork Butts Pork Chop Recipes Pork Ribs Rulled Pork Poultry Recipes Stews Recipes Ground Beef Barbecue Grill Barbecue Smoker All Purpose Sauce BBQ Sauce Barbecue Sauce Carolina BBQ Sauce Pickle Recipes Marinades Smoking Low Fat Appetizers & Dips Low Fat Breakfast Low Fat Cakes Low Fat Cheesecakes Low Fat Cookies Low Fat Desserts Low Fat Fish & Seafood Low Fat Meats Low Fat Pasta Low Fat Pies Low Fat Salads Low Fat Sandwiches Low Fat Sauces & Condiments Low Fat Sides Low Fat Soups Low Fat Vegetarian Baker's Dozen Taste of Home Recipe Book Bon Appetit Cookbook Blacktie Cookbook Buster Cook Book Cookbook USA Cook Book Cook Book Sara's Cookbook Sara's Cookbook Appetizers and Dips Poultry recipes Diabetic recipes Holiday recipes Miscellaneous recipes 110 recipes 1986 Usenet cookbook 2900 recipes Cyberrealm recipes Great sysops of world Specialty recipes Ceideburg recipes Cheese recipes Chili recipes Fruits recipes Garlic recipes Great chefs of NY Londontowne recipes Raisins recipes Recipes for kids US Food Vegetarian recipes Bread recipes Drinks Meat Dishes Brisket recipes Caribou recipes Chicken recipes Filet mignons recipes Pork recipes Swordfish recipes Turkey recipes Pasta recipes Uncategorized recipes Ethnic recipes Canada recipes English recipes Ethiopia recipes Germany recipes Greece recipes Mexican recipes Philippines recipes Welsh recipes Microwave recipes Soups recipes Vegetable recipes Asparagus recipes Barley recipes Brown rice recipes Lentil recipes Mushrooms recipes Salads recipes Wild rice Desserts recipes Cakes recipes Chocolate recipes Cookies recipes Ice cream recipes