Solved

Creating excel workbooks based on different values in a column

Posted on 2014-03-06
5
896 Views
Last Modified: 2014-03-13
Hi,

I want to write a vbscript to be able to create new workbooks based on different values in a column: e.g.


Data Type
aaa    A-B
aaa    A-B
bbb   B-C
rrr     C-D
eee   B-C


If i have data in an excel sheet like above, i need to have three excel workbooks created in a folder, such that:
1st workbook should be A-B.xls which has elements with type A-B, 2nd workbook should be B-C which has elements with type B-C, and the 3rd workbook should have elements with type C-D.

Please advice.

Thanks,
Aditya
0
Comment
Question by:adirisin
  • 2
  • 2
5 Comments
 
LVL 12

Expert Comment

by:Harry Lee
ID: 39911372
adirisin,

Try the following code. MAKE BACKUP OF YOUR ORIGINAL DATA SHEET!!!

Sub CreateFilesBasedOnData()
Dim NFN As String, FPath As String, NFFN As String, SourceWS As Worksheet, SourceWB As Workbook, CurrWS As Worksheet
Dim ClmnNum As Long, RwCnt As Long, RwCnt2 As Long, LstRW As Long, I As Long, I2 As Long

Set SourceWB = ActiveWorkbook
Set SourceWS = ActiveSheet
FPath = "C:\Data\"

ClmnNum = Application.WorksheetFunction.Match("Type", Rows(1), 0)
Columns(ClmnNum).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Range("D1" _
        ), Unique:=True
RwCnt = Cells(Rows.Count, 4).End(xlUp).Row
RwCnt2 = Cells(Rows.Count, 2).End(xlUp).Row

For I = 2 To RwCnt
    NFN = Cells(I, 4) & ".xlsx"
    NFFN = FPath & NFN
    Workbooks.Add
    Set CurrWS = ActiveSheet
    SourceWS.Activate
    Range("1:1").Copy
    CurrWS.Activate
    Range("A1").Select
    CurrWS.Paste
    LstRW = CurrWS.Cells(Rows.Count, 1).End(xlUp).Row
    
    For I2 = 2 To RwCnt2
        If SourceWS.Cells(I2, ClmnNum) = SourceWS.Cells(I, 4) Then
            SourceWS.Cells(I2, ClmnNum).EntireRow.Copy
            LstRW = LstRW + 1
            CurrWS.Activate
            CurrWS.Cells(LstRW, 1).Select
            CurrWS.Paste
        End If
    Next
    CurrWS.Range("C:D").Delete
Application.DisplayAlerts = False
    ActiveWorkbook.SaveAs Filename:=NFFN
    ActiveWorkbook.Close
Application.DisplayAlerts = True
Next

ActiveSheet.Columns(ClmnNum + 1).Delete
ActiveSheet.Columns(ClmnNum + 1).Delete

End Sub

Open in new window

0
 

Author Comment

by:adirisin
ID: 39911586
Hi,

Thanks for the solution. I am looking for VBscript, let me know if it is possible using that.

Aditya
0
 
LVL 12

Accepted Solution

by:
Harry Lee earned 500 total points
ID: 39914154
adirisin,

I had been running tests all day to get this to work.

I believe you will have to make a lots of changes since I believe your actual data file is much bigger than the example data you have provided.

Option Explicit
Dim xlApp
Dim NFN, FPath, NFFN, xlSourceWS, xlSourceWB, CurrWS, xlListWS, xlListWB
Dim ClmnNum, RwCnt, RwCnt2, LstRW, I, I2
Dim fso
Dim ListFile, TgrPath

' You need to change "C:\Data\VBSTest.xls" to whatever file carrying the list you mentioned in the question
ListFile = "C:\Data\VBSTest.xls"

' You need to set where you want to save the files
FPath = "C:\Data\"

Set fso = CreateObject("Scripting.FileSystemObject")

If (fso.FileExists(ListFile)) Then
    Set xlApp = CreateObject("Excel.Application")
    xlApp.Visible = True
    Set xlSourceWB = xlApp.Workbooks.Open(ListFile, 0, True)
    Set xlSourceWB = xlApp.ActiveWorkbook
    Set xlListWB = xlApp.Workbooks.Add()
    Set xlSourceWS = xlSourceWB.Worksheets("Sheet1")
    Set xlListWS = xlListWB.Worksheets("Sheet1")

    xlSourceWS.Range("B:B").AdvancedFilter 2, xlSourceWS.Range("B:B"), xlListWS.Range("A1"), True
    RwCnt = xlListWS.UsedRange.Rows.Count
    RwCnt2 = xlSourceWS.UsedRange.Rows.Count

    For I = 2 To RwCnt Step 1
        NFN = xlListWS.Cells(I, 1).Value & ".xlsx"
        NFFN = FPath & NFN
        xlApp.Workbooks.Add
        Set CurrWS = xlApp.ActiveSheet
        xlSourceWS.Activate
        xlSourceWS.Range("1:1").Copy
        CurrWS.Activate
        CurrWS.Range("A1").Select
        CurrWS.Paste
        LstRW = CurrWS.UsedRange.Rows.Count

        For I2 = 2 To RwCnt2 Step 1
            If xlSourceWS.Cells(I2, 2) = xlListWS.Cells(I, 1) Then
                xlSourceWS.Cells(I2, 2).EntireRow.Copy
                LstRW = LstRW + 1
                CurrWS.Activate
                CurrWS.Cells(LstRW, 1).Select
                CurrWS.Paste
            End If
        Next
        CurrWS.Range("C:D").Delete
	CurrWS.Range("A1").Select
	xlApp.Application.DisplayAlerts = False
        xlApp.ActiveWorkbook.SaveAs NFFN
        xlApp.ActiveWorkbook.Close
	xlApp.Application.DisplayAlerts = True
    Next

    xlApp.Application.DisplayAlerts = False
    xlSourceWB.Close
    xlListWB.Close
    xlApp.Application.DisplayAlerts = True

    xlApp.Quit
    Else
    WScript.Echo ("File does not exist!")
End If
WScript.Quit()

Open in new window

0
 

Author Closing Comment

by:adirisin
ID: 39926226
with slight customizations, i was able to get what i wanted. Thanks so much Harry!!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Welcome to part one of a multi-part tutorial series, VBScript for Windows System Administrators.  The goal of this series is to teach non-programmers how to write useful VBS code to automate their environment, and perform tasks faster, and in a more…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

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

20 Experts available now in Live!

Get 1:1 Help Now