Solved

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

Posted on 2014-10-31
13
133 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
  • 8
  • 5
13 Comments
 
LVL 24

Expert Comment

by:Phillip Burton
Comment Utility
Sample data always good.

It would need to be done in VBA.
0
 

Author Comment

by:esu4236
Comment Utility
Okay will do on Monday. Thx.
0
 

Author Comment

by:esu4236
Comment Utility
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
 
LVL 24

Expert Comment

by:Phillip Burton
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Author Comment

by:esu4236
Comment Utility
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
Comment Utility
Phillip Burton - any suggestions on my last comment?
0
 
LVL 24

Accepted Solution

by:
Phillip Burton earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Yes, it's possible, but I suggest you post that as a separate question.
0
 

Author Closing Comment

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

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
In this article, I will show you HOW TO: Create your first Windows Virtual Machine on a VMware vSphere Hypervisor 6.5 (ESXi 6.5) Host Server, the Windows OS we will install is Windows Server 2016.
Viewers will learn how to create a PivotTable and make basic changes to it in Excel 2013.
Video by: Zack
Viewers will learn the basics of using Excel Tables, the benefits found with them, and some pitfalls.

762 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

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now