Solved

Automatically "cut and paste" data from one Excel workbook to multiple workbooks

Posted on 2014-10-31
13
141 Views
Last Modified: 2014-11-12
Is there an easy, automatic way - rather than manual cut and paste - to copy data from an Excel workbook into separate, individual workbooks based on one of the columns of data? So if I have several columns of data and one of the columns is an agent number, I want to take all the rows for a particular agent number and create a separate workbook only with that agent's information and then save the file using the agent's first and last name, which are in another column. Is this possible? If so, how? If it needs to be done with Visual Basic coding, does anyone have that coding or willing to put it together for me? I am not a programmer and wouldn't know where to start with this. If you need a sample of my data, I can provide that. Thanks for any help you can provide.
0
Comment
Question by:esu4236
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 5
13 Comments
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40416385
Sample data always good.

It would need to be done in VBA.
0
 

Author Comment

by:esu4236
ID: 40416667
Okay will do on Monday. Thx.
0
 

Author Comment

by:esu4236
ID: 40419368
Attached is a sample file. We want a separate file based on each Rep #, Column A. And we would ultimately like to name each file by the Rep Name, Column B.
Kevin-Sample-Data.xlsx
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40419390
Sub GenerateSpreadsheet()

    Dim myValue As String
    myValue = InputBox("Make sure you have saved this workbook first!" & Chr(13) & Chr(13) & "Which rep number do you want?")
    If myValue <> "" Then
        introw = 2
        Do Until Cells(introw, 1) = ""
            Select Case Cells(introw, 1)
            Case myValue
                introw = introw + 1
            Case Else
                Rows(introw).Delete
            End Select
        Loop
        If Cells(2, 2) = "" Then
            MsgBox "No data!"
        Else
            Application.DisplayAlerts = False
            ActiveWorkbook.SaveAs Filename:="E:\Myfolder\" & Cells(2, 1) & " - " & Cells(2, 2) & ".xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
            Application.DisplayAlerts = True
        End If
        ' ActiveWindow.Close - if you want it to close.
    Else
        ' Abort
    End If
End Sub

Open in new window

0
 

Author Comment

by:esu4236
ID: 40425132
Philip Burton, your code works great!!!! One question though - is there any way to have it keep the original document open, so I don't have to keep opening it every time? The reason I want to do this is that I have to run all these reports at the same time, where I'm having it save the individual person's records in their own file. I want to be able to do one right after the other. I see the line in the macro that says to close the file, but it looks to me like it's commented out with the ' in front of it, but yet it's closing the original file. If I can get that working, then I think we're set!! Thanks for your help on this - greatly appreciate it.
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40425736
Change line 22 to:

        ActiveWindow.Close ' This closes the new file.
    Workbooks.Open Filename:="E:\Myfolder\Kevin-Sample-Data.xlsx" ' This re-opens the original file.

Open in new window


You will need to change the Filename to the path and filename of the original file.
0
 

Author Comment

by:esu4236
ID: 40427355
For some reason, it's not re-opening the original file. It's making the individual files just fine, but I have to keep re-opening the original file. Below is the code I have so far. What am I doing wrong?

Also, what happens if a file already exists for one of the reps? It doesn't look like it will overwrite that file with the same name (1329 - Lyle). Do I need to make sure these separate files are maybe moved to another folder before the macro is run again for the same reps?

Sub Separate()

    Dim myValue As String
    myValue = InputBox("Make sure you have saved this workbook first!" & Chr(13) & Chr(13) & "Which rep number do you want?")
    If myValue <> "" Then
        introw = 2
        Do Until Cells(introw, 1) = ""
            Select Case Cells(introw, 1)
            Case myValue
                introw = introw + 1
            Case Else
                Rows(introw).Delete
            End Select
        Loop
        If Cells(2, 2) = "" Then
            MsgBox "No data!"
        Else
            Application.DisplayAlerts = False
            ActiveWorkbook.SaveAs Filename:="G:\esu4236\excel\" & Cells(2, 1) & " - " & Cells(2, 2) & ".xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
            Application.DisplayAlerts = True
        End If
            ActiveWindow.Close
            Workbooks.Open Filename:="G:\esu4236\excel\Kevin Sample Data - macro enabled.xlsm"
    Else
        ' Abort
    End If
End Sub
0
 

Author Comment

by:esu4236
ID: 40432691
Phillip Burton - any suggestions on my last comment?
0
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 500 total points
ID: 40432710
Sorry - I didn't see your last message.

How about this:

Sub Separate()

    Dim myValue As String
    Set myWorkbook = ActiveWorkbook
    myValue = InputBox("Make sure you have saved this workbook first!" & Chr(13) & Chr(13) & "Which rep number do you want?")
    If myValue <> "" Then
        introw = 2
        Do Until Cells(introw, 1) = ""
            Select Case Cells(introw, 1)
            Case myValue
                introw = introw + 1
            Case Else
                Rows(introw).Delete
            End Select
        Loop
        If Cells(2, 2) = "" Then
            MsgBox "No data!"
        Else
            Application.DisplayAlerts = False
            ActiveWorkbook.SaveAs Filename:="G:\esu4236\excel\" & Cells(2, 1) & " - " & Cells(2, 2) & ".xlsx", FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False
            Application.DisplayAlerts = True
        End If
            Workbooks.Open Filename:="G:\esu4236\excel\Kevin Sample Data - macro enabled.xlsm"
	    myWorkbook.close
    Else
        ' Abort
    End If
End Sub

Open in new window

0
 

Author Comment

by:esu4236
ID: 40435816
I think this is going to work. I need to get together with my user and test it out, so I'm going to leave this open right now. It worked beautifully for me - AWESOME!!!!  Thanks for all your help - so much appreciated. I'll hopefully have a final answer by the end of this week so I can resolve this question. Thanks again!!!!
0
 

Author Comment

by:esu4236
ID: 40437853
Okay - the end user has come back with one more request. Is there a way to have this macro run so that it is creating a separate file for each Rep # without any user intervention? In other words, as soon as I click to run the macro, I want it to create a separate file for each Rep #, no matter if there are 50 reps, 100 reps, or 300 reps without inputting each rep number individually. Is that possible? I know it means a change in the coding since we wouldn't want it to prompt us anymore. Let me know. THANKS FOR ALL YOUR HELP!!!!
0
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40437885
Yes, it's possible, but I suggest you post that as a separate question.
0
 

Author Closing Comment

by:esu4236
ID: 40437940
Thanks for all the help!! I've posted my next question as a separate one.
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Excel - Sum Formula 3 81
Transposing this formula ... 5 23
Modify to work without control shift enter 9 36
Excel Data Validation Help 9 49
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
How to get Spreadsheet Compare 2016 working with the 64 bit version of Office 2016
Viewers will learn how to apply various conditional formatting in Excel 2013.
Viewers will learn the basics of printing in Excel 2013 and how to adjust some common settings.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question