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 15th, 2002, 08:48 AM   #1
ermccarthy
Board Regular
 
Join Date: Feb 2002
Location: San Antonio, TX
Posts: 186
Default

I have a macro that runs and looks for an entered "account number". If the user mistypes the account number it search and returns a Run time error # 9: subscript out of range.

I would like to put in the macro, if this error occurs to exit the loop, and display a message box that says "Please check your account number", and continues on with the rest of the macro....

Any Ideas.....Here is the Macro...

acct1 = Range("acct1").Value
acct2 = Range("acct2").Value
acct3 = Range("acct3").Value
acct4 = Range("acct4").Value
path1 = Range("path1").Value
path2 = Range("path2").Value
path3 = Range("path3").Value
path4 = Range("path4").Value
Sht1 = Range("Sheet1").Value
Sht2 = Range("Sheet2").Value
Sht3 = Range("Sheet3").Value
Sht4 = Range("Sheet4").Value
workbk1 = Range("FileName").Value
acct = ActiveCell.Value
Sheets("Matrix").Visible = True
Worksheets("matrix").Select
Range("D4").Select
Sheets("PO").Select
Range("G49:G52").Select
Selection.ClearContents
Sheets("Matrix").Select
Range("G4:G7").Select
Selection.ClearContents
Sheets("PO").Select
Range("F48").Select
Worksheets("matrix").Select
Range("D4").Select
If ActiveCell.Value <> "" Then
Workbooks.Open FileName:=path1
Worksheets(Sht1).Select
Range("F11").Select
Do Until ActiveCell.Value = acct1
ActiveCell.Offset(0, 1).Select 'this is where the error pops in....
Loop
ActiveCell.Offset(100, 0).Select
Do Until ActiveCell <> ""
ActiveCell.Offset(-1, 0).Select
Loop
Selection.Copy
'close the workbook

Workbooks(workbk1).Activate
ActiveCell.Offset(0, 3).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
ActiveCell.Offset(1, -3).Select
Windows("Current year.xls").Activate
ActiveWorkbook.Close
Workbooks(workbk1).Activate
Else: End If

'''''''I want the macro to restart here when the error occurs!!!!!!!!!

'start second account
Range("acct2").Select

If ActiveCell.Value <> "" Then
Workbooks.Open FileName:=path2
Worksheets(Sht2).Select
Range("F11").Select
Do Until ActiveCell.Value = acct2
ActiveCell.Offset(0, 1).Select
Loop
ActiveCell.Offset(100, 0).Select
Do Until ActiveCell <> ""
ActiveCell.Offset(-1, 0).Select
Loop
Selection.Copy
'close the workbook

Workbooks(workbk1).Activate
ActiveCell.Offset(0, 3).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
ActiveCell.Offset(1, -3).Select
Windows("Current year.xls").Activate
ActiveWorkbook.Close
Workbooks(workbk1).Activate
Else: End If

'start third account
If ActiveCell.Value <> "" Then
Workbooks.Open FileName:=path3
Worksheets(Sht1).Select
Range("F11").Select
Do Until ActiveCell.Value = acct3
ActiveCell.Offset(0, 1).Select
Loop
ActiveCell.Offset(100, 0).Select
Do Until ActiveCell <> ""
ActiveCell.Offset(-1, 0).Select
Loop
Selection.Copy
'close the workbook

Workbooks(workbk1).Activate
ActiveCell.Offset(0, 3).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
ActiveCell.Offset(1, -3).Select
Windows("Current year.xls").Activate
ActiveWorkbook.Close
Workbooks(workbk1).Activate
Else: End If


'start fourth account
If ActiveCell.Value <> "" Then
Workbooks.Open FileName:=path4
Worksheets(Sht1).Select
Range("F11").Select
Do Until ActiveCell.Value = acct4
ActiveCell.Offset(0, 1).Select
Loop
ActiveCell.Offset(100, 0).Select
Do Until ActiveCell <> ""
ActiveCell.Offset(-1, 0).Select
Loop
Selection.Copy
'close the workbook
Workbooks(workbk1).Activate
ActiveCell.Offset(0, 3).Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
ActiveCell.Offset(1, -3).Select
Windows("Current year.xls").Activate
ActiveWorkbook.Close
Workbooks(workbk1).Activate
Else: End If
'paste the prev balances to the Purchase order

Sheets("Matrix").Select
Range("G4:G7").Select
Selection.Copy
Sheets("PO").Select
ActiveWindow.ScrollRow = 22
ActiveWindow.ScrollRow = 31
ActiveWindow.SmallScroll Down:=3
Range("G49").Select
Selection.PasteSpecial Paste:=xlValues, Operation:=xlNone, SkipBlanks:= _
False, Transpose:=False
Range("F49").Select

Sheets("Matrix").Select
ActiveWindow.SelectedSheets.Visible = False
Sheets("PO").Select
End Sub

__________________
Russell
ermccarthy is offline   Reply With Quote
Old May 15th, 2002, 09:26 AM   #2
kkknie
Board Regular
 
Join Date: Apr 2002
Location: Greenwood, SC
Posts: 677
Default

Somewhere near the beginning of your code place this line:

On Error Goto ErrHnd

Where you want the code to return place this line:

ResumePlace:

Right before the End Sub line, place this code

Exit Sub

ErrHnd:

msgbox "Please check your account number"
resume ResumePlace

End Sub

You may want to enhance the ErrHnd section by checking for the correct error number (i.e. if err.Number = 9 then...)

K
kkknie is offline   Reply With Quote
Old May 15th, 2002, 09:42 AM   #3
ermccarthy
Board Regular
 
Join Date: Feb 2002
Location: San Antonio, TX
Posts: 186
Default

GREAT!!!!!!!!!!!!!

But I have one more little item....
if you look at my code it is basically the same code running four times with different values (acct1, acct2, etc). therefore if the wrong account number is inputted on the 1st account, it works, but if it is on the second account, it jumps it back to the ResumePlace: (back in the first part of the code)...any ideas short of breaking the code up into 4 different routines and running them consectively........but if this is the only way, I am not opposed to it


__________________
Russell
ermccarthy 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 12:12 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