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 17th, 2002, 09:56 PM   #1
chookers
Board Regular
 
Join Date: Mar 2002
Posts: 115
Default

Hello there.

I've got some code here (done with some of your help) to find the last row on a worksheet. What I have been trying to do is to now select and copy rows 1 through to the "last row". And I can't bloody make it work! Normally you'd select Rows 1:25 or whatever, but I just can't work out where and how exactly to put it all together. I'm eventually going to paste it to another sheet and reformat it, which I can handle, but of course this one stupid bit which should be easy has me stumped. So can anyone tell me how (and where to put!) code which will select (and copy) rows 1:endof list. I am feeling quite inept at the moment. As specific as you can get will not go astray.

Here's what we have so far:

Sub FindLastRow()
Dim lastrow As String
Dim EndofList As String

If WorksheetFunction.CountA(Cells) > 0 Then
'Search for any entry, by searching back from Rows.
lastrow = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
End If
EndofList = lastrow
Range("A1").Select
ActiveCell.Offset(EndofList, 1).Range("A1").Select

End Sub

Thanking you!!!
chookers is offline   Reply With Quote
Old Mar 17th, 2002, 10:02 PM   #2
Dave Hawley
Banned
 
Join Date: Feb 2002
Posts: 1,582
Default

Hello Chookers

Try this

Sub FindLastRow()
Dim rlast As Range


If WorksheetFunction.CountA(Cells) > 0 Then
'Search for any entry, by searching back from Rows.
Set rlast = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
End If

Range("A1", rlast).Copy Destination:=Sheet5.Range("A1")
End

End Sub
Dave Hawley is offline   Reply With Quote
Old Mar 17th, 2002, 10:31 PM   #3
chookers
Board Regular
 
Join Date: Mar 2002
Posts: 115
Default

Thanks Dave. Yes that makes sense but when I try it it wants me to debug the last .row just before the End If. It says compile error - type mismatch. I'm actually on my way out the door to work tomorrow so will try some more tomorrow.

Cheers!
chookers is offline   Reply With Quote
Old Mar 17th, 2002, 10:42 PM   #4
Dave Hawley
Banned
 
Join Date: Feb 2002
Posts: 1,582
Default

Sorry Chookers, forgot to remove the Row Property from the Find Method


Sub FindLastRow()
Dim rlast As Range


If WorksheetFunction.CountA(Cells) > 0 Then
'Search for any entry, by searching back from Rows.
Set rlast = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
End If

Range("A1", rlast).Copy Destination:=Sheet3.Range("A1")
End

End Sub

Now Sheet5 is a CodeName of a Sheet. You may well have to alter this to suit.


Dave Hawley is offline   Reply With Quote
Old Mar 18th, 2002, 03:35 PM   #5
chookers
Board Regular
 
Join Date: Mar 2002
Posts: 115
Default

Hi Dave, I tried the new code but now it doesn't like the 'set rlast=' row. It says "Unable to get the find property of the range class".

am assuming for your "sheet5" I just substitute my sheet name instead? I actually tried that but it didn't like that either.

Give me a gun.

Any other thoughts? Thanks in advance, Lauren aka Chookers
chookers is offline   Reply With Quote
Old Mar 18th, 2002, 03:41 PM   #6
Cosmos75
Board Regular
 
Join Date: Feb 2002
Location: Tulsa, OK
Posts: 354
Default

Try this

Range("A1", Range("A65536").End(xlUp).Address).Copy

Hope this helps!
Cosmos75 is offline   Reply With Quote
Old Mar 18th, 2002, 03:49 PM   #7
chookers
Board Regular
 
Join Date: Mar 2002
Posts: 115
Default

Thanks very much, I will - have to go tutor some mad kids for a couple hours first though! Upon looking at that code some more, maybe the problem is something silly in the last line - The 'Sheet3' No matter what I put there it says variable not defined, what do I do for this? Do I have to do a Dim As String. God almighty, give me Access any day. I like this, but the frustration factor of learning under pressure is wicked!

Will check back and see if I can sort it out. By the way the sheet the code is meant to copy from is called "My Class", and the sheet I want it to past to is called "Activities".

Thank you!
chookers is offline   Reply With Quote
Old Mar 18th, 2002, 06:51 PM   #8
chookers
Board Regular
 
Join Date: Mar 2002
Posts: 115
Default

Cosmos tried your code at the bottom and it gives me the 'unable to get the find property of the range class' error for the Set rlast line of code.

Survived the kids, can't survive Excel code!

Help!
chookers is offline   Reply With Quote
Old Mar 18th, 2002, 07:56 PM   #9
Dave Hawley
Banned
 
Join Date: Feb 2002
Posts: 1,582
Default

Hi chookers

Sheet5 is a sheets CodeName, these can been seen in the "Project Explorer" they are the names NOT is brackets.This is the best means of referencing Worksheets as the CodeName cannot be changed.

You say you tried the new code but it doesn't like the:

"'set rlast=' row" There is no "row" in the NEW code???

Sub FindLastRow()
Dim rlast As Range


If WorksheetFunction.CountA(Cells) > 0 Then
'Search for any entry, by searching back from Rows.
Set rlast = Cells.Find(What:="*", After:=[A1], SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
End If

Range("A1", rlast).Copy Destination:=Sheet3.Range("A1")
End

End Sub


For "Sheet3" use either the CodeName of you sheet or replace it with it's Tab name, eg Sheets("Sheet1")

Are you running this via a CommandButton from the Control toolbox? If so set it's "TakeFocus*******" Propert to False.

You could use just:
Sub CopyAllData()
ActiveSheet.UsedRange.Copy Destination:=Sheet5.Range("A1")
End Sub

But this method is very unreliable,see:
http://www.ozgrid.com/VBA/ExcelRanges.htm


If it all get's too hard just email the Workbook.



Dave Hawley is offline   Reply With Quote
Old Mar 18th, 2002, 08:07 PM   #10
chookers
Board Regular
 
Join Date: Mar 2002
Posts: 115
Default

Hi Dave, thanks for that. Will try it in about an hour when I'm free again. I did take the "row" out of the code, I just meant row as in row, you know, not row as in row being code. Just row 3 of the code. Am going to run this just from a button on a toolbar.

Gotta run, will let you know what happens!

Cheers!
chookers 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 03:11 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