Consolidation Function

MS_Xsmell

Board Regular
Joined
Jan 27, 2007
Messages
76
Office Version
  1. 365
Platform
  1. Windows
Hello friends, I am using the consolidation function in excel to collect data from a number of worksheets. The code currently consolidates data on all tabs named 0,1,2,3,4,5,...."tab count" ("tabcount" representing the last tab name (currently at 50)).

I would like to update my code to only consolidate specific tabs in the range. In this case, I want to consolidate all tabs related to "Company 1" in the table below.
snip.JPG


My current code follows:
Excel Formula:
Sub ConsolidateTabs()
 
Sheets("consl").Select

'Clear
    Sheets("Consl").Range("a1").Select
    Rows("1:165").Select
    Selection.ClearContents
    Sheets("Consl").Range("d10").Select

'Consolidated Sheets
  Dim i As Long, arr() As Variant

  ReDim arr(0 To Range("tabcount"))
  For i = 0 To UBound(arr)
    arr(i) = "'" & i & "'!R10C4:R160C240"
  Next
  Selection.Consolidate arr, xlSum, True, True, False


End Sub

Thank you!
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
An update of your code like this could work. Note the use of a new procedure which is calling the updated one.
VBA Code:
Sub ConsolidateByCompany()

    Call ConsolidateTabs(argWs:=Sheets("SheetWithTablePerAttachedImage"), _
                         argCy:="Company1")
End Sub


Sub ConsolidateTabs(ByRef argWs As Worksheet, ByVal argCy As String)
 
    Sheets("consl").Select

'Clear
    Sheets("Consl").Range("a1").Select
    Rows("1:165").Select
    Selection.ClearContents
    Sheets("Consl").Range("d10").Select

'Consolidated Sheets
    Dim i As Long, arr() As Variant

    Dim arrCy() As Variant
    Dim c       As Range
    With argWs
        For Each c In .Range("C3", .Cells(.Rows.Count, "C").End(xlUp))
            If StrComp(c.Value, argCy, vbTextCompare) = 0 Then
                ReDim Preserve arrCy(i)
                arrCy(i) = c.Offset(, -1).Value
                i = i + 1
            End If
        Next c
    End With

    ReDim arr(0 To UBound(arrCy))
    For i = 0 To UBound(arr)
        arr(i) = "'" & arrCy(i) & "'!R10C4:R160C240"
    Next
    Selection.Consolidate arr, xlSum, True, True, False
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,672
Messages
6,126,134
Members
449,294
Latest member
Jitesh_Sharma

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