Results 1 to 5 of 5

Save dynamic ranges into Global array and return combined ranges

Threaded View

  1. #1
    Registered User
    Join Date
    10-12-2022
    Location
    Copenhagen
    MS-Off Ver
    365
    Posts
    24

    Save dynamic ranges into Global array and return combined ranges

    Hi VBA geniuses


    I have a table with data that i am filtering with the (=FILTER) function.

    Each time I have applied a filter ("A4 & "A6") I would like to save that range in a Global Array, and change the filter accordingly with a different setting for a new range to save.
    Once I am done with chosing filters and populating the Array, I would like to return the whole thing (combinedRanges) in another output range.

    I have written the following code, but it is not returning the desired outcome. I might have made it too complicated, however, im looking for any help you can give me.

    Column "E:I" and "K:M" is my source range
    Column "AC:Aj" is my destination range

    CODE:

    Sub SaveRange()
        Dim lastRow As Long
        Dim combinedRange As Range
    
        lastRow = Range("E" & Rows.Count).End(xlUp).Row ' Find the last used row in column T
    
        ' Define the ranges to save
        Set combinedRange = Union(Range("E2:I" & lastRow), Range("K2:M" & lastRow))
    
        ' Check if the last used row is within the range
        If lastRow <= combinedRange.Row + combinedRange.Rows.Count - 1 Then
            currentIndex = currentIndex + 1
            ReDim Preserve savedRanges(1 To currentIndex)
            Set savedRanges(currentIndex) = combinedRange
                
            End If
        End If
    End Sub
    
    Sub ReturnRanges()
        Dim i As Integer, j As Integer
        Dim outputRange1 As Range, outputRange2 As Range
        
        Set outputRange1 = Range("AC4:AG4").Resize(UBound(savedRanges), 5)
        Set outputRange2 = Range("AH4:AJ4").Resize(UBound(savedRanges), 3)
        
        ' Loop through the savedRanges array and copy each range to the appropriate output range
        For i = 1 To UBound(savedRanges)
            For j = 1 To 5
                outputRange1(i, j).Value = savedRanges(i).Cells(1, j + 19).Value
            Next j
            outputRange2(i, 1).Value = savedRanges(i).Cells(1, 27).Value
            outputRange2(i, 2).Value = savedRanges(i).Cells(1, 28).Value
        Next i
        
        Erase savedRanges ' Clear the savedRanges array
        currentIndex = 0 ' Reset the currentIndex variable
    End Sub
    Attached Files Attached Files
    Last edited by Fishtail; 02-22-2023 at 10:12 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Dynamic chart ranges without naming lots of ranges
    By benoj2005 in forum Excel Charting & Pivots
    Replies: 7
    Last Post: 12-21-2020, 04:30 AM
  2. [SOLVED] Use 2 Different Named Cell Ranges To Return Values From 1 Of the Ranges In Formula
    By DESSTRO in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 07-20-2017, 08:37 PM
  3. Dynamic ranges in array formulas.
    By smatchymo in forum Excel Formulas & Functions
    Replies: 3
    Last Post: 01-03-2016, 07:59 AM
  4. Save Ranges in Array
    By h0ps in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-12-2014, 07:57 AM
  5. [SOLVED] VBA Code To Return The Number Of Columns Between Two Dynamic Ranges.
    By GEANZ in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-04-2013, 01:44 AM
  6. VBA Array formula for dynamic ranges
    By -nada- in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-25-2010, 07:41 AM
  7. Can you create dynamic named ranges containing array formulas?
    By Marie J-son in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-28-2006, 09:10 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1