+ Reply to Thread
Results 1 to 19 of 19

Filtering a Variant

  1. #1
    Registered User
    Join Date
    07-15-2009
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2003
    Posts
    10

    Filtering a Variant

    Hi,

    Been a while since I last coded in VBA, so appreciate some help. I'm trying to filter a variant that has data coming from the database. Simplified example of the code as below:

    Please Login or Register  to view this content.
    The data will look as below:
    date state population
    1-Jan-21 State A 5
    2-Jan-21 State B 7
    2-Jan-21 State C 8

    In the example data above, I would like the code to filter for 2-Jan-21 and display the results on the Excel sheet.

    Appreciate any help on how I can filter the variant vTest to do so.

    Thank you.

  2. #2
    Forum Expert XLent's Avatar
    Join Date
    10-13-2010
    Location
    Northumberland, UK
    MS-Off Ver
    various
    Posts
    2,704

    Re: Filtering a Variant

    Hi, it's been a long time since I did anything with ADO but, if only to get the ball rolling...

    rather than filtering the resulting Variant could you, instead, apply a Filter to the Recordset itself ?

    see the (VB) "Filter Property Example": https://docs.oracle.com/cd/E19531-01...11_ADO115.html

  3. #3
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    365 32bit Win
    Posts
    1,908

    Re: Filtering a Variant

    Maybe specify a date via a parameter of the GetDataFunction, then copy the recordset to Excel using the CopyFromRecordset method?

  4. #4
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,168

    Re: Filtering a Variant

    I would like the code to filter for 2-Jan-21
    Why not just ...
    Please Login or Register  to view this content.
    Or am i missing something...
    Good Luck
    I don't presume to know what I am doing, however, just like you, I too started somewhere...
    One-day, One-problem at a time!!!
    If you feel I have helped, please click on the star to left of post [Add Reputation]
    Also....add a comment if you like!!!!
    And remember...Mark Thread as Solved.
    Excel Forum Rocks!!!

  5. #5
    Registered User
    Join Date
    07-15-2009
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Filtering a Variant

    Thanks XLent & ByteMarks. The original code that I have, which I didn't post, is doing something similar, I believe. In the original code, I have the date passed through the SQL query to get only data for the particular date. However, as I'm looping through a large dataset with a lot of dates, the function is calling the SQL database too often. I think it's slowing down the entire process. Hence, I'm looking to pass a large part of the data into a variant and then loop the dates and get the data. Hopefully this will be faster.

    Quote Originally Posted by sintek View Post
    Why not just ...
    Please Login or Register  to view this content.
    Or am i missing something...
    Mmmm... might be what I am looking for. Let me have a crack at this. Thanks.

  6. #6
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,168

    Re: Filtering a Variant

    However, as I'm looping through a large dataset with a lot of dates
    Why not upload a sample file representing actual so that a more simplistic solution can be offered...
    Also ... is it just to filter the data or to extract the filtered records to another sheet

  7. #7
    Registered User
    Join Date
    07-15-2009
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Filtering a Variant

    Quote Originally Posted by sintek View Post
    Why not upload a sample file representing actual so that a more simplistic solution can be offered...
    Also ... is it just to filter the data or to extract the filtered records to another sheet
    Ah yes, good idea. Please see attached sample file. It has half hourly data going for a number of days. Data are all from a SQL database. Currently, I've an SQL query that filtered data for each half hour. This is basically done by looping the start to end date & time but as explained before, this means a lot of trip to the database.

    Instead of making multiple trips to the database, I'm hoping to query all the needed data into an array, then extract the filter the data for a specified date & time to another sheet.

    Say, for example, I want to display the data for 1-Jan-21 01:00, I would like it to show on Sheet1 as below:

    A 22
    B 35
    C 56
    Attached Files Attached Files

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

    Re: Filtering a Variant

    Have you tried something like
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    07-15-2009
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Filtering a Variant

    Quote Originally Posted by jindon View Post
    Have you tried something like
    Please Login or Register  to view this content.
    Yes, that is what my current code, that I've not posted is doing. As mentioned, it's making too many trips to the database and I think is a reason for its slower performance.

  10. #10
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,168

    Re: Filtering a Variant

    Please Login or Register  to view this content.

  11. #11
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,903

    Re: Filtering a Variant

    Open the recordset once with all the data, then you can filter it as needed and use CopyFromRecordset to put the filtered data onto the worksheet.
    Rory

  12. #12
    Forum Expert sintek's Avatar
    Join Date
    12-04-2015
    Location
    Cape Town
    MS-Off Ver
    2013 | 2016 | 2019
    Posts
    13,168

    Re: Filtering a Variant

    So why not just store data into array and extract from that to other sheet?

  13. #13
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    365 32bit Win
    Posts
    1,908

    Re: Filtering a Variant

    Something like this which will take a start and end date and filter for every 30 min interval and append the results?

    Please Login or Register  to view this content.

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

    Re: Filtering a Variant

    Why not just Open csv by sequential access?

    e.g
    Please Login or Register  to view this content.

  15. #15
    Registered User
    Join Date
    07-15-2009
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Filtering a Variant

    Quote Originally Posted by ByteMarks View Post
    Something like this which will take a start and end date and filter for every 30 min interval and append the results?

    Please Login or Register  to view this content.
    Sorry, silly question... if I'm pulling data via a function, how do I assign it to another recordset and do steps that you've outlined? In my example, I've the data coming from GetDataFunction. If I replace your code with GetDataFunction as below, it still looks to be calling the function & SQL query for each loop. I am missing something but too rusty on VBA to think through it atm.

    Please Login or Register  to view this content.

  16. #16
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    365 32bit Win
    Posts
    1,908

    Re: Filtering a Variant

    I didn't use GetDataFunction. I just got one recordset at the start of Test containing all the data, then looped through the time intervals and filtered that recordset for the results.
    So the database is queried once, then filters applied subsequently.

  17. #17
    Registered User
    Join Date
    07-15-2009
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Filtering a Variant

    Quote Originally Posted by ByteMarks View Post
    I didn't use GetDataFunction. I just got one recordset at the start of Test containing all the data, then looped through the time intervals and filtered that recordset for the results.
    So the database is queried once, then filters applied subsequently.
    Thanks ByteMarks. I was wondering how I could do it if the data comes from a function. My code in the spreadsheet has multiple functions pulling different data from the database.

  18. #18
    Forum Expert
    Join Date
    07-23-2018
    Location
    UK
    MS-Off Ver
    365 32bit Win
    Posts
    1,908

    Re: Filtering a Variant

    You could clone the recordset returned by GetDataFunction. But that function creates and terminates the connection each time it's called.

    Please Login or Register  to view this content.

  19. #19
    Registered User
    Join Date
    07-15-2009
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2003
    Posts
    10

    Re: Filtering a Variant

    Quote Originally Posted by ByteMarks View Post
    You could clone the recordset returned by GetDataFunction. But that function creates and terminates the connection each time it's called.

    Please Login or Register  to view this content.
    Thank you, appreciate the help.

+ 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. Use of variant
    By miles44 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-31-2020, 09:08 AM
  2. DIM variant
    By miles44 in forum Excel General
    Replies: 2
    Last Post: 02-24-2017, 03:00 PM
  3. [SOLVED] A variable defined as variant or variant array
    By billj in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 11-14-2016, 05:35 PM
  4. Does setting a variant acting as an array equal to "" accomplish same as "Erase" variant?
    By dmasters4919 in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 11-21-2014, 05:59 PM
  5. Using a variant in forumla... help please
    By KATIEexcel in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 05-16-2013, 03:48 AM
  6. Passing value of variant
    By Andrew in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 05-21-2006, 10:20 PM
  7. Variant
    By owl527 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 10-14-2005, 05:05 PM

Tags for this Thread

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