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 Apr 23rd, 2002, 04:48 PM   #1
Todd_M
Board Regular
 
Join Date: Feb 2002
Posts: 117
Default

Hi

The code below finds a date using the caledar control on sheet1 , then finds the first set of 7 cells on sheet1 and pastes that info on sheet3.range("a:h"). Works great thanks to Jay's help. Now If I want to paste the next set of 7 cells from sheet1 to sheet3 then I have to rewrite some of the code so that the next line on sheet3(row2) contains the next set of info. This works great also!


Private Sub CommandButton1_Click()
Sheets("sheet1").Activate
For Each a In Worksheets("sheet1").Range("d2:d10000")
If a.Value = ListBox1.Value Then
a.Offset(0, 0).Activate

Dim lastrow As Long, rng As Range

lastrow = Sheets("Sheet3").Cells(Rows.Count, 1).End(xlUp).Row
With ActiveSheet
Set rng = ActiveCell.Offset(0, 7).Resize(,
End With
rng.Copy Sheets("Sheet3").Cells(lastrow + 1, 1)


lastrow = Sheets("Sheet3").Cells(Rows.Count, 1).End(xlUp).Row
With ActiveSheet
Set rng = ActiveCell.Offset(0, 15).Resize(,
End With
rng.Copy Sheets("Sheet3").Cells(lastrow + 1, 1)




End If
Next a

Sheets("sheet3").Activate


End Sub



Heres the problem- if I have 29 sets of info to be pasted from sheet1 to sheet3, do I have to rewrite this code 29 times or is there an easier way to do this??
Todd_M is offline   Reply With Quote
Old Apr 23rd, 2002, 04:58 PM   #2
Jay Petrulis
MrExcel MVP
 
Jay Petrulis's Avatar
 
Join Date: Mar 2002
Location: Chicago, IL USA
Posts: 2,042
Default

Quote:
On 2002-04-23 15:48, Todd_M wrote:
->snip<-

Heres the problem- if I have 29 sets of info to be pasted from sheet1 to sheet3, do I have to rewrite this code 29 times or is there an easier way to do this??
No, never rewrite the code 29 times. Put the pasting code in another loop within the "For each a" loop....

For x = 7 to 239 step 8

lastrow = Sheets("Sheet3").Cells(Rows.Count, 1).End(xlUp).Row
With ActiveSheet
Set rng = ActiveCell.Offset(0, x).Resize(,8)
End With
rng.Copy Sheets("Sheet3").Cells(lastrow + 1, 1)

Next x
Jay Petrulis is offline   Reply With Quote
Old Apr 23rd, 2002, 05:26 PM   #3
Todd_M
Board Regular
 
Join Date: Feb 2002
Posts: 117
Default

Jay you are a GOD-Thankyou so much, now there two small ajustments to add on to make this work.

Private Sub CommandButton1_Click()
Sheets("sheet1").Activate
For Each a In Worksheets("sheet1").Range("d2:d10000")
If a.Value = ListBox1.Value Then
a.Offset(0, 0).Activate

Dim lastrow As Long, rng As Range
For x = 7 To 239 Step 8
lastrow = Sheets("Sheet3").Cells(Rows.Count, 1).End(xlUp).Row
With ActiveSheet
Set rng = ActiveCell.Offset(0, x).Resize(,
End With
rng.Copy Sheets("Sheet3").Cells(lastrow + 1, 1)

Next x
End If
Next a


First: The code reads column d in sheet1 for a date that was selected from a listbox. Then it move to the 7th cell and pastes on sheet3 to the next empty cell. Now lets say if I need to also show the date in the first column of sheet3 just to the left of each set of pastes from sheet1. I could also show it on the right of each set of pastes on sheet3(column I), but I would perfere to read a date first then the set of pastes. I know the date is listed as offset(0, 0), but im not sure how to aplly it to this wonderful code!

Secound: The code not only has to read 29 times across, but between 28 to 31 rows down depending on how many days are in the month that is selected for viewing.

I hope im not asking to much, thankyou so much.
Todd_M is offline   Reply With Quote
Old Apr 23rd, 2002, 06:09 PM   #4
Jay Petrulis
MrExcel MVP
 
Jay Petrulis's Avatar
 
Join Date: Mar 2002
Location: Chicago, IL USA
Posts: 2,042
Default

Quote:
On 2002-04-23 16:26, Todd_M wrote:

...snip...

First: The code reads column d in sheet1 for a date that was selected from a listbox. Then it move to the 7th cell and pastes on sheet3 to the next empty cell. Now lets say if I need to also show the date in the first column of sheet3 just to the left of each set of pastes from sheet1. I could also show it on the right of each set of pastes on sheet3(column I), but I would perfere to read a date first then the set of pastes. I know the date is listed as offset(0, 0), but im not sure how to aplly it to this wonderful code!

Secound: The code not only has to read 29 times across, but between 28 to 31 rows down depending on how many days are in the month that is selected for viewing.

I hope im not asking to much, thankyou so much.
For the first question, will the following work for you? It should place the date in column A, with the pasted data to Column B and beyond.

Private Sub CommandButton1_Click()
Sheets("sheet1").Activate
For Each a In Worksheets("sheet1").Range("d2:d10000")
If a.Value = ListBox1.Value Then
a.Offset(0, 0).Activate

Dim lastrow As Long, rng As Range
For x = 7 To 239 Step 8
lastrow = Sheets("Sheet3").Cells(Rows.Count, 1).End(xlUp).Row
With ActiveSheet
Set rng = ActiveCell.Offset(0, x).Resize(,8)
End With
Sheets("Sheet3").cells(lastrow+1,1) = a.value
rng.Copy Sheets("Sheet3").Cells(lastrow + 1, 2)

Next x
End If
Next a

I don't understand your second question. You are selecting a date and copying the data. Is the date just the first of any particular month? Do you then have to load the rest of the month's values? In what order?

This shouldn't be difficult, either. You may have to set a reference to the analysis toolpak in your VBE (Tools>References>atpvbaen) and then you can use
=DAY(EOMONTH(a.value)) to find the number of days to throw into a third loop. The is easier than writing the non-ATP function.

This multiple looping is quite slow, I'm sure.

HTH,
Jay
Jay Petrulis 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 06:58 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