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 May 14th, 2002, 12:14 PM   #1
slimpickens
New Member
 
Join Date: Feb 2002
Posts: 35
Default

Hello,

I have trying everything I can think of to solve this problem. I a message with the end result being OK. When I click Ok, I want Sheets1, 2, 3 and 5 contents to be cleared leaving my headings. I have applied the following code but I get "subscript out of range". Take a look and see if you can tell me what is wrong with it.

Private Sub CommandButton1_Click()
Dim Range1 As Range
Dim Range2 As Range
Dim Range3 As Range
Dim Range4 As Range

Set Range1 = Worksheets("Sheet1").Range("A6:Z60")
Set Range2 = Worksheets("Sheet2").Range("A6:BC60")
Set Range1 = Worksheets("Sheet3").Range("A6:AY60")
Set Range1 = Worksheets("Sheet5").Range("A6:L60")
Range1.ClearContents
Range2.ClearContents
Range3.ClearContents
Range4.ClearContents

Any help would be appreciated.

Carol
slimpickens is offline   Reply With Quote
Old May 14th, 2002, 12:20 PM   #2
klb
Board Regular
 
Join Date: Apr 2002
Location: Minnesota
Posts: 821
Default

Set Range1 = Worksheets("Sheet3").Range("A6:AY60")
Set Range1 = Worksheets("Sheet5").Range("A6:L60")


Shouldn't these be Range3 and Range 4 respectively?
klb is offline   Reply With Quote
Old May 14th, 2002, 12:23 PM   #3
kkknie
Board Regular
 
Join Date: Apr 2002
Location: Greenwood, SC
Posts: 677
Default

Looks like a typo...

You set Range1 three times. Looks like it should have been Range3 and Range4. I duplicated this and got an object variable not set. Maybe the typo was in your post?

Also, when I first copied to a blank worksheet I got subscript out of range. The reason was becaus I didn't have a sheet called sheet5. I would bet your worksheet doesn't have the same sheet names either.

K
kkknie is offline   Reply With Quote
Old May 14th, 2002, 12:32 PM   #4
slimpickens
New Member
 
Join Date: Feb 2002
Posts: 35
Default

Hi K,

I made the correction and yes I do have sheet5, sheet 4 has the data for my controls on the form. I ran it again but I still get the same message. It highlights

Set Range1 = Worksheets("Sheet1").Range("A6:Z60")

Something has to be wrong with the code, but I can't figure out what is wrong. I will try messing around with it again and post if I make any progress.

Carol
slimpickens is offline   Reply With Quote
Old May 14th, 2002, 12:41 PM   #5
NateO
Legend
 
NateO's Avatar
 
Join Date: Feb 2002
Location: Minneapolis, Mn, USA
Posts: 9,704
Default

You don't have to set the range if you don't want to. The following should work:


Private Sub CommandButton1_Click()
Sheets("Sheet1").Range("A6:Z60").ClearContents
Sheets("Sheet2").Range("A6:BC60").ClearContents
Sheets("Sheet3").Range("A6:AY60").ClearContents
Sheets("Sheet5").Range("A6:L60").ClearContents
End Sub

__________________
Regards,
Nate Oliver
Microsoft Excel MVP
Nate's Excel Blog
NateO is offline   Reply With Quote
Old May 14th, 2002, 12:43 PM   #6
NateO
Legend
 
NateO's Avatar
 
Join Date: Feb 2002
Location: Minneapolis, Mn, USA
Posts: 9,704
Default

This works for me if you do want these assigned to variables:


Private Sub CommandButton1_Click()
Dim Range1 As Range, Range2 As Range, Range3 As Range, Range4 As Range
Set Range1 = Worksheets("Sheet1").Range("A6:Z60")
Set Range2 = Worksheets("Sheet2").Range("A6:BC60")
Set Range3 = Worksheets("Sheet3").Range("A6:AY60")
Set Range4 = Worksheets("Sheet5").Range("A6:L60")
Range1.ClearContents
Range2.ClearContents
Range3.ClearContents
Range4.ClearContents
End Sub


Are your sheets protected? You can't clear locked cells if the sheet's protected in either case.
_________________
Cheers, NateO

[ This Message was edited by: NateO on 2002-05-14 11:50 ]
NateO is offline   Reply With Quote
Old May 14th, 2002, 05:29 PM   #7
Ivan F Moala
MrExcel MVP
 
Ivan F Moala's Avatar
 
Join Date: Feb 2002
Location: Auckland, New Zealand
Posts: 4,209
Default

Also Try setting the commandbuttons
[TakeFocus*******] = False


__________________
Kind Regards,
Ivan F Moala From the City of Sails
Ivan F Moala is offline   Reply With Quote
Old May 15th, 2002, 10:49 AM   #8
slimpickens
New Member
 
Join Date: Feb 2002
Posts: 35
Default

I decided to record a macro for what I wanted done step by step and the coding is a little longer than the ones suggested but it is working up to the point now where I want to save and exit the application. I keep getting different error messages when I try to save & exit. Messages like:-

Automation error - the object invoked has disconnected from its client

Path/File access error.

Workbook not saved.

Take a look at this code and tell me where I am going wrong


Private Sub CommandButton1_Click()

Range("A6:Z70").Select
Selection.ClearContents
Range("A6").Select
Sheets("Marine").Select
Range("A6:BC70").Select
Selection.ClearContents
Range("A6").Select
Sheets(" Payments").Select
Range("A6:M70").Select
Selection.ClearContents
Range("A6").Select
Sheets("Motor & Accident").Select
Range("A6:AY70").Select
Selection.ClearContents
Range("A6").Select


End Sub


Private Sub ExitSave_Click()
For Each w In Application.Workbooks
w.Save
Next w
Application.Quit
End

End Sub

Private Sub Weekly_Click()

Dim MyDate
Dim MyMonth
MyDate = Date
MyMonth = Month(MyDate)
ThisWorkbook.SaveCopyAs Filename:="Claims-" & MonthName(MyMonth) & Day(Date) & "-" & Year(Date)
Response = MsgBox("Copy called Claims with today's date was saved,Please Email to Head Office", _
vbOKOnly)
If vbOK Then
CommandButton1_Click
ExitSave_Click
End If
End Sub

Any help would be appreciated

Carol
slimpickens is offline   Reply With Quote
Old May 16th, 2002, 10:25 AM   #9
slimpickens
New Member
 
Join Date: Feb 2002
Posts: 35
Default

I have solved my problem, but now another problem has surfaced. I have sent the mail by copying the workbook into a temporary file ,and then deleted the file after the mail has been sent - now when I want to close the original file and clear all the contents, I am running into a snag. How do I get it to close? I have tried putting in a command button called 'Close Workbook' on a form and loading this after the mail is sent but I get 'subscript out of range when I click it and it highlights line one of the code below. Can anyone offer any help??


Sheets("Fire").Select
Range("A6:Z70").Select
Selection.ClearContents
Range("A6").Select
Sheets("Marine").Select
Range("A6:BC70").Select
Selection.ClearContents
Range("A6").Select
Sheets(" Payments").Select
Range("A6:M70").Select
Selection.ClearContents
Range("A6").Select
Sheets("Motor & Accident").Select
Range("A6:AY70").Select
Selection.ClearContents
Range("A6").Select
slimpickens is offline   Reply With Quote
Old May 16th, 2002, 07:32 PM   #10
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-05-16 09:25, slimpickens wrote:
I have solved my problem, but now another problem has surfaced. I have sent the mail by copying the workbook into a temporary file ,and then deleted the file after the mail has been sent - now when I want to close the original file and clear all the contents, I am running into a snag. How do I get it to close? I have tried putting in a command button called 'Close Workbook' on a form and loading this after the mail is sent but I get 'subscript out of range when I click it and it highlights line one of the code below. Can anyone offer any help??


Sheets("Fire").Select
Range("A6:Z70").Select
Selection.ClearContents
Range("A6").Select
Sheets("Marine").Select
Range("A6:BC70").Select
Selection.ClearContents
Range("A6").Select
Sheets(" Payments").Select
Range("A6:M70").Select
Selection.ClearContents
Range("A6").Select
Sheets("Motor & Accident").Select
Range("A6:AY70").Select
Selection.ClearContents
Range("A6").Select
Are you in the correct workbook?
If so then is there a sheet named
Sheets "Fire"
__________________
Kind Regards,
Ivan F Moala From the City of Sails
Ivan F Moala 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 11:08 AM.


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