Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Populate Excel sheets based on criteria from import sheet using VBA

Posted on 2016-07-22
7
Medium Priority
?
99 Views
Last Modified: 2016-07-25
I have an excel workbook with a worksheet call "Import".  The data is refreshed from a sharepoint list.  The data structure is similar to this:

ID      Loc      Start Date      End Date      Group
44      CA      5/6/2017      5/8/2017      A
33      TX      6/8/2017      6/10/2017      B
34      NM      9/1/2017      9/3/2017      C
33      DC      9/1/2017      9/3/2017      B
33      TX      6/8/2017      6/10/2017      B
34      NM      9/1/2017      9/3/2017      C
33      DC      9/1/2017      9/3/2017      B
33      TX      6/8/2017      6/10/2017      B
34      NM      9/1/2017      9/3/2017      C
33      DC      9/1/2017      9/3/2017      B

I would like to create VBA to go through each imported row of data and populate 3 additional worksheet tabs (Group A) (Group B) and (Group C) with only the  matching criteria.

For example Worksheet labeled "Group B" be recreated and look like this:

ID Loc      Start Date      End Date      Group
33      TX      6/8/2017      6/10/2017      B
33      DC      9/1/2017      9/3/2017      B
33      TX      6/8/2017      6/10/2017      B
33      DC      9/1/2017      9/3/2017      B
33      TX      6/8/2017      6/10/2017      B
33      DC      9/1/2017      9/3/2017      B

Thanks for any help//suggestions.
0
Comment
Question by:robbdfw
[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
  • 4
  • 3
7 Comments
 
LVL 33

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41725277
Please try this...
In the attached, click the button on Import Sheet called Split Data to run the code to get the desired output.
Sub SplitData()
Dim sws As Worksheet, dws As Worksheet
Dim lr As Long, i As Long, j As Long
Dim dict As Object
Dim x, y
Application.ScreenUpdating = False
Set sws = Sheets("Import")
lr = sws.Cells(Rows.Count, 1).End(xlUp).Row
Set dict = CreateObject("Scripting.Dictionary")
x = sws.Range("E2:E" & lr).Value

For i = 1 To UBound(x, 1)
   dict.Item(x(i, 1)) = x(i, 1)
Next i
y = dict.items
For i = 0 To UBound(y)
   With sws.Rows(1)
      On Error Resume Next
      Set dws = Sheets(y(i))
      dws.Cells.cle
      On Error GoTo 0
      If dws Is Nothing Then Sheets.Add(after:=Sheets(Sheets.Count)).Name = "Group " & y(i)
      Set dws = ActiveSheet
      .AutoFilter field:=5, Criteria1:=y(i)
      sws.Range("A1").CurrentRegion.SpecialCells(xlCellTypeVisible).Copy dws.Range("A1")
      dws.Columns.AutoFit
      dws.Range("A1").CurrentRegion.Borders.Color = vbBlack
   End With
   Set dws = Nothing
Next i
Set dict = Nothing
sws.AutoFilterMode = 0
sws.Activate
Application.ScreenUpdating = True
End Sub

Open in new window

Split-Data.xlsm
0
 

Author Comment

by:robbdfw
ID: 41725920
Worked like a charm, however when I added additional columns of data I kept getting a VBA 400 error.  I kept the Group in column E, howevered added about 10 columns to the right. Columns F-Column P. Would that be the cause of the problem?
0
 
LVL 33

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41725935
No. That shouldn't be the case.
Did you tweak the code at your end?
It would be helpful if you can upload the workbook along with the code in question.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:robbdfw
ID: 41727963
I have attached a worksheet that includes your code as well as a couple of lines with the import tab as it comes in from our share point site.
 
ID      Loc  Start      End          Group
17      TX      7/25/16      8/24/17      A
18      CA      6/24/16      6/29/16      B
19      CA      7/25/16      8/24/17      C
20      TX      6/24/16      6/29/16      D
21      CA      7/25/16      8/24/17      A
22      CA      6/24/16      6/29/16      D
23      TX      7/25/16      8/24/17      B
24      CA      6/24/16      6/29/16      C
25      CA      7/25/16      8/24/17      D
26      TX      6/24/16      6/29/16      D
27      CA      7/25/16      8/24/17      D
28      CA      6/24/16      6/29/16      D
29      TX      7/25/16      8/24/17      D
30      CA      6/24/16      6/29/16      B
31      CA      7/25/16      8/24/17      D
32      TX      6/24/16      6/29/16      C
33      CA      7/25/16      8/24/17      D
34      CA      6/24/16      6/29/16      D
35      TX      7/25/16      8/24/17      D
36      CA      6/24/16      6/29/16      D
37      CA      7/25/16      8/24/17      D
38      TX      6/24/16      6/29/16      D
39      CA      7/25/16      8/24/17      C
40      CA      6/24/16      6/29/16      D
41      TX      7/25/16      8/24/17      D
42      CA      6/24/16      6/29/16      D
43      CA      7/25/16      8/24/17      D
44      TX      6/24/16      6/29/16      D
45      CA      7/25/16      8/24/17      E
SampleData.xlsm
0
 
LVL 33

Accepted Solution

by:
Subodh Tiwari (Neeraj) earned 2000 total points
ID: 41728008
The code was not working as your data is formatted as an Excel Table.
Also you need to place this code on a standard module not on ThisWorkbook module as you have done in the sample file.

Please try this.....


Sub SplitData()
Dim sws As Worksheet, dws As Worksheet
Dim lr As Long, i As Long, j As Long
Dim dict As Object
Dim x, y
Dim tbl As ListObject
Application.ScreenUpdating = False
Set sws = Sheets("Import")
lr = sws.Cells(Rows.Count, 1).End(xlUp).Row
Set tbl = sws.ListObjects("Table_owssvr")
Set dict = CreateObject("Scripting.Dictionary")
x = sws.Range("E2:E" & lr).Value

For i = 1 To UBound(x, 1)
   dict.Item(x(i, 1)) = x(i, 1)
Next i
y = dict.items
For i = 0 To UBound(y)
   With tbl.Range
      On Error Resume Next
      Set dws = Sheets(y(i))
      dws.Cells.Clear
      On Error GoTo 0
      If dws Is Nothing Then Sheets.Add(after:=Sheets(Sheets.Count)).Name = "Group " & y(i)
      Set dws = ActiveSheet
      .AutoFilter Field:=5, Criteria1:=y(i)
      sws.Range("A1").CurrentRegion.SpecialCells(xlCellTypeVisible).Copy dws.Range("A1")
      dws.Columns.AutoFit
      dws.Range("A1").CurrentRegion.Borders.Color = vbBlack
   End With
   Set dws = Nothing
Next i
Set dict = Nothing
tbl.Range.AutoFilter Field:=5
sws.Activate
Application.ScreenUpdating = True
End Sub

Open in new window

SampleData.xlsm
1
 

Author Closing Comment

by:robbdfw
ID: 41728227
It worked perfectly!  Thank you for all your help.  I appreciate your assistance.
0
 
LVL 33

Expert Comment

by:Subodh Tiwari (Neeraj)
ID: 41728280
You're welcome. Glad I could help. :)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Question has a verified solution.

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

Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

636 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