Question

When I click on a sheet in excel 2007, I want to run a macro that copies certain data from another sheet into the current one

Asked by: eadam-uk

Hi all,

I have a complete list of data on sheet one, and then a breakdown of that data by certain categories on the following sheets. People can edit one sheet one, and rather than get them to copy and past that data into each of the breakdown spreadsheets, I would rather it did it automatically once the user clicked on one of the breakdown sheets.

The categories are 25, 30, 35, 40 and 45. and each row on the first sheet is assigned one of these categories in column C.

Once someone clicks on the "25" sheet, I want to copy all the 25 rows from sheet one, and paste them into the 25 sheet. The column names and structure are identical in all sheets.

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-10-06 at 15:26:13ID24790740
Tags

excel 2007

,

macro

,

copy and paste

Topic

Microsoft Excel Spreadsheet Software

Participating Experts
2
Points
500
Comments
12

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Excel: creating a macro to go to another sheet
    I have a spreadsheet with about 20 detail pages, and one summary page. I'd like to create one macro to use on the summary page that would read the name of the detail sheet and go there. The names of the detail sheets are stored in column A of the summary sheet. (I assign t...
  2. Macro for creating a sheet with totals ?
    Several times a day I need to repeat a certain action in Excel and I would realy like to automate this proces by using a macro ! :) Let me explain the situation: 1. I have one sheet with "totals" 2. After this sheet, I have a changing amount of other sheets (sheet...
  3. totals sheet
    I am setting up a spreadsheet where each week will be a new sheet. We need to have a seperate totals sheet where we add up all the weeks to create a running tally. The way i have mine setup is that cell B4 will add up all the weekly data for B11, C4 will add C11, B7 will d...
  4. A macro to compare 2 sheets
    Hi, I need to interogate data in a spreadsheet - and I think the best way of doing it will be to create a macro that automates the task ... the problem is, I wouldn't know where to start! This is the setup: I have 2 sheets, 1 called LIVE and 1 called TEST (they both contai...
  5. I have a macro that sorts 3 ranges in a spreadsheet and n…
    I have a spreadsheet (Sheet1) that has data grouped and totaled in 3 sections. I have a macro that sorts columns A-N above the last row in the section, by finding the cell that has the following text: "Summary Of OGM" or "Summary Of TS CRU" or "Sum...
  6. I need a way to copy a row from sheet 1 to sheet 3 of an …
    We use an excel spreadsheet that always changes depending on amount of orders, and I need a way to put specific row data from sheet 1 to sheet 3 if a 1 is placed in column J. I need to pull this specific data so I can use a macro to import sheet 3 into an access temp table. T...

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: GruebnerPosted on 2009-10-06 at 15:32:48ID: 25510625

Sorry I don't have time to find or write a snippet at the moment, but you could activate sheet1, autofilter on category column for the desired target sheet, then select copy / paste..

 

by: saurabh726Posted on 2009-10-06 at 15:34:07ID: 25510637

You can use the following code, when you run it will move the data from your main sheet which i have assumed is your activesheet as well to the various sheets which are present and incase if it doesnt, it will give you a message box for the same as well.

Saurabh...

Sub movecategory()
    Dim rng As Range, cell As Range, ws As Worksheet
    Set rng = Range("C2:C" & Cells(Cells.Rows.Count, "C").End(xlUp).Row)
 
    For Each cell In rng
        On Error Resume Next
        Set ws = cell.Value
 
        If Not ws Is Nothing Then
            cell.EntireRow.Copy ws.Range("C" & ws.Cells(Cells.Rows.Count, "c").End(xlUp).Row + 1)
            Else
            MsgBox " Sheet not present for category --> " & cell.Value & " mentioned in row number --> " & cell.Row
        End If
        Set ws = Nothing
    Next cell
 
    MsgBox "Done"
 
 
End Sub

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:

Select allOpen in new window

 

by: eadam-ukPosted on 2009-10-07 at 12:34:51ID: 25519068

Thanks saurabh726,

A couple of things, the master list spreadsheet is called "Master List" and the others are actually more wordy that just the value in C column. I was going to put the macro on each of the spreadsheets I wanted updating when they are activated by the user. So I click on spreadsheet called "Category 50 List" and it goes into the master spreadsheet and looks through column C for the every row that has the number "50" in it. So I guess using some variables at the top I could hard code which category it was looking for in the master list for that particular sheet.

I tried rewriting this macro to take that into account and it all went wrong. :-S i would copy my code but I think it might be useless. How you have used the if statement and ws confused me.

 

by: saurabh726Posted on 2009-10-07 at 12:50:18ID: 25519263

Ahh i did a error in that the ws will be this...

Set ws = sheets(cell.Value)

Now you can modify it to the names you have in actual.

Also since the data is in master list, change the following line to this or use the below code...

Saurabh...

Sub movecategory()
    Dim rng As Range, cell As Range, ws As Worksheet,ws1 as worksheet
set ws1=sheets("Master List")
 
    Set rng = ws1.Range("C2:C" & ws1.Cells(Cells.Rows.Count, "C").End(xlUp).Row)
 
    For Each cell In rng
        On Error Resume Next
        Set ws = sheets(cell.Value)
 
        If Not ws Is Nothing Then
            cell.EntireRow.Copy ws.Range("C" & ws.Cells(Cells.Rows.Count, "c").End(xlUp).Row + 1)
            Else
            MsgBox " Sheet not present for category --> " & cell.Value & " mentioned in row number --> " & cell.Row
        End If
        Set ws = Nothing
    Next cell
 
    MsgBox "Done"
 
 
End Sub
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:

Select allOpen in new window

 

by: eadam-ukPosted on 2009-10-07 at 13:09:15ID: 25519475

Hi Saurabh,

Is this loop still looking for each of the spreadsheets for each of the categories? I am happy for it to deal with just one category, lets say "50" for example, and the macro will be run when I click the 50 category sheet, so the macro only needs to know active sheet as the place to paste.

I think its still working through all the sheets and not finding them because they are not called the same as the cell value.

thanks.

 

by: eadam-ukPosted on 2009-10-07 at 14:12:24ID: 25520151

I had a go at changing it, but nothing appears in the 50 category spreadsheet.

Private Sub Worksheet_Activate()
 
Dim rng As Range, cell As Range, ws As Worksheet, ws1 As Worksheet
Set ws1 = Sheets("Master List")
 
    Set rng = ws1.Range("C2:C" & ws1.Cells(Cells.Rows.Count, "C").End(xlUp).Row)
 
    For Each cell In rng
        On Error Resume Next
        
        If cell = 50 Then
            cell.EntireRow.Copy ws.Range("C" & ws.Cells(Cells.Rows.Count, "c").End(xlUp).Row + 1)
            Else
            'MsgBox " Sheet not present for category --> " & cell.Value & " mentioned in row number --> " & cell.Row
        End If
        Set ws = Nothing
    Next cell
 
    MsgBox "Done"
 
 
End Sub

                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:

Select allOpen in new window

 

by: eadam-ukPosted on 2009-10-07 at 14:58:28ID: 25520529

Doesn't it need a paste commend somewhere in there?

 

by: saurabh726Posted on 2009-10-07 at 15:25:49ID: 25520763

Because you dont have the ws defined there,If you use my code which i gave to you does what you are looking for..

Saurabh...

 

by: eadam-ukPosted on 2009-10-07 at 15:27:43ID: 25520784

I did use your sheet but it keeps looking for spreadsheets by the name it finds in the cell, which it will never find.

 

by: GruebnerPosted on 2009-10-07 at 15:49:16ID: 25520979

Don't mean to add confusion, but this may be a possible - different approach.  The following can be attached to a button or whatever on each sheet.  You will need to massage it a bit if your sheet names are not category numbers and sheet 1 is not named main  - but I think you get the idea.

Sub CopyCats()

TargSheet = ActiveSheet.Name

Worksheets("Main").Select
Columns("C:C").Select

        Selection.AutoFilter
        Selection.AutoFilter Field:=1, Criteria1:=TargSheet
        Cells.Find(What:=TargSheet, after:=ActiveCell, LookIn:=xlFormulas, LookAt _
                :=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:= _
                False).Select
        Selection.CurrentRegion.Select
        Selection.Copy Destination:=Worksheets(TargSheet).Range("A1")
        ActiveSheet.AutoFilterMode = off

Worksheets(TargSheet).Select
Range("A1").Select

End Sub

 

by: eadam-ukPosted on 2009-10-08 at 09:41:34ID: 25527378

Ok, here is the code I got to work...

Private Sub Worksheet_Activate()
 
Dim rng As Range, cell As Range, masterList As Worksheet, actSheet As Worksheet, cat As String
 
    'Variable setting.
    Set masterList = Sheets("Master List")
    Set actSheet = activeSheet
    
    cat = "50" 'Change for each sheet!
    
    actSheet.Rows("2:200").ClearContents
    masterList.AutoFilterMode = False
        
    Set rng = masterList.Range("C2:C" & masterList.Cells(Cells.Rows.Count, "C").End(xlUp).Row)
 
    For Each cell In rng
        If cell = cat Then
            cell.EntireRow.Copy actSheet.Range("A" & actSheet.Cells(Cells.Rows.Count, "A").End(xlUp).Row + 1)
        End If
    Next cell
  
End Sub
                                              
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:

Select allOpen in new window

 

by: saurabh726Posted on 2009-10-08 at 15:03:35ID: 25530671

Okay so do you need any further help over this..?

Saurabh...

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...