Live Chat & Podcast at 1:00PM Eastern on Sunday!
There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
Search
Business Applications
Tag Cloud
access acer asus bios bsod computer crash desktop driver drivers error ethernet excel freeze gaming hard drive hardware hdmi internet laptop malware memory modem monitor motherboard network printer problem ram registry router security slow software sound toshiba trojan ubuntu 11.10 uninstall usb video virus vista wifi windows windows 7 windows 7 32 bit windows 7 64 bit windows xp wireless
Search
Search for:
Tech Support Guy Forums > Software & Hardware > Business Applications >
Excel Macro that copies whole rows

Reply  
Thread Tools
vba_newbie's Avatar
Computer Specs
Junior Member with 5 posts.
 
Join Date: Aug 2009
Experience: Beginner
12-Aug-2009, 01:17 PM #1
Exclamation Excel Macro that copies whole rows
Hello

I am a total vba beginer and really need some help. I just can't get my head round the following. Any help would be so very much appreciated.

This is what I am trying to do-

I have a sheet with many columns of info. This info is basically a listing of the fruits that Shop A, B and C sell and the different prices and other stats. I want to copy across the rows of info for each shop, sort by fruit but exclude some of the stats.

example,

If column 2 equals shop A and column 12 equals pears, then copy the row of info into the new sheet but only the info in columns 2,5,8,12 and 18. I then want a blank row
Then I want if column 2 equals Shop A and column 12 equals apples, then copy the row of info into the new sheet but only in the info in columns 2,5,8,12 and 18.
Then I want a blank row
Then I want if column 2 equals Shop A and column 12 equals peaches, then copy the row of info into the new sheet but only in the info in columns 2,5,8,12 and 18.
Then a blank row and repeat process for Shop B and C

If Shop A/B/C has no apples/peaches/pears then nothing will be returned

After each shop's fruit has been listed I want to total up the prices (column 18 in the orginal sheet)

And then I want a grand total of Shop A+ shop B +Shop C as the last line.


I hope I am clear

Thanks so much in advance
slurpee55's Avatar
Computer Specs
Distinguished Member with 7,837 posts.
 
Join Date: Oct 2004
Location: Southwest Iowa....
Experience: Currently stupid...
12-Aug-2009, 04:03 PM #2
A), this sounds a lot like homework...
B), it sounds like an absolutely horrible layout of the data. But, if you are essentially wanting a transposed sorting of the data from columns to rows, I would first get rid of the extra spaces between the stores and move the other data to another worksheet, copy the data going down (for say, apples) and paste special, transpose the data to the row underneath the stores. Then you can add in the columns you took out.
__________________
Iowa? I could have sworn this was heaven.
Well, I think I can answer this question most successfully in mime.
My theme song... | Affero - rate me!
vba_newbie's Avatar
Computer Specs
Junior Member with 5 posts.
 
Join Date: Aug 2009
Experience: Beginner
13-Aug-2009, 04:26 AM #3
Hi Slurpee

Thanks for your post. I'm not sure I explained myself very clearly... I have come up with a basic marco for this already ( see below), but dont know how to do the following -

1. I dont want to copy the entire row - I want to exclude some cells. eg, if I am copying row 2 from the sheet, I dont want to include cells d2 and e2 in my new sheet.
2. I dont know how to put a blank row in between each listing of a shop's fruit.....

here is what I have so far...... (it gives me the listing I want, but I need blank line in between and also I dont know how to exclude some of the columns from the row that I am copying across)

Sub Macro1()
Application.ScreenUpdating = False
Sheets("Data").Select
Lastrow = Range("A65536").End(xlUp).Row

For i = 1 To Lastrow
Sheets("Data").Select

If Cells(i, 2) = "SHOP A" _
And Cells(i, 12) = "APPLES" Then
Rows(i & ":" & i).Select
Selection.Copy
Sheets("Sheet3").Select
PasteRow = Range("F65536").End(xlUp).Offset(1, 0).Row
Rows(PasteRow & ":" & PasteRow).Select
Selection.Insert Shift:=xlDown
End If

Next i

Range("A1").Select
Application.ScreenUpdating = True
Application.ScreenUpdating = False
Sheets("Data").Select
Lastrow = Range("A65536").End(xlUp).Row

For i = 1 To Lastrow
Sheets("Data").Select

If Cells(i, 2) = "SHOP A" _
And Cells(i, 12) = "PEARS" Then
Rows(i & ":" & i).Select
Selection.Copy
Sheets("Sheet3").Select
PasteRow = Range("F65536").End(xlUp).Offset(1, 0).Row
Rows(PasteRow & ":" & PasteRow).Select
Selection.Insert Shift:=xlDown
End If

Next i

Range("A1").Select
Application.ScreenUpdating = True
End Sub

Thanks v much in advance !!
vba_newbie's Avatar
Computer Specs
Junior Member with 5 posts.
 
Join Date: Aug 2009
Experience: Beginner
13-Aug-2009, 05:21 AM #4
Hi Slurpie... so I just had a brain wave, and thought maybe it would be easier to take my original spreadsheet and first delete all the irrelevent coloumns. I used the macro recorder to do this and it seems to work (see below). So I thought I could

1. run the macro to copy the data in 'Data', exlcluding the coloumns I dont want into 'Sheet 1'
2. then run the second macro that I wrote in the previous post to pull in the data I need from 'Sheet 1' to another sheet, 'Sheet 2'.
This probably isnt the most efficent way to do things, but hey !

... and that leaves me needing to alter my code in step 2. to leave a blank line inbwteen Shop A's Apples, Shop A's Pears etc. I also wanted to total up at the side, the price totals....(see example below)

SHOP A STAT 1 STAT 2 APPLES 2.30
SHOP A STAT 1 STAT 2 APPLES 2.30
4.60
SHOP A STAT 1 STAT 2 PEARS 3.40
SHOP A STAT 1 STAT 2 PEARS 4.10
7.50

This is the macro recorder code for copying data and deleting some columns -
Range("A1").Select
Sheets("Data").Select
Cells.Select
Selection.Copy
Sheets("Sheet1").Select
ActiveSheet.Paste
Columns("A:A").Select
Application.CutCopyMode = False
Selection.Delete Shift:=xlToLeft
Columns("B:C").Select
Selection.Delete Shift:=xlToLeft
Columns("C").Select
Selection.Delete Shift:=xlToLeft
Columns("E:F").Select
Selection.Delete Shift:=xlToLeft
ActiveWindow.SmallScroll ToRight:=5
Columns("G:H").Select
Selection.Delete Shift:=xlToLeft
Columns("H:H").Select
Selection.Delete Shift:=xlToLeft
Selection.Delete Shift:=xlToLeft
ActiveWindow.SmallScroll ToRight:=3
Columns("J:K").Select
Selection.Delete Shift:=xlToLeft
Columns("K:M").Select
Selection.Delete Shift:=xlToLeft
Columns("L:N").Select
Selection.Delete Shift:=xlToLeft
Columns("L:AB").Select
Selection.Delete Shift:=xlToLeft
ActiveWindow.ScrollColumn = 1
ActiveWindow.SmallScroll Down:=9

This is the code I need to alter for the blank lines and totals -

Sub Macro1()
Application.ScreenUpdating = False
Sheets("Data").Select
Lastrow = Range("A65536").End(xlUp).Row

For i = 1 To Lastrow
Sheets("Data").Select

If Cells(i, 2) = "SHOP A" _
And Cells(i, 12) = "APPLES" Then
Rows(i & ":" & i).Select
Selection.Copy
Sheets("Sheet3").Select
PasteRow = Range("F65536").End(xlUp).Offset(1, 0).Row
Rows(PasteRow & ":" & PasteRow).Select
Selection.Insert Shift:=xlDown
End If

Next i

Range("A1").Select
Application.ScreenUpdating = True
Application.ScreenUpdating = False
Sheets("Data").Select
Lastrow = Range("A65536").End(xlUp).Row

For i = 1 To Lastrow
Sheets("Data").Select

If Cells(i, 2) = "SHOP A" _
And Cells(i, 12) = "PEARS" Then
Rows(i & ":" & i).Select
Selection.Copy
Sheets("Sheet3").Select
PasteRow = Range("F65536").End(xlUp).Offset(1, 0).Row
Rows(PasteRow & ":" & PasteRow).Select
Selection.Insert Shift:=xlDown
End If

Next i

Range("A1").Select
Application.ScreenUpdating = True
End Sub



THANKS IN ADVANCE !!!!!
vba_newbie's Avatar
Computer Specs
Junior Member with 5 posts.
 
Join Date: Aug 2009
Experience: Beginner
13-Aug-2009, 05:24 AM #5
The totals came out all wrong in my previous post...

They should have been in the line below, but to the right of the last price
So if the last price was in D4, the total of all those prices above would show in E5


Sorry for the confusion !
computerman29642's Avatar
Computer Specs
Senior Member with 2,794 posts.
 
Join Date: Dec 2007
Location: HERE OR THERE?!?!?!
Experience: Always Learning!
13-Aug-2009, 03:53 PM #6
I believe the best thing would be to post a sample file (remove any sensitive data with dummy data). Then we all can see exactly what you are seeing and explaining.
Reply

Tags
data presentation, excel, urgent help, vba

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.

Search Tech Support Guy

Find the solution to your
computer problem!




Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who want to help you solve your computer problems. See our Welcome Guide to get started.
Thread Tools



Facebook Facebook Twitter Twitter TechGuy.tv TechGuy.tv Mobile TSG Mobile
You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 11:08 PM.
Copyright © 1996 - 2011 TechGuy, Inc. All rights reserved.

Powered by Cermak Technologies, Inc.