+ Reply to Thread
Results 1 to 3 of 3

Help with 2 dimensional Array of table for range intersect etc

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    08-21-2008
    Location
    Hamilton, New Zealand
    MS-Off Ver
    Office 2007
    Posts
    255

    Help with 2 dimensional Array of table for range intersect etc

    My understanding of arrays is poor.
    In basic terms my requirement is as follows -
    I have a table of data (refer Sheet(“Data”) of attached example.
    Row 1 of this table is the default values of the Data Template (this row will be hidden to the user inputting the data).
    The user will populate data in the data template (via cell validation drop-down selection etc).
    The actual data template will have approx 160 columns, by 1 – 5000 rows approx.
    The data template will contain hidden columns (fields) that do not relate to the specific task scenario (all shown here, but actual file will have various columns not applicable to the scenario hidden). So In reality approx 6-50 columns will be unhidden for data entry etc.

    Each column is assigned a named range to identify it in row 2
    eg. Cell A2 (Status) named range is “Status_Col”
    Cell D2 (Model) named range is “Data_Col_3”

    Each named range (from Col D onwards) is referenced with an “Owner”.
    Refer Sheet(“Assignment”). Where each column title (Col A) has a named range listed in Col B, with the related Owner in Col C.

    Each Owner is assigned an email address (refer Sheet(“Email Addr”)
    The plan is to have these owner/email addresses declared as Constants in VBA
    Eg. strEmailEngineering = [email protected]
    Approx 10 owners will exist (5 only exampled here)

    What I require is to loop through all cells within named range = “Dataset” (D$:J9 in this example) and in an array store the range for a given owner of all cells where cell value in given (visible) column differs from the default value for that column where “Status” column value = “Include”.
    So this example the array would identify the following –
    Owner (1) = Human Resources Range (1) = E4, E6
    Owner (2) = Engineering Range (2) = F4, J4, F6
    Owner (3) = Health & Safety Range (3) = H4, H5

    Owner = Laboratory would be omitted from the array as no cells values for that owner column (“HP”) differ from the default value (“Select”)

    With the Array I then want to loop through each Owner captured and display only columns and rows applicable (that intersect with the Owner range (as per array for that owner).
    So displaying only the applicable rows with Status = “Include” with columns for that Owner where at least 1 row value differs from the default.
    And repeat for all owners captured.
    I am looking for a fast solution so, if it is quicker to read the dataset into an array, then process as above to achieve the same result all the better.

    I will then email the workbook to that Owner (as per email address for that Owner)
    There is a lot more to it than stated here, but I think I can manage the rest.
    Your help is much appreciated.
    Attached Files Attached Files

  2. #2
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Help with 2 dimensional Array of table for range intersect etc

    First off, an excellent, thoroughly detailed and coherent question. Often the greatest challenge of all is to get a complex scenario across in the first instance - I feel you achieved that here...

    The below will I *think* get you close to what you want but it's highly unlikely to do everything given this was a dummy file and the real version is likely to be far more complex... anyway I hope it helps to some extent.

    Public Sub Demo()
    Dim wsData As Worksheet, wsTitle As Worksheet, wsOwners As Worksheet
    Dim xlCalc As XlCalculation
    Dim rngData As Range, rngOwners As Range, rngOwner As Range, rngTitles As Range, rngTitle As Range
    Dim rngX As Range, rngResult As Range, rngVisible As Range, rngCell As Range
    Dim strRows As String
    With Application
        xlCalc = .Calculation
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
        .EnableEvents = False
    End With
    Set wsData = Sheets("Data")
    Set wsTitle = Sheets("Assignment")
    Set wsOwners = Sheets("Email Addr")
    Set rngData = Range("dataset")
    Range("Status_Col").Resize(1 + rngData.Rows.Count, rngData.Columns.Count).AutoFilter field:=1, Criteria1:="Include"
    With wsTitle: Set rngTitles = .Range(.Cells(2, "A"), .Cells(.Rows.Count, "A").End(xlUp)): End With
    With wsOwners: Set rngOwners = .Range(.Cells(2, "A"), .Cells(.Rows.Count, "A").End(xlUp)): End With
    For Each rngOwner In rngOwners.Cells
        strRows = ""
        If rngOwner.Value <> "" Then
            With wsTitle: rngTitles.Offset(-1).Resize(1 + rngTitles.Rows.Count, 3).AutoFilter field:=3, Criteria1:=rngOwner.Value: End With
                For Each rngTitle In rngTitles.SpecialCells(xlCellTypeVisible)
                    If rngTitle.Value <> "" Then
                        Set rngX = Range(rngTitle.Offset(, 1).Value).Offset(1).Resize(rngData.Rows.Count)
                        With wsData
                            With rngX.SpecialCells(xlCellTypeVisible).Offset(, .Columns.Count - rngX.Column)
                                .FormulaR1C1 = "=IF(RC" & rngX.Column & "<>R" & rngX.Offset(-2).Row & "C" & rngX.Column & ",ADDRESS(ROW()," & rngX.Column & "),0)"
                                On Error Resume Next
                                For Each rngResult In .Cells.SpecialCells(xlCellTypeFormulas, xlTextValues)
                                    strRows = strRows & "," & rngResult.Value
                                Next rngResult
                                On Error GoTo 0
                                .Clear
                            End With
                        End With
                        Set rngX = Nothing
                    End If
                Next rngTitle
            With wsTitle: rngTitles.Rows(1).AutoFilter: End With
        End If
        If strRows <> "" Then
            Set rngVisible = wsData.Range(Replace(strRows, ",", "", 1, 1))
            rngData.Rows.Hidden = True: rngData.Columns.Hidden = True
            For Each rngCell In rngVisible.Cells
                rngCell.EntireColumn.Hidden = False: rngCell.EntireRow.Hidden = False
            Next rngCell
            'copy results to new file
            wsData.UsedRange.Cells.SpecialCells(xlCellTypeVisible).Copy
            Workbooks.Add
            With ActiveSheet.Range("A1")
                .PasteSpecial xlPasteColumnWidths
                .PasteSpecial xlPasteValues
                .PasteSpecial xlPasteFormats
            End With
            'issue new file to owner
            With ActiveWorkbook
                .SendMail Recipients:=rngOwner.Offset(, 1).Value
                .Close False
            End With
            Set rngVisible = Nothing
            rngData.Columns.Hidden = False
            Range("Status_Col").Resize(1 + rngData.Rows.Count, rngData.Columns.Count).AutoFilter field:=1, Criteria1:="Include"
        End If
    Next rngOwner
    rngData.Offset(-1).Resize(1).AutoFilter
    ExitPoint:
    Set wsOwners = Nothing
    Set wsTitle = Nothing
    Set wsData = Nothing
    With Application
        .Calculation = xlCalc
        .ScreenUpdating = True
        .EnableEvents = True
    End With
    Exit Sub
    
    Handler:
    MsgBox "Error Has Occurred" & vbLf & vbLf & _
            "Error Number: " & Err.Number & vbLf & vbLf & _
            "Error Desc.: " & Err.Description, _
            vbCritical, _
            "Fatal Error"
    Resume ExitPoint
    
    End Sub

  3. #3
    Forum Contributor
    Join Date
    08-21-2008
    Location
    Hamilton, New Zealand
    MS-Off Ver
    Office 2007
    Posts
    255

    Re: Help with 2 dimensional Array of table for range intersect etc

    That gets me well down the path & plenty of ideas.
    Some of which i dont yet fully understand, but will as I adapt it for my working file.
    Thanks Donkeyote.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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