Extraction of Data based on dynamic criteria

thespardian

Board Regular
Joined
Aug 31, 2012
Messages
119
Office Version
  1. 2016
  2. 2013
Platform
  1. Windows
Q1.png


I simply want a dynamic way to group the above data . if there is empty cell in the column J (Centre) then ignore the whole row otherwise extract the data groups as per below format.


Q2.png


Any help will be highly appreciated.
Here is the link for excel file
Question123.xlsx
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
If you are looking for VBA method then try this
Note: The result sheet is named as Result in my code

VBA Code:
Sub Extract()

Dim Centre$
Dim iRowData&, eRowData&, n&
Dim key As Variant
Dim cell As Range, rngData As Range
Dim dictCentre As Object
Dim wsData As Worksheet, wsResult As Worksheet
Dim wb As Workbook

Application.ScreenUpdating = False

Set wb = ActiveWorkbook
Set wsData = wb.Sheets("Data")
Set wsResult = wb.Sheets("Result")
Set dictCentre = CreateObject("Scripting.Dictionary")

iRowData = 12
eRowData = wsData.Range("A" & iRowData).End(xlDown).Row

Set rngData = wsData.Range("A" & iRowData, "A" & eRowData)

' Store all Center names
For Each cell In rngData
    Centre = cell.Offset(0, 9)
    If Not dictCentre.Exists(Centre) Then
        dictCentre.Add Centre, Centre
    End If
Next

' Write Result for each Centre
n = 6
For Each key In dictCentre.Keys
    wsResult.Range("A" & n) = UCase(key)
    n = n + 1
    For Each cell In rngData
        If cell.Offset(0, 9) = key Then
            wsData.Range("A" & cell.Row, "I" & cell.Row).Copy
            wsResult.Range("A" & n).PasteSpecial (xlPasteValuesAndNumberFormats)
            n = n + 1
        End If
    Next
    n = n + 1
Next

Application.CutCopyMode = False
Application.ScreenUpdating = True

End Sub
@ Zot -- These codes are working fine. But when i have an ampty cell in "Centre". It doesn't ignore it. I only need the data which a value in "Centre" Column
 
Last edited:
Upvote 0
@ Zot -- These codes are working fine. But when i have an ampty cell in "Centre". It doesn't ignore it. I only need the data which a value in "Centre" Column
My fault I forgot about the condition where you did mention about this requirement. Then just add line to skip copy if the Center row is empty

VBA Code:
Sub Extract()

Dim Centre$
Dim iRowData&, eRowData&, n&
Dim key As Variant
Dim cell As Range, rngData As Range
Dim dictCentre As Object
Dim wsData As Worksheet, wsResult As Worksheet
Dim wb As Workbook

Application.ScreenUpdating = False

Set wb = ActiveWorkbook
Set wsData = wb.Sheets("Data")
Set wsResult = wb.Sheets("Result")
Set dictCentre = CreateObject("Scripting.Dictionary")

iRowData = 12
eRowData = wsData.Range("A" & iRowData).End(xlDown).Row

Set rngData = wsData.Range("A" & iRowData, "A" & eRowData)

' Store all Center names
For Each cell In rngData
    Centre = cell.Offset(0, 9)
    If Not dictCentre.Exists(Centre) Then
        dictCentre.Add Centre, Centre
    End If
Next

' Write Result for each Centre
n = 6
For Each key In dictCentre.Keys
    wsResult.Range("A" & n) = UCase(key)
    n = n + 1
    For Each cell In rngData
        If Not Len(Range("J" & cell.Row)) = 0 Then                                                ' Skip the line if Center is empty
            If cell.Offset(0, 9) = key Then
                wsData.Range("A" & cell.Row, "I" & cell.Row).Copy
                wsResult.Range("A" & n).PasteSpecial (xlPasteValuesAndNumberFormats)
                n = n + 1
            End If
        End If
    Next
    n = n + 1
Next

Application.CutCopyMode = False
Application.ScreenUpdating = True

End Sub
 
Upvote 0
Solution
@ Zot -- These codes are working fine. But when i have an ampty cell in "Centre". It doesn't ignore it. I only need the data which a value in "Centre" Column
I have some empty cells in the start of Column "Centre" and the results are as under. The data from Sr. 1 to 17 is not required.
Q3.png
 
Upvote 0
My fault I forgot about the condition where you did mention about this requirement. Then just add line to skip copy if the Center row is empty

VBA Code:
Sub Extract()

Dim Centre$
Dim iRowData&, eRowData&, n&
Dim key As Variant
Dim cell As Range, rngData As Range
Dim dictCentre As Object
Dim wsData As Worksheet, wsResult As Worksheet
Dim wb As Workbook

Application.ScreenUpdating = False

Set wb = ActiveWorkbook
Set wsData = wb.Sheets("Data")
Set wsResult = wb.Sheets("Result")
Set dictCentre = CreateObject("Scripting.Dictionary")

iRowData = 12
eRowData = wsData.Range("A" & iRowData).End(xlDown).Row

Set rngData = wsData.Range("A" & iRowData, "A" & eRowData)

' Store all Center names
For Each cell In rngData
    Centre = cell.Offset(0, 9)
    If Not dictCentre.Exists(Centre) Then
        dictCentre.Add Centre, Centre
    End If
Next

' Write Result for each Centre
n = 6
For Each key In dictCentre.Keys
    wsResult.Range("A" & n) = UCase(key)
    n = n + 1
    For Each cell In rngData
        If Not Len(Range("J" & cell.Row)) = 0 Then                                                ' Skip the line if Center is empty
            If cell.Offset(0, 9) = key Then
                wsData.Range("A" & cell.Row, "I" & cell.Row).Copy
                wsResult.Range("A" & n).PasteSpecial (xlPasteValuesAndNumberFormats)
                n = n + 1
            End If
        End If
    Next
    n = n + 1
Next

Application.CutCopyMode = False
Application.ScreenUpdating = True

End Sub
Thanks a lot for your prompt reply. It working. You gave me the strength of knowledge for which i am really thankful. Thanks again for taking time. Stay blessed.
 
Upvote 0
Thanks a lot for your prompt reply. It working. You gave me the strength of knowledge for which i am really thankful. Thanks again for taking time. Stay blessed.
Thanks for the update. Glad to have the code works. :)
 
Upvote 0
The vb code you provided is showing error at this line. Can you please check it again.

Thanks a lot for your detailed reply and the guidance.
oops... typo error.
hth... was not part of the original code, my bad... you can remove it.
 
Upvote 0
Thanks for the update. Glad to have the code works. :)
@Zot Sorry to bother you again. I am facing a strange problem. When i assign the macro to the button. The results are incorrect. But when i run the code (by clicking Developer > Visual Basic> Module1 and hit the play button or f5). The results are accurate. I am attaching the file. Can you please check it for me.
Zot.xlsm
 
Upvote 0
@Zot Sorry to bother you again. I am facing a strange problem. When i assign the macro to the button. The results are incorrect. But when i run the code (by clicking Developer > Visual Basic> Module1 and hit the play button or f5). The results are accurate. I am attaching the file. Can you please check it for me.
Zot.xlsm
I clicked the button and I don't see anything wrong on the Result sheet. What was not correct? I probably missed something.
 
Upvote 0

Forum statistics

Threads
1,214,894
Messages
6,122,124
Members
449,066
Latest member
Andyg666

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top