Domanda

I'm having some trouble trying to make a function that acts like vlookup but returns a range rather than a cell

The data to search through looks like this

lookup

Sometimes there is a space separating sometimes not What I would like to do is to look up 16 from my main page and return all the values in that range.

main

the code I currently am using will only return the first line in a messagebox

Public Function findrulepos(target) As Range    
    Dim ruleStart, ruleEnd, ruleEnd2 As String       
    Dim RuleRange As Range
    'Dim ruleEnd As Range

    MaxRule = 100000
    MaxRow = 100000

    Set target = Sheets("main").Range("E2")

    Sheets("ResRules").Select

    For i = 3 To MaxRow
        If CStr(ThisWorkbook.Sheets("ResRules").Range("A" & i).Value) = _
        CStr(target.Value) Then
            ruleStart = _
            ThisWorkbook.Sheets("ResRules").Range("A" & i).Offset(0, 1).Text

            Exit For
         Else
         End If
    Next i
End Function
È stato utile?

Soluzione

If we can assume that the numeric group labels in col A are sequential, then I think this will achieve what you need:

  1. Enter the numeric label you are wanting to extract (16 in your example) into cell E1
  2. Note that =MATCH(E1,A:A,0) gives us the row number where group 16 starts, which is the first row we want to copy. Similarly, =MATCH(E1+1,A:A,0) gives us the row number where group 17 starts, which is one row below the last row we want to copy. (Unfortunately that's not true for the very last group of code, but to rectify that you just need to add a dummy number at the very bottom of the data in col A.)
  3. Enter the formula =IF(ROW()+MATCH(E$1,A:A,0)-1<MATCH(E$1+1,A:A,0),INDIRECT("B"&MATCH(E$1,A:A,0)+ROW()-1),"") in F1. That should copy the first value of the selected code block -- [DO] ADD TO QUEUE P in your example.
  4. Copy F1 down as many rows as the largest code block is likely to be.
  5. The one problem with that is that it will put 0 whenever it copies a blank row. So you have to explicitly check for that case, e.g. by changing the formula in F1 to =IF(ROW()+MATCH(E$1,A:A,0)-1<MATCH(E$1+1,A:A,0),IF(ISBLANK(INDIRECT("B"&MATCH(E$1,A:A,0)+ROW()-1)),"",INDIRECT("B"&MATCH(E$1,A:A,0)+ROW()-1)),"")
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top