+ Reply to Thread
Results 1 to 5 of 5

Save dynamic ranges into Global array and return combined ranges

Hybrid 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.

  2. #2
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,719

    Re: Save dynamic ranges into Global array and return combined ranges

    Does it need to be Global ?
    Global myArray
    
    Sub test()
        With Range("e1").CurrentRegion
            myArray = Application.Index(Evaluate("if(" & .Address & "<>0," & .Address & ","""")"), _
                Evaluate("row(2:" & .Rows.Count & ")"), [{1,2,3,4,5,7,8,9}])
        End With
        With [ac3].CurrentRegion
            .Offset(2).ClearContents
            .Rows(3).Resize(UBound(myArray, 1), UBound(myArray, 2)) = myArray
        End With
    End Sub

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

    Re: Save dynamic ranges into Global array and return combined ranges

    Hi jindon

    Thanks for your reply. The code works, however, the output data gets overrided when I use the code for the second time.

    Im not sure it needs to be Global. Maybe I did not explain myself correctly.
    What I mean is that the populated cells in the array needs to stay everytime I use the sub-routine and be resized to fit the next range I wanna save. And only when I am ready to output, the saved ranges are returned to column AC:AJ.

    As explained, I would like to filter on my data using cell A4 and A6. Once filtered, I would like to save my range. This will be done multiple of times.


    Hope this makes sence.

  4. #4
    Forum Guru
    Join Date
    08-15-2004
    Location
    Tokyo, Japan
    MS-Off Ver
    2013 O.365
    Posts
    22,719

    Re: Save dynamic ranges into Global array and return combined ranges

    Do you want it directly to output?
    Option Explicit
    
    Global myArray
    
    Sub test_Your_Formula()
        Dim x As Long
        With Sheets("sheet3")
            myArray = .[FILTER($O$2:$W$47761,($U$2:$U$47761 > 0)*($U$2:$U$47761 < $A$6),"No Combo")]
            With .Range("ac" & Rows.Count).End(xlUp)(2)
                If IsArray(myArray) Then
                    On Error Resume Next
                    x = UBound(myArray, 2)
                    On Error GoTo 0
                    If x = 0 Then
                        .Resize(, UBound(myArray) - 1) = Application.Index(myArray, [{1,2,3,4,5,7,8,9}])
                    Else
                        .Resize(UBound(myArray), UBound(myArray, 2)) = Application.Index(myArray, _
                            Evaluate("row(1:" & UBound(myArray) & ")"), [{1,2,3,4,5,7,8,9}])
                    End If
                Else
                    .Value = myArray
                End If
            End With
        End With
    End Sub
    
    
    Sub testHow_I_Do()
        Dim x
        With Sheets("sheet3")
            With .Range("o1").CurrentRegion
                x = .Columns(7).Address
                x = Filter(.Parent.Evaluate("transpose(if((" & x & ">0)*(" & x & "<a6),row(" & x & ")))"), False, 0)
                If UBound(x) > -1 Then myArray = Application.Index(.Value, Application.Transpose(x), [{1,2,3,4,5,7,8,9}])
            End With
            With .Range("ac" & Rows.Count).End(xlUp)(2)
                If UBound(x) > -1 Then
                    If UBound(x) > 0 Then
                        .Resize(UBound(myArray, 1), UBound(myArray, 2)) = myArray
                    Else
                        .Resize(, UBound(myArray)) = myArray
                    End If
                Else
                    .Value = "No Combo"
                End If
            End With
        End With
    End Sub

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

    Re: Save dynamic ranges into Global array and return combined ranges

    Thanks, jindon. This is really good stuff. Both subroutines are working.

    Have a nice weekend!

+ Reply to Thread

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