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 10th, 2002, 07:37 AM   #1
Guest
 
Posts: n/a
Default

I am using the Marco below, pieced together from previous code from this board, to move selected data to a new sheet. It works like I need except that if a user puts the column letter instead of the column number for the Auto Filter field, flagged in bold in the Marco, I get and error. How can this error be handled? I used on error Resume Next and it takes care of the error but instead of filtered data all data is put in the new sheet, thought there might be a better way, Thanks for your help

Option Explicit
Dim FilterCriteria
Dim CurrentsheetName As String
Dim NewFileName As String
Dim ws As Worksheet
Dim Flag As Boolean
Dim C

Public Sub Filter_Data()
Flag = False
Application.ScreenUpdating = False
Application.DisplayAlerts = False

CurrentsheetName = ActiveSheet.Name
FilterCriteria = UCase(InputBox("Enter Your Filter Criteria"))

'CHANGE THIS RANGE WHEN UPDATING THE RANGE
'THERE ARE two PLACES THAT NEED TO BE CHANGED!
For Each C In Range("a1:f752") '#1
If UCase(C.Value) = FilterCriteria Then Flag = True: Exit For
Next C
If Not Flag Or FilterCriteria = "" Then MsgBox "No Match To Your Criteria ": Exit Sub
'CHANGE THIS RANGE WHEN UPDATING THE RANGE
'THERE ARE two PLACES THAT NEED TO BE CHANGED!
Range("A1:f752").Select '#2
Selection.AutoFilter
'The error happens below if someone puts the column letter and not the number
Selection.AutoFilter field:=(InputBox("Enter Column NUMBER, A=1, B=2, C=3, D=4, E=5, F=6... THIS MUST BE A NUMBER AND NOT A LETTER")), Criteria1:=FilterCriteria
Selection.SpecialCells(xlCellTypeVisible).Select
Selection.Copy

With Worksheets.Add
.Range("A1").PasteSpecial Paste:=xlPasteAll
.Move after:=Worksheets(Worksheets.Count)
For Each ws In Worksheets
If ws.Name = FilterCriteria Then ws.Delete
Next ws
.Name = FilterCriteria
End With

Cells.Select
Selection.Columns.AutoFit
Range("A1").Select
Worksheets(CurrentsheetName).Activate
Selection.AutoFilter field:=1
Selection.AutoFilter
Range("B65536").Select
Selection.End(xlUp).Select
ActiveCell.Offset(1, 0).Select
Application.DisplayAlerts = True
Application.ScreenUpdating = True
End Sub
  Reply With Quote
Old Mar 10th, 2002, 07:47 AM   #2
dk
MrExcel MVP
 
Join Date: Feb 2002
Location: Sydney, Australia
Posts: 2,908
Default

You can use the inputbox of the Excel application rather than the VBA inputbox. The Excel one allow you to restrict the values the user can enter by setting it's type argument to one of the following or a combination (e.g. for text and number use 3)


0 A formula
1 A number
2 Text (a string)
4 A logical value (True or False)
8 A cell reference, as a Range object
16 An error value, such as #N/A
64 An array of values

Just change the line with the second inputbox to this:-

Selection.AutoFilter field:=(Application.InputBox(prompt:="Enter Column NUMBER, A=1, B=2, C=3, D=4, E=5, F=6... THIS MUST BE A NUMBER AND NOT A LETTER", Type:=1)), Criteria1:=FilterCriteria


HTH,
D
dk is offline   Reply With Quote
Old Mar 10th, 2002, 08:03 AM   #3
Guest
 
Posts: n/a
Default

Thanks DK, always get fast and useful info on this board
  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 02:13 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