• Skip to main content
  • Skip to header right navigation
  • Skip to site footer

My Online Training Hub

Learn Dashboards, Excel, Power BI, Power Query, Power Pivot

  • Courses
  • Pricing
    • Free Courses
    • Power BI Course
    • Excel Power Query Course
    • Power Pivot and DAX Course
    • Excel Dashboard Course
    • Excel PivotTable Course – Quick Start
    • Advanced Excel Formulas Course
    • Excel Expert Advanced Excel Training
    • Excel Tables Course
    • Excel, Word, Outlook
    • Financial Modelling Course
    • Excel PivotTable Course
    • Excel for Customer Service Professionals
    • Excel for Operations Management Course
    • Excel for Decision Making Under Uncertainty Course
    • Excel for Finance Course
    • Excel Analysis ToolPak Course
    • Multi-User Pricing
  • Resources
    • Free Downloads
    • Excel Functions Explained
    • Excel Formulas
    • Excel Add-ins
    • IF Function
      • Excel IF Statement Explained
      • Excel IF AND OR Functions
      • IF Formula Builder
    • Time & Dates in Excel
      • Excel Date & Time
      • Calculating Time in Excel
      • Excel Time Calculation Tricks
      • Excel Date and Time Formatting
    • Excel Keyboard Shortcuts
    • Excel Custom Number Format Guide
    • Pivot Tables Guide
    • VLOOKUP Guide
    • ALT Codes
    • Excel VBA & Macros
    • Excel User Forms
    • VBA String Functions
  • Members
    • Login
    • Password Reset
  • Blog
  • Excel Webinars
  • Excel Forum
    • Register as Forum Member

Parse Sheets of data Based on Criteria List|VBA & Macros|Excel Forum|My Online Training Hub

You are here: Home / Parse Sheets of data Based on Criteria List|VBA & Macros|Excel Forum|My Online Training Hub
Avatar
sp_LogInOut Log In sp_Registration Register
sp_Search Search
Advanced Search|Last Search Results
Search
Forum Scope




Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Search Search
sp_RankInfo
Lost password?
sp_CrumbsHome HomeExcel ForumVBA & MacrosParse Sheets of data Based on Crite…
sp_PrintTopic sp_TopicIcon
Parse Sheets of data Based on Criteria List
Avatar
Sherry Fox
Poinciana, FL
Member
Members
Level 0
Forum Posts: 68
Member Since:
December 4, 2021
sp_UserOfflineSmall Offline
1
January 11, 2022 - 8:15 am
sp_Permalink sp_Print

I need to be able to parse out data based on multiple sheets.  When this happens there will be a ne sheet created and renamed with the data in Column B on the Master.  Now, not all data on the Master will be used, the QCH # in column B of the Master MUST appear on Column A of the Status Percent Sheet, otherwise, it is not included.

 

Sheet Name: Master, Named Range: Table2, Column: B (Change Number)

Sheet Name: Status Percent, Named Range: Table1, Column: A (Change Number)

 

I would like the newly parsed sheets to be renamed based on the QCH number, for example "QCH00059634".  Then all records with that QCH # would appear on that sheet.  Some of these sheets may contain only a few records while others will contain a great many.  And if possible, I would like the set of new QCH sheets to appear in a new workbook and the file would be saved as "Transformation QCHs Lates - MM-DD-YYYY.xlsx  Thanks so much!!!!

sp_AnswersTopicSeeAnswer See Answer
Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1807
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
2
January 13, 2022 - 4:29 am
sp_Permalink sp_Print

Hi Sherry,

Is there a reason why you need to split data into sheets? Most of the times, data is best used if it's in one place.

Avatar
Sherry Fox
Poinciana, FL
Member
Members
Level 0
Forum Posts: 68
Member Since:
December 4, 2021
sp_UserOfflineSmall Offline
3
January 14, 2022 - 4:39 am
sp_Permalink sp_Print

Catalin Bombea,

I agree, but the manager will use this, and each of those tabs that is is split into will be accessed by different people.  I have already suggested filtering and such.  However management is set in their ways on how they want the final result.  There is no changing their minds about it.

Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1807
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
4
January 14, 2022 - 2:54 pm
sp_Permalink sp_Print

Hi Sherry,

Put the following code in a normal vba module (included also in the file attached), should do what you need:

Option Explicit

Sub ExportData()
Dim Status As ListObject: Set Status = ThisWorkbook.Worksheets("Status Percent").ListObjects("Table1")
Dim Master As ListObject: Set Master = ThisWorkbook.Worksheets("Master").ListObjects("Table2")
If Status.DataBodyRange Is Nothing Or Master.DataBodyRange Is Nothing Then Exit Sub
'load source data into arrays
Dim ArrStatus As Variant: ArrStatus = Status.DataBodyRange.Value
Dim ArrMaster As Variant: ArrMaster = Master.DataBodyRange.Value
Dim i As Long, ArrResults() As Variant, Counter As Long, ChangeCode As String, aKey As Variant

'build Dictionaries
Dim StatusDict As Object: Set StatusDict = CreateObject("scripting.dictionary")
Dim MasterDict As Object: Set MasterDict = CreateObject("scripting.dictionary")

For i = 1 To UBound(ArrStatus)
ChangeCode = ArrStatus(i, Status.ListColumns("Change Number").Index)
StatusDict(ChangeCode) = ChangeCode
Next

For i = 1 To UBound(ArrMaster)
ChangeCode = ArrMaster(i, Master.ListColumns("Change Number").Index)
If StatusDict.Exists(ChangeCode) Then
If MasterDict.Exists(ChangeCode) = True Then
ArrResults = MasterDict(ChangeCode)
Counter = UBound(ArrResults, 2) + 1
Else
Counter = 1
ReDim ArrResults(1 To Master.ListColumns.Count, 1 To Counter)
'add headers
ArrResults(Master.ListColumns("Org. Entity").Index, Counter) = "Org. Entity"
ArrResults(Master.ListColumns("Change Number").Index, Counter) = "Change Number"
ArrResults(Master.ListColumns("Process Area (Alignment)").Index, Counter) = "Process Area (Alignment)"
ArrResults(Master.ListColumns("QMS Process").Index, Counter) = "QMS Process"
Counter = Counter + 1
End If

ReDim Preserve ArrResults(1 To Master.ListColumns.Count, 1 To Counter)
ArrResults(Master.ListColumns("Org. Entity").Index, Counter) = ArrMaster(i, Master.ListColumns("Org. Entity").Index)
ArrResults(Master.ListColumns("Change Number").Index, Counter) = ChangeCode
ArrResults(Master.ListColumns("Process Area (Alignment)").Index, Counter) = ArrMaster(i, Master.ListColumns("Process Area (Alignment)").Index)
ArrResults(Master.ListColumns("QMS Process").Index, Counter) = ArrMaster(i, Master.ListColumns("QMS Process").Index)

MasterDict(ChangeCode) = ArrResults
End If
Next

If MasterDict.Count > 0 Then
Dim NewWb As Workbook: Set NewWb = Workbooks.Add
Dim DestFolder As String: DestFolder = ThisWorkbook.Path & Application.PathSeparator & "Exports"
If Len(Dir$(DestFolder, vbDirectory)) = 0 Then MkDir DestFolder
NewWb.SaveAs DestFolder & Application.PathSeparator & "Transformation QCHs Lates - " & Format(Date, "mm-dd-yyyy") & ".xlsx"
Dim NewSh As Worksheet, Rng As Range
For Each aKey In MasterDict.Keys
Counter = UBound(MasterDict(aKey), 2)
'add new sheet
Set NewSh = NewWb.Worksheets.Add
NewSh.Name = CStr(aKey)
If Counter > 0 Then
Set Rng = NewSh.Range(NewSh.Cells(1), NewSh.Cells(Counter, Master.ListColumns.Count))
Rng.Value = TransposeArray(MasterDict(aKey))
Rng.Columns.AutoFit
NewSh.ListObjects.Add xlSrcRange, Rng, , xlYes
End If

Next aKey
End If

End Sub

Function TransposeArray(myarray As Variant) As Variant
Dim X As Long
Dim Y As Long
Dim Xupper As Long
Dim Yupper As Long
Dim tempArray As Variant
Xupper = UBound(myarray, 2)
Yupper = UBound(myarray, 1)
ReDim tempArray(LBound(myarray, 2) To Xupper, LBound(myarray, 1) To Yupper)
For X = LBound(myarray, 2) To Xupper
For Y = LBound(myarray, 1) To Yupper
tempArray(X, Y) = myarray(Y, X)
Next Y
Next X
TransposeArray = tempArray
End Function

Avatar
Sherry Fox
Poinciana, FL
Member
Members
Level 0
Forum Posts: 68
Member Since:
December 4, 2021
sp_UserOfflineSmall Offline
5
February 1, 2022 - 1:37 am
sp_Permalink sp_Print

Catalin Bombea,

First sorry for my delay in responding.  My boss had me on an Urgent project, and there was no spare time.  Thank you for looking into this for me.  I tried the macro in the workbook you attached, and it resulted in an error message.  Attached is the error message and where it shows when I went to attempt to debug.

 

2022-01-31_9-38-36.jpgImage Enlarger

2022-01-31_9-44-07.jpgImage Enlarger

sp_PlupAttachments Attachments
  • sp_PlupImage 2022-01-31_9-38-36.jpg (10 KB)
  • sp_PlupImage 2022-01-31_9-44-07.jpg (47 KB)
Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1807
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
6
February 1, 2022 - 1:58 am
sp_Permalink sp_Print sp_EditHistory

Where did you saved the test file?

Location is important.

OneDrive has some specific issues, so I suggest saving the file in a location where you have rights to create folders, but not OneDrive.

Avatar
Sherry Fox
Poinciana, FL
Member
Members
Level 0
Forum Posts: 68
Member Since:
December 4, 2021
sp_UserOfflineSmall Offline
7
February 2, 2022 - 2:25 am
sp_Permalink sp_Print

I hate OneDrive... they force us to use it at work.  This is great, but I do have one question.  It worked perfectly, however, it also added a blank sheet called "Sheet1", is there a way to prevent that?

Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1807
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
8
February 2, 2022 - 4:19 am
sp_Permalink sp_Print sp_EditHistory

Sheet1is not added by code, it is the initial sheet that is in the newly added workbook (when you create a new workbook, it does not come empty, there is at least 1 sheet in it).

After all necessary sheets are added, we can write a code to delete initial empty "SheetX" sheets.

If you need to use it in OneDrive, it can be done, but OneDrive is a complex environment. For example, there is OneDrive Personal environment, OneDrive Business, SharePoint Site files. If you have multiple office licenses, there will be multiple onedrive link types.

Let me know if you have to use it in OneDrive, I can prepare a solution to manage these environments.

Avatar
Sherry Fox
Poinciana, FL
Member
Members
Level 0
Forum Posts: 68
Member Since:
December 4, 2021
sp_UserOfflineSmall Offline
9
February 2, 2022 - 4:49 am
sp_Permalink sp_Print

Catalin Bombea,

What code would need to be added to delete Sheet1 (or whatever empty sheet) , and where would it be added?  I am still learning how to code.

As for OneDrive, I think it would be easier for me to have my user run this process from their download folder.  I have already confirmed your macro works in that folder.  The end user can then email the file directly from that location.  So we don't need to try to overcomplicate things due to OneDrive.  I appreciate your willingness on that though.

Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1807
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
10
February 2, 2022 - 5:56 am
sp_Permalink sp_Print

Before this line:

If MasterDict.Count > 0 Then

The following code should be added:

Dim Wks as worksheet

Application.DisplayAlerts=False

For Each Wks in ThisWorkbook.Worksheets

If Wks.Name like "Sheet*" then Wks.Delete

Next Wks

Application.DisplayAlerts=True

Avatar
Sherry Fox
Poinciana, FL
Member
Members
Level 0
Forum Posts: 68
Member Since:
December 4, 2021
sp_UserOfflineSmall Offline
11
February 2, 2022 - 6:30 am
sp_Permalink sp_Print sp_EditHistory

Catalin Bombea,

I added the code, and ran the macro from my Downloads folder, and I still got the blank Sheet1.  Did I do something wrong? New text you told me to add to the code is in Red. NOTE:  I looked for code tags but did not see any, I am sorry.

Option Explicit

Sub ExportData()
Dim Status As ListObject: Set Status = ThisWorkbook.Worksheets("Status Percent").ListObjects("Table1")
Dim Master As ListObject: Set Master = ThisWorkbook.Worksheets("Master").ListObjects("Table2")
If Status.DataBodyRange Is Nothing Or Master.DataBodyRange Is Nothing Then Exit Sub
'load source data into arrays
Dim ArrStatus As Variant: ArrStatus = Status.DataBodyRange.Value
Dim ArrMaster As Variant: ArrMaster = Master.DataBodyRange.Value
Dim i As Long, ArrResults() As Variant, Counter As Long, ChangeCode As String, aKey As Variant

'build Dictionaries
Dim StatusDict As Object: Set StatusDict = CreateObject("scripting.dictionary")
Dim MasterDict As Object: Set MasterDict = CreateObject("scripting.dictionary")

For i = 1 To UBound(ArrStatus)
ChangeCode = ArrStatus(i, Status.ListColumns("Change Number").Index)
StatusDict(ChangeCode) = ChangeCode
Next

For i = 1 To UBound(ArrMaster)
ChangeCode = ArrMaster(i, Master.ListColumns("Change Number").Index)
If StatusDict.Exists(ChangeCode) Then
If MasterDict.Exists(ChangeCode) = True Then
ArrResults = MasterDict(ChangeCode)
Counter = UBound(ArrResults, 2) + 1
Else
Counter = 1
ReDim ArrResults(1 To Master.ListColumns.Count, 1 To Counter)
'add headers
ArrResults(Master.ListColumns("Org. Entity").Index, Counter) = "Org. Entity"
ArrResults(Master.ListColumns("Change Number").Index, Counter) = "Change Number"
ArrResults(Master.ListColumns("Process Area (Alignment)").Index, Counter) = "Process Area (Alignment)"
ArrResults(Master.ListColumns("QMS Process").Index, Counter) = "QMS Process"
Counter = Counter + 1
End If

ReDim Preserve ArrResults(1 To Master.ListColumns.Count, 1 To Counter)
ArrResults(Master.ListColumns("Org. Entity").Index, Counter) = ArrMaster(i, Master.ListColumns("Org. Entity").Index)
ArrResults(Master.ListColumns("Change Number").Index, Counter) = ChangeCode
ArrResults(Master.ListColumns("Process Area (Alignment)").Index, Counter) = ArrMaster(i, Master.ListColumns("Process Area (Alignment)").Index)
ArrResults(Master.ListColumns("QMS Process").Index, Counter) = ArrMaster(i, Master.ListColumns("QMS Process").Index)

MasterDict(ChangeCode) = ArrResults
End If
Next

Dim Wks As Worksheet

Application.DisplayAlerts = False

For Each Wks In ThisWorkbook.Worksheets

If Wks.Name Like "Sheet*" Then Wks.Delete

Next Wks

Application.DisplayAlerts = True

If MasterDict.Count > 0 Then
Dim NewWb As Workbook: Set NewWb = Workbooks.Add
Dim DestFolder As String: DestFolder = ThisWorkbook.Path & Application.PathSeparator & "Exports"
If Len(Dir$(DestFolder, vbDirectory)) = 0 Then MkDir DestFolder
NewWb.SaveAs DestFolder & Application.PathSeparator & "Transformation QCHs Lates - " & Format(Date, "mm-dd-yyyy") & ".xlsx"
Dim NewSh As Worksheet, Rng As Range
For Each aKey In MasterDict.Keys
Counter = UBound(MasterDict(aKey), 2)
'add new sheet
Set NewSh = NewWb.Worksheets.Add
NewSh.Name = CStr(aKey)
If Counter > 0 Then
Set Rng = NewSh.Range(NewSh.Cells(1), NewSh.Cells(Counter, Master.ListColumns.Count))
Rng.Value = TransposeArray(MasterDict(aKey))
Rng.Columns.AutoFit
NewSh.ListObjects.Add xlSrcRange, Rng, , xlYes
End If

Next aKey
End If

End Sub

Function TransposeArray(myarray As Variant) As Variant
Dim X As Long
Dim Y As Long
Dim Xupper As Long
Dim Yupper As Long
Dim tempArray As Variant
Xupper = UBound(myarray, 2)
Yupper = UBound(myarray, 1)
ReDim tempArray(LBound(myarray, 2) To Xupper, LBound(myarray, 1) To Yupper)
For X = LBound(myarray, 2) To Xupper
For Y = LBound(myarray, 1) To Yupper
tempArray(X, Y) = myarray(Y, X)
Next Y
Next X
TransposeArray = tempArray
End Function

Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1807
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
12
February 2, 2022 - 3:15 pm
sp_Permalink sp_Print

Sorry, the place for the code is wrong, my mistake.

Please move the code just above End Sub line.

Avatar
Sherry Fox
Poinciana, FL
Member
Members
Level 0
Forum Posts: 68
Member Since:
December 4, 2021
sp_UserOfflineSmall Offline
13
February 3, 2022 - 12:15 am
sp_Permalink sp_Print

Catalin Bombea,

I moved that new code (the part in RED) from the original location you stated to above End Sub as you mentioned.  Sorry, I still cannot find where the code tags are on this forum.  The rest of the code works, it is just that the Sheet1, which is a blank in the new workbook exists.

End If

Code pasted here

End Sub

Avatar
Catalin Bombea
Iasi, Romania
Admin
Level 10
Forum Posts: 1807
Member Since:
November 8, 2013
sp_UserOfflineSmall Offline
14
February 3, 2022 - 1:18 am
sp_Permalink sp_Print sp_EditHistory

Hi Sherry,

The code is properly placed now, but I made a small mistake:

I referred to ThisWorkbook, where the code is, not to the NewWb, where the sheets are created.

Updated code (added also a new check, to make sure we do not delete all sheets, there must be at least one in a workbook, code will fail to delete the last sheet):

If NewWb is nothing then exit sub ' no new file was created
Dim Wks As Worksheet
Application.DisplayAlerts = False
For Each Wks In NewWb.Worksheets
Debug.Print Wks.Name
If Wks.Name Like "Sheet*" And NewWb.Worksheets.Count > 1 Then Wks.Delete
Next Wks
Application.DisplayAlerts = True

To add code tags, simply type the start and closing tags:

Screenshot-2022-02-02-172707.pngImage Enlarger

sp_PlupAttachments Attachments
  • sp_PlupImage Screenshot-2022-02-02-172707.png (5 KB)
sp_AnswersTopicAnswer
Answers Post
Avatar
Sherry Fox
Poinciana, FL
Member
Members
Level 0
Forum Posts: 68
Member Since:
December 4, 2021
sp_UserOfflineSmall Offline
15
February 3, 2022 - 1:58 am
sp_Permalink sp_Print

Catalin Bombea,

Perfect!  Thanks so much for all your help on this.  It is amazing how the tiniest of errors, or a wrong location will cause the whole code to explode!  This is EXACTLY the way management wan ts the file files.  Although I do agree, it should be on 1 worksheet so it can be analyzed. An d thanks for the tip on the code tags, I know now for next time!

sp_Feed
Go to top
Forum Timezone: Australia/Brisbane
Most Users Ever Online: 245
Currently Online: David, yonatan zelig, Christopher Anderson
Guest(s) 10
Currently Browsing this Page:
1 Guest(s)
Top Posters:
SunnyKow: 1432
Anders Sehlstedt: 870
Purfleet: 412
Frans Visser: 346
David_Ng: 306
lea cohen: 219
A.Maurizio: 202
Jessica Stewart: 202
Aye Mu: 201
jaryszek: 183
Newest Members:
yashal minahil
Oluwadamilola Ogun
Yannik H
dectator mang
Francis Drouillard
Orlando Inocente
Jovitha Clemence
Maloxat Axmatovna
Ricardo Freitas
Marko Meglic
Forum Stats:
Groups: 3
Forums: 24
Topics: 6200
Posts: 27182

 

Member Stats:
Guest Posters: 49
Members: 31861
Moderators: 3
Admins: 4
Administrators: Mynda Treacy, Philip Treacy, Catalin Bombea, FT
Moderators: MOTH Support, Velouria, Riny van Eekelen
© Simple:Press —sp_Information

Sidebar

Blog Categories

  • Excel
  • Excel Charts
  • Excel Dashboard
  • Excel Formulas
  • Excel PivotTables
  • Excel Shortcuts
  • Excel VBA
  • General Tips
  • Online Training
  • Outlook
  • Power Apps
  • Power Automate
  • Power BI
  • Power Pivot
  • Power Query
microsoft mvp logo
trustpilot excellent rating
Secured by Sucuri Badge
MyOnlineTrainingHub on YouTube Mynda Treacy on Linked In Mynda Treacy on Instagram Mynda Treacy on Twitter Mynda Treacy on Pinterest MyOnlineTrainingHub on Facebook
 

Company

  • About My Online Training Hub
  • Disclosure Statement
  • Frequently Asked Questions
  • Guarantee
  • Privacy Policy
  • Terms & Conditions
  • Testimonials
  • Become an Affiliate

Support

  • Contact
  • Forum
  • Helpdesk - For Technical Issues

Copyright © 2023 · My Online Training Hub · All Rights Reserved. Microsoft and the Microsoft Office logo are trademarks or registered trademarks of Microsoft Corporation in the United States and/or other countries. Product names, logos, brands, and other trademarks featured or referred to within this website are the property of their respective trademark holders.