Tejas Kore
Board Regular
- Joined
- Nov 2, 2017
- Messages
- 72
- Office Version
- 365
- Platform
- Windows
Hi Friends,
I have this data.
A 1
A 2
B 3
B 4
I am creating new files based on first column.So for this purpose I am applying filter on 1st column (taking input from user) and then based on applied filter generating new files(in this case one file for 'A' and another one for 'B').Below is the code to do this.
Now I want to enhance this code by adding a new functionality.
I want to display available filter values to user(in this case 'A' and 'B') and then let user select which filter values he/she wants.
For eg. in the above example if users selects both the available filters then two files will be generated and if he/she selects any one value 'A' or 'B' then accordingly one file should be generated.
How can this be achieved ?
I don't have any idea about Userforms as of now that's why I didn't write anything regarding that in the below code.
Sub Sample()
Dim wswb As String
Dim wssh As String
wswb = ActiveWorkbook.Name
wssh = ActiveSheet.Name
vcolumn = InputBox("Please indicate which column,you would like to split by", "Column Selection")
Columns(vcolumn).Copy
Sheets.Add
ActiveSheet.Name = "_Summary"
Range("A1").PasteSpecial
Columns("A").RemoveDuplicates Columns:=1, Header:=xlYes
vCounter = Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To vCounter
vfilter = Sheets("_Summary").Cells(i, 1)
Sheets(wssh).Activate
ActiveSheet.Columns.AutoFilter field:=Columns(vcolumn).Column, Criteria1:=vfilter
Cells.Copy
Workbooks.Add
Range("A1").PasteSpecial
If vfilter <> "" Then
ActiveWorkbook.SaveAs ThisWorkbook.Path & "\split results" & vfilter
Else
ActiveWorkbook.SaveAs ThisWorkbook.Path & "\split results\_Empty"
End If
ActiveWorkbook.Close
Workbooks(wswb).Activate
Next i
Cells.AutoFilter
Application.DisplayAlerts = False
Sheets("_Summary").Delete
End Sub
I have this data.
A 1
A 2
B 3
B 4
I am creating new files based on first column.So for this purpose I am applying filter on 1st column (taking input from user) and then based on applied filter generating new files(in this case one file for 'A' and another one for 'B').Below is the code to do this.
Now I want to enhance this code by adding a new functionality.
I want to display available filter values to user(in this case 'A' and 'B') and then let user select which filter values he/she wants.
For eg. in the above example if users selects both the available filters then two files will be generated and if he/she selects any one value 'A' or 'B' then accordingly one file should be generated.
How can this be achieved ?
I don't have any idea about Userforms as of now that's why I didn't write anything regarding that in the below code.
Sub Sample()
Dim wswb As String
Dim wssh As String
wswb = ActiveWorkbook.Name
wssh = ActiveSheet.Name
vcolumn = InputBox("Please indicate which column,you would like to split by", "Column Selection")
Columns(vcolumn).Copy
Sheets.Add
ActiveSheet.Name = "_Summary"
Range("A1").PasteSpecial
Columns("A").RemoveDuplicates Columns:=1, Header:=xlYes
vCounter = Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To vCounter
vfilter = Sheets("_Summary").Cells(i, 1)
Sheets(wssh).Activate
ActiveSheet.Columns.AutoFilter field:=Columns(vcolumn).Column, Criteria1:=vfilter
Cells.Copy
Workbooks.Add
Range("A1").PasteSpecial
If vfilter <> "" Then
ActiveWorkbook.SaveAs ThisWorkbook.Path & "\split results" & vfilter
Else
ActiveWorkbook.SaveAs ThisWorkbook.Path & "\split results\_Empty"
End If
ActiveWorkbook.Close
Workbooks(wswb).Activate
Next i
Cells.AutoFilter
Application.DisplayAlerts = False
Sheets("_Summary").Delete
End Sub