Вопрос

For reference I have a similiar set up to: http://chandoo.org/wp/2012/11/27/extract-subset-of-data/.

So basically I have 3 sheets I'm working with. There's the RawData sheet which holds a table with you know the raw data. Then there's the master sheet which has the criteria that's going to be filtered and then a filter sheet which utilizes both to create a dropdown so that you can choose what you want to filter on the table and it's pulled to the filter sheet for viewing.

My question is thus: What direction would I be looking at so that if someone makes a change on the filter sheet it's then pushed to the rawdata sheet? I'm thinking something along the lines of

On worksheet change
    if target is not in row 1, 2, or 3,
        Search rawdata 'This is where I dont know exactly what I'd be doing 
        copy target row to rawdata row
Это было полезно?

Решение

A couple of examples of different methods below. You will of course need to change how it knows what the data range is and what keys to search on etc.

Once you have identified the relevant row, you can then make your changes to the data as required.

The below are based on some random letters in A1:B12, and it is desired to match on the combination of A and B.

'---Using Range.Find---
'The simplest method is to create a new column in Excel
'The column should use a formula to combine the different columns you want to match on together into a key
'An example would be, to match on column A and B: =A1 & "|" & B1
'You can then use Range.Search to find the match
Sub Example1()
    Dim searchRange As Range, matchedCell As Range

    Set searchRange = Worksheets("Sheet1").Columns("C")

    Set matchedCell = searchRange.Find( _
        what:="c" & "|" & "e", _
        LookIn:=XlFindLookIn.xlValues, _
        lookat:=XlLookAt.xlWhole, _
        MatchCase:=True)

    Debug.Print matchedCell.Address

End Sub

'---Using a keyed collection---
'This method is faster if you need to do the lookups a lot
'It does not need pre-generated keys in a column (unlike method 1)
'Though if you have pre-generated keys it can be optimised a bit by only loading
'the column of Keys into the array rather than all the data
Sub Example2()
    Dim dataRange As Range
    Dim arr
    Dim hashTable As Collection
    Dim i As Long

    Set dataRange = Worksheets("Sheet1").Range("A1:B12")
    arr = dataRange.Value 'load the data into an array

    Set hashTable = New Collection

    For i = LBound(arr, 1) To UBound(arr, 1)
        hashTable.Add i, arr(i, 1) & "|" & arr(i, 2) 'add the row index as the value, and the key as the lookup conditions joined together
        'arr(i,1) etc will need to change depending on how you know which columns are the columns to match on
        'if datatype matters (i.e. "1" <> 1 for your lookup) then do something like
        'Vartype(arr(i,1)) & arr(i,1) & "|" & Vartype(arr(i,2)) & arr(i,2)
    Next i

    'Now for each match you need to do, you can just find the row within dataRange by looking up in the lookupCollection
    'e.g. to find "c|e":
    Debug.Print dataRange.Rows(hashTable("c" & "|" & "e")).Address

End Sub
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top