Fetch unique values from several sheets to one sheet.

Lux Aeterna

Board Regular
Joined
Aug 27, 2015
Messages
201
Office Version
  1. 2019
Platform
  1. Windows
There's an excel workbook that contains several sheets with our yearly workload and I'd like to summarise that data in a new sheet.

Some of the branches we work with remain the same, while some others are added and some others leave.

List of branches are in the List sheets (List2022, List2023 etc) range K5:K38

What I'd like, if possible, is to have a copy of all branches in the Stats sheet column A, so that I can use a vlookup formula to fetch data.

Hope I made my self clear ! 🫣
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
You are more likely to get a response if you help the helper. Trying to imagine your workbook structure is not likely, for me at least.

If possible post a version of the workbook with fake but realistic data. Do that using the link icon above the message area. Put your workbook on Dropbox or 1 drive, Google drive, etc. Maybe try again to describe what is needed.

It sounds like you want a "new worksheet" named Stats that has a list of all "branches" listed in all worksheets whose name includes the
word List?
 
Upvote 0
You are more likely to get a response if you help the helper. Trying to imagine your workbook structure is not likely, for me at least.

If possible post a version of the workbook with fake but realistic data. Do that using the link icon above the message area. Put your workbook on Dropbox or 1 drive, Google drive, etc. Maybe try again to describe what is needed.

It sounds like you want a "new worksheet" named Stats that has a list of all "branches" listed in all worksheets whose name includes the
word List?
Hey, thanks for replying!

You got it right! There's already a worksheet named stats, where I put the branches manually (Column A). So, instead of writing any new branch on my own, I'd like the branches from sheets named List**** (K5:K38) to be listed automatically in Stats column A.

Password is 299. Stats password, if needed, is 24823.

Thank you!!
 
Upvote 0
So you want a unique list of branches -- from range K5:K38 in year-specific worksheets -- in Column A in Stats worksheet starting in cell A3?

I had a look. If the Branches might change will there always be 34? If no, then there may be more or fewer than 34
branches listed in Stats worksheet?
 
Last edited:
Upvote 0
So you want a unique list of branches -- from range K5:K38 in year-specific worksheets -- in Column A in Stats worksheet starting in cell A3?
Yes, exactly!

Branches will probably change and be more than 34, but no-one knows for sure. I guess I'll be able to make adjustments if K5:K38 range changes.
 
Upvote 0
I hope that THIS does what is needed.

VBA Code:
Option Explicit

Sub CreateListing()

'   -----------------------------------
'           Declare Variables
'   -----------------------------------

'   Worksheet object that points to the stats worksheet.
    Dim wsStats As Worksheet

'   Used to iterate through List worksheets.
    Dim wsLoop As Worksheet

'   Used to iterate through branches in a range.
    Dim rCell As Range

'   Cell that anchors data in thr Stats worksheet.
    Dim rTargetAnchorCell As Range
    
'   Range that contains branches for a specific list.
    Dim rListData As Range

'   Address where data is located in List worksheet.
    Dim sDataSourceAddress As String

'   Variable holding the name of the Stat worksheet.
    Dim sStatSheetName As String
    
'   Keep count of how many rows have been transferred to the Stats worksheet.
    Dim iBranchesTransferred As Long
    
'   Used to determine how many rows of existing data exists that requiring clearing
    Dim iRowsToClear As Long
    
'   Collection of unique branches.
    Dim colBranches As New Collection
    
'   Used to access each entry in the collection.
    Dim vItem As Variant
    
'   For iterating through each item in the collection.
    Dim iBranch As Long
    
'   -----------------------------------
'          Initialize Variables
'   -----------------------------------
        
    sStatSheetName = "Stats" '<= Change if the name of the stats sheet changes.
    
    sDataSourceAddress = "K5:K38" '<= Change if range where list data is located changes.
    
    Set wsStats = Worksheets(sStatSheetName)

    Set rTargetAnchorCell = wsStats.Range("A3") '<= Change if the upperleftmost cell in
'                                                   the stats sheet changes.

'   Initialize count of rows of List data that has been transferred.
    iBranchesTransferred = 0
    
'   -----------------------------------
'        Clear the existing Data
'   -----------------------------------

    iRowsToClear = wsStats.Cells(Rows.Count, 1).End(xlUp).Row - rTargetAnchorCell.Row + 1
    
    If iRowsToClear > 0 _
     Then
        rTargetAnchorCell.Resize(iRowsToClear, 1).Value = ""
        rTargetAnchorCell.FormatConditions.Delete
    End If

'   --------------------------------------
'       Process List Worksheets' Data
'   --------------------------------------

    For Each wsLoop In Worksheets

        If UCase(wsLoop.Name) Like "LIST*" _
         Then

            Set rListData = wsLoop.Range(sDataSourceAddress)

            For Each rCell In rListData
            
                On Error Resume Next
                colBranches.Add rCell.Value, rCell.Value
                On Error GoTo 0
            
            Next rCell
'
        End If

    Next wsLoop

'   ------------------------------------
'       Transfer Branches to Target
'   ------------------------------------

     For Each vItem In colBranches
    
        iBranch = iBranch + 1
    
        rTargetAnchorCell.Cells(iBranch).Value = vItem
     
     Next

'   -----------------------------------
'       Format Transferred Data
'   -----------------------------------

    With rTargetAnchorCell.Resize(iBranch)

        .FormatConditions.Add Type:=xlExpression, Formula1:="=MOD(ROW(),2)=1"
        .FormatConditions(.FormatConditions.Count).SetFirstPriority

        With .FormatConditions(1).Interior
            .Pattern = xlGray16
            .PatternThemeColor = xlThemeColorDark1
            .ColorIndex = xlAutomatic
            .PatternTintAndShade = -0.349986266670736
        End With

    End With

End Sub
 
Upvote 0
Solution
I hope that THIS does what is needed.

VBA Code:
Option Explicit

Sub CreateListing()

'   -----------------------------------
'           Declare Variables
'   -----------------------------------

'   Worksheet object that points to the stats worksheet.
    Dim wsStats As Worksheet

'   Used to iterate through List worksheets.
    Dim wsLoop As Worksheet

'   Used to iterate through branches in a range.
    Dim rCell As Range

'   Cell that anchors data in thr Stats worksheet.
    Dim rTargetAnchorCell As Range
   
'   Range that contains branches for a specific list.
    Dim rListData As Range

'   Address where data is located in List worksheet.
    Dim sDataSourceAddress As String

'   Variable holding the name of the Stat worksheet.
    Dim sStatSheetName As String
   
'   Keep count of how many rows have been transferred to the Stats worksheet.
    Dim iBranchesTransferred As Long
   
'   Used to determine how many rows of existing data exists that requiring clearing
    Dim iRowsToClear As Long
   
'   Collection of unique branches.
    Dim colBranches As New Collection
   
'   Used to access each entry in the collection.
    Dim vItem As Variant
   
'   For iterating through each item in the collection.
    Dim iBranch As Long
   
'   -----------------------------------
'          Initialize Variables
'   -----------------------------------
       
    sStatSheetName = "Stats" '<= Change if the name of the stats sheet changes.
   
    sDataSourceAddress = "K5:K38" '<= Change if range where list data is located changes.
   
    Set wsStats = Worksheets(sStatSheetName)

    Set rTargetAnchorCell = wsStats.Range("A3") '<= Change if the upperleftmost cell in
'                                                   the stats sheet changes.

'   Initialize count of rows of List data that has been transferred.
    iBranchesTransferred = 0
   
'   -----------------------------------
'        Clear the existing Data
'   -----------------------------------

    iRowsToClear = wsStats.Cells(Rows.Count, 1).End(xlUp).Row - rTargetAnchorCell.Row + 1
   
    If iRowsToClear > 0 _
     Then
        rTargetAnchorCell.Resize(iRowsToClear, 1).Value = ""
        rTargetAnchorCell.FormatConditions.Delete
    End If

'   --------------------------------------
'       Process List Worksheets' Data
'   --------------------------------------

    For Each wsLoop In Worksheets

        If UCase(wsLoop.Name) Like "LIST*" _
         Then

            Set rListData = wsLoop.Range(sDataSourceAddress)

            For Each rCell In rListData
           
                On Error Resume Next
                colBranches.Add rCell.Value, rCell.Value
                On Error GoTo 0
           
            Next rCell
'
        End If

    Next wsLoop

'   ------------------------------------
'       Transfer Branches to Target
'   ------------------------------------

     For Each vItem In colBranches
   
        iBranch = iBranch + 1
   
        rTargetAnchorCell.Cells(iBranch).Value = vItem
    
     Next

'   -----------------------------------
'       Format Transferred Data
'   -----------------------------------

    With rTargetAnchorCell.Resize(iBranch)

        .FormatConditions.Add Type:=xlExpression, Formula1:="=MOD(ROW(),2)=1"
        .FormatConditions(.FormatConditions.Count).SetFirstPriority

        With .FormatConditions(1).Interior
            .Pattern = xlGray16
            .PatternThemeColor = xlThemeColorDark1
            .ColorIndex = xlAutomatic
            .PatternTintAndShade = -0.349986266670736
        End With

    End With

End Sub

It generates the list, but it also gives me a runtime error

1687863428417.png
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,325
Members
452,635
Latest member
laura12345

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