0

I have an array containing values in Sheet1.Range(A1:A10).

After I open my recordset I'd like to filter the data based on values in my array. Is this possible at all?

Thanks

2
  • Wouldn't it be easier to restrict the original recordset using a WHERE clause - or do you need the other data too? Commented Jun 16, 2014 at 10:36
  • Yes, I've been trying this but not sure how to attach the variant to the end of the WHERE. I need to code something like "WHERE transaction IN [myarray]" Commented Jun 16, 2014 at 10:39

1 Answer 1

1

This is too long for a comment, so here are a couple of functions I use (this assumes strings as the criteria):

Function GetStringInList(rngCriteria As Range) As String
   Dim rngCell           As Range
   Dim strTemp           As String
   For Each rngCell In rngCriteria.Cells
      If Len(rngCell.Value) > 0 Then strTemp = strTemp & ",'" & DoubleQuotes(rngCell.Value) & "'"
   Next rngCell
   ' now strip off leading comma
   GetStringInList = Mid$(strTemp, 2)
End Function
Function DoubleQuotes(strIn As String) As String
   DoubleQuotes = Replace(strIn, "'", "''")
End Function

so when building the SQL you could use something like:

strSQL = strSQL & " WHERE [FieldName] In " & GetStringInList(Range("A1:A10"))
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Rory, I figured it out in the end. I used a for...next to convert by array to csv then included that in my WHERE. Very useful trick indeed! Thanks
That's what that function does. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.