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 12th, 2002, 10:57 AM   #1
IanMc
New Member
 
Join Date: Mar 2002
Posts: 7
Default

Dear Excelsperts,

I haven't mucked about with Excel in quite a while now and have been asked to do a module in Excel 2000.

When given a workbook (tej-exit.xls) which has one worksheet of thousands of rows with columns from A to AS, i would like to copy all rows which have a zero in column N to a new worksheet.

Is this difficult?
Would i have to have one workbook with the code module in, load up the tej-exit.xls file ?

Thanks for any help

IanMc
IanMc is offline   Reply With Quote
Old Mar 12th, 2002, 07:51 PM   #2
Paul B
Board Regular
 
Join Date: Feb 2002
Location: Georgia USA
Posts: 544
Default

Try this
This Marco was modified to fit your needs, from one written by Barrie Davidson, you will need to change the range to fit, right now it is looking at Range("A1:AS3000"). Put this in a module in your workbook (tej-exit.xls). As always, make a backup before trying anything new
HTH
Sub Extract_Data()
'this macro assumes that your first row of data is a header row.
'will copy a row from one worksheet, to another blank workbook
'IF there is a 0 in column N
'Variables used by the macro
Application.ScreenUpdating = False
Dim FilterCriteria
Dim CurrentFileName As String
Dim NewFileName As String

'Get the current file's name
CurrentFileName = ActiveWorkbook.Name
'Select Range
'(note you can change this to meet your requirements)
Range("A1:AS3000").Select
'Apply Autofilter
Selection.AutoFilter
FilterCriteria = 0
'NOTE - this filter is on column N (field:=14), to change
'to a different column you need to change the field number
Selection.AutoFilter field:=14, Criteria1:=FilterCriteria
'Select the visible cells (the filtered data)
Selection.SpecialCells(xlCellTypeVisible).Select
'Copy the cells
Selection.Copy
'Open a new file
Workbooks.Add Template:="Workbook"
'Get this file's name
NewFileName = ActiveWorkbook.Name
'Make sure you are in cell A1
Range("A1").Select
'Paste the copied cells
ActiveSheet.Paste
'Clear the clipboard contents
Application.CutCopyMode = False
'Go back to the original file
Workbooks(CurrentFileName).Activate
'Clear the autofilter
Selection.AutoFilter field:=1
'Take the Autofilter off
Selection.AutoFilter
'Go to A1
Range("A1").Select
Application.ScreenUpdating = True
End Sub


Paul B is offline   Reply With Quote
Old Mar 13th, 2002, 02:23 AM   #3
IanMc
New Member
 
Join Date: Mar 2002
Posts: 7
Default

Thanks Paul B !!!

I'll give it a whirl and let you know how i get on.

Is it also possible to have it in another blank workbook like 'code.xls' and then to 'import' the worksheet from the original?

Is it possible to get 'it' to do the importing?

Thanks muchly for your swift and knowledgeable help

IanMc
IanMc is offline   Reply With Quote
Old Mar 13th, 2002, 05:24 AM   #4
Paul B
Board Regular
 
Join Date: Feb 2002
Location: Georgia USA
Posts: 544
Default

you can put it your personal.xls workbook and it will be available in all workbooks
Paul B is offline   Reply With Quote
Old Mar 13th, 2002, 06:02 AM   #5
IanMc
New Member
 
Join Date: Mar 2002
Posts: 7
Default

Thanks again Paul,

What if i just want to add another worksheet with the results?

I've tried this so far:

Sub Extract_Data()
'this macro assumes that your first row of data is a header row.
'will copy a row from one worksheet, to another blank workbook
'IF there is a 0 in column N
'Variables used by the macro
Application.ScreenUpdating = False
Dim FilterCriteria
Dim CurrentFileName As String
Dim NewFileName As String
'Dim FirstSheet As Long

'FirstSheet = Sheets(ActiveWorkSheet)
'Get the current file's name
CurrentFileName = ActiveWorkbook.Name
'Select Range
'(note you can change this to meet your requirements)
Range("A1:AS3000").Select

'Apply Autofilter
Selection.AutoFilter
FilterCriteria = 0
'NOTE - this filter is on column N (field:=14), to change
'to a different column you need to change the field number
Selection.AutoFilter field:=14, Criteria1:=FilterCriteria
'Select the visible cells (the filtered data)
Selection.SpecialCells(xlCellTypeVisible).Select
'Copy the cells
Selection.Copy

'Clear the autofilter
'
Selection.AutoFilter field:=1
'Take the Autofilter off
Selection.AutoFilter

'Open a new worksheet 'file
Sheets.Add().Name = "Results"
Sheets("Results").Paste
'Get this file's name
'NewFileName = ActiveWorkbook.Name
'Make sure you are in cell A1
'ActiveSheet = "Results"
'Range("A1").Select
'Paste the copied cells
'ActiveSheet.Paste

'Go back to the original file
'Sheets(FirstSheet).Select

'Workbooks(CurrentFileName).Activate


'Clear the clipboard contents
Application.CutCopyMode = False
'Go to A1
Range("A1").Select
Application.ScreenUpdating = True
'Now make a worksheet with everything that is NOT zero in column N


UserForm1.Hide
End
End Sub


But it fails to paste the data,,, i don't have the VBA help files installed for my Excel yet (i've put in a request) so i'm a bit lost.

Thanks

Ian
IanMc is offline   Reply With Quote
Old Mar 13th, 2002, 07:03 AM   #6
Paul B
Board Regular
 
Join Date: Feb 2002
Location: Georgia USA
Posts: 544
Default

'this will put your data in a new worksheet
'it will also Auto fits text in Columns on the new sheet
Sub Extract_Data_Two()
Application.ScreenUpdating = False
Dim FilterCriteria
Dim CurrentsheetName As String
Dim NewFileName As String
'Get the current sheets's name
CurrentsheetName = ActiveSheet.Name
'Select the range
'(note you can change this to meet your requirements)
Range("A1:AS3000").Select
'Apply Autofilter
Selection.AutoFilter
'Get the filter's criteria from the user
FilterCriteria = 0
'Filter the data based on the user's input
'NOTE - this filter is on column N (field:=14), to change
'to a different column you need to change the field number
Selection.AutoFilter field:=14, Criteria1:=FilterCriteria
'Select the visible cells (the filtered data)
Selection.SpecialCells(xlCellTypeVisible).Select
'Copy the cells
Selection.Copy
Sheets.Add
'Make sure you are in cell A1
Range("A1").Select
'Paste the copied cells
ActiveSheet.Paste
'Clear the clipboard contents
Application.CutCopyMode = False
' Auto fits text in Columns
Cells.Select
Selection.Columns.AutoFit
Range("A1").Select
'Go back to the original sheet
Worksheets(CurrentsheetName).Activate
'Clear the autofilter
Selection.AutoFilter field:=1
'Take the Autofilter off
Selection.AutoFilter
'Go to A1
Range("A1").Select
Application.ScreenUpdating = True
End Sub

Paul B is offline   Reply With Quote
Old Mar 13th, 2002, 07:27 AM   #7
IanMc
New Member
 
Join Date: Mar 2002
Posts: 7
Default

Thanks Paul !! i shall give that a try,
Meanwhile thanks to your help i've managed to get it exactly as required in this instance.

The final subroutine (below) does the job.
The routine is activated by a button on the toolbar, (well actually this activates a nicely drawn UserForm with a picture, label and button on it, the button actually activates the subroutine)
The user first loads this Excel program with this in it and assigns a toolbar button to open the UserForm macro.

They then choose 'Open' and open the tej-exit.xls data file,
when the file opens all of the data apart from cell A1 is selected but this doesn't seem to matter,,, when they click on the toolbar button (which is a smiley) the UserForm opens and tells them what to do, they click on the button and the working is:
All rown with column N = zero are highlighted on the worksheet called tej-exit plus there has appeared a new worksheet called 'Results' which has only the rows in it with column N = zero
(it doesn't need for the first row to be headers or anything as it turns the filter back off)

The subroutine is here below, thank you very much indeed for your excellent help and i shall have a play with your latest version.

IanMc


Sub Extract_Data()
'this macro assumes that your first row of data is a header row.
'will copy a row from one worksheet, to another blank workbook
'IF there is a 0 in column N
'Variables used by the macro
Application.ScreenUpdating = False
Dim FilterCriteria
Dim CurrentFileName As String
Dim NewFileName As String

Set a = ActiveSheet
'Select Range
'(note you can change this to meet your requirements)
Range("A1:AS4000").Select
'Apply Autofilter
Selection.AutoFilter
FilterCriteria = 0
'NOTE - this filter is on column N (field:=14), to change
'to a different column you need to change the field number
Selection.AutoFilter field:=14, Criteria1:=FilterCriteria
'Select the visible cells (the filtered data)
Selection.SpecialCells(xlCellTypeVisible).Select
'Copy the cells
Selection.Copy
'Open a new file
'Workbooks.Add Template:="Workbook"
'Get this file's name
'NewFileName = ActiveWorkbook.Name
Sheets.Add().Name = "Results"
Set b = ActiveSheet
'Make sure you are in cell A1
Range("A1").Select
'Paste the copied cells
ActiveSheet.Paste
Range("A1").Select 'unselect everything
'Clear the clipboard contents
Application.CutCopyMode = False
'Go back to the original file
'Workbooks(CurrentFileName).Activate
a.Select
Selection.AutoFilter field:=14, Criteria1:=FilterCriteria
Selection.SpecialCells(xlCellTypeVisible).Select
'Clear the autofilter
'Selection.AutoFilter field:=1
'Take the Autofilter off
Selection.AutoFilter
'Go to A1
'Range("A1").Select
Application.ScreenUpdating = True
End
End Sub
IanMc is offline   Reply With Quote
Old Nov 20th, 2008, 09:29 PM   #8
mlangen
Board Regular
 
Join Date: Nov 2008
Location: Seattle, WA
Posts: 246
Default Re: Copy rows to new worksheet on condition

When I run this it only copies the header row into a newly created results worksheet?

Once I get this working it is exactly what I was looking for...
mlangen is offline   Reply With Quote
Old Nov 21st, 2008, 01:09 PM   #9
mlangen
Board Regular
 
Join Date: Nov 2008
Location: Seattle, WA
Posts: 246
Default Re: Copy rows to new worksheet on condition

Got it working
mlangen is offline   Reply With Quote
Old Jun 27th, 2009, 11:26 PM   #10
sadi
Board Regular
 
sadi's Avatar
 
Join Date: Jun 2009
Posts: 76
Default Re: Copy rows to new worksheet on condition

Thanks Paul,
but how can i protect my work book with morethan 5 sheet form another user. I can't run macro in protected workbook.

BR
sadi
sadi 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 05:46 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