Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default filter with vb code

Please forgive my lack of brain functions today.

This may sound silly, but Ive been working with access for weeks and cant
wrap my brain around this problem in excel.

Its rather straight forward.
I have a large table some 19000 records.
Each record has 18 columns.
I want to select all records with a value from column # 12 that is equal to
variable €œA1€.
Then take all matching records and copy to a new workbook and save.
Then do again with different value.

Dan

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 71
Default filter with vb code

Hello Dan,

Speaking of Access, using ADO might make some sense with such a quandary.
Here's an example that creates a new workbook in c:\temp\ named Book1.xls:

Sub ExToEx()
Dim cn As Object
Set cn = CreateObject("ADODB.Connection")
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _
ThisWorkbook.FullName & ";Extended Properties=Excel 8.0;"
cn.Execute "SELECT Col1, Col2 INTO [Excel
8.0;Database=c:\temp\book1.xls].[Sheet1]" _
& " FROM [Sheet1$a:b] Where Col2 = " & Sheets(2).Range("a1").Value
cn.Close: Set cn = Nothing
End Sub

Where Col1 and Col2 are column headers located in row 1 of Sheet1, and the
variable is located in the 2nd Sheet, A1. The workbook that your running this
from must be saved so that a proper connection can be established.

More info can be found on such an approach he
http://support.microsoft.com/kb/295646/EN-US/

Regards,
Nate Oliver
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 16
Default filter with vb code

I need to keep this in excel if I can.

Any ideas?
Let me know.

Dan


"Nate Oliver" wrote:

Hello Dan,

Speaking of Access, using ADO might make some sense with such a quandary.
Here's an example that creates a new workbook in c:\temp\ named Book1.xls:

Sub ExToEx()
Dim cn As Object
Set cn = CreateObject("ADODB.Connection")
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _
ThisWorkbook.FullName & ";Extended Properties=Excel 8.0;"
cn.Execute "SELECT Col1, Col2 INTO [Excel
8.0;Database=c:\temp\book1.xls].[Sheet1]" _
& " FROM [Sheet1$a:b] Where Col2 = " & Sheets(2).Range("a1").Value
cn.Close: Set cn = Nothing
End Sub

Where Col1 and Col2 are column headers located in row 1 of Sheet1, and the
variable is located in the 2nd Sheet, A1. The workbook that your running this
from must be saved so that a proper connection can be established.

More info can be found on such an approach he
http://support.microsoft.com/kb/295646/EN-US/

Regards,
Nate Oliver

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default filter with vb code

You can filter data to a new location with Data=Filter=Advanced filter.
I assume A1 was representative. If you need code, turn on the macro
recorder while you do it manually - then turn off the recorder. This
records a single command which should be easy to modify to take variable
arguments and place into a loop.

--
Regards,
Tom Ogilvy

"wtpcomplab" wrote in message
...
I need to keep this in excel if I can.

Any ideas?
Let me know.

Dan


"Nate Oliver" wrote:

Hello Dan,

Speaking of Access, using ADO might make some sense with such a

quandary.
Here's an example that creates a new workbook in c:\temp\ named

Book1.xls:

Sub ExToEx()
Dim cn As Object
Set cn = CreateObject("ADODB.Connection")
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _
ThisWorkbook.FullName & ";Extended Properties=Excel 8.0;"
cn.Execute "SELECT Col1, Col2 INTO [Excel
8.0;Database=c:\temp\book1.xls].[Sheet1]" _
& " FROM [Sheet1$a:b] Where Col2 = " & Sheets(2).Range("a1").Value
cn.Close: Set cn = Nothing
End Sub

Where Col1 and Col2 are column headers located in row 1 of Sheet1, and

the
variable is located in the 2nd Sheet, A1. The workbook that your running

this
from must be saved so that a proper connection can be established.

More info can be found on such an approach he
http://support.microsoft.com/kb/295646/EN-US/

Regards,
Nate Oliver



  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 71
Default filter with vb code

"wtpcomplab" wrote:
I need to keep this in excel if I can.


Right, this is all in Excel. Excel can act as a Jet DB as well. I wrote the
code in Excel for Excel and even tested it. One potential issue is that your
data table would really need to look like a DB.

Tom's suggestion is more straightforward although I'm not sure about
looping. You could pass the copied, filtered data to a new workbook, save and
close.

Bon chance,
Nate Oliver


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default filter with vb code

Looping would loop through a list of unique items (also can be built with
advanced filter) and would put each of the items in this list as criteria in
the criteria cell/range, adjust the destination (which would be a variable
in the advanced filter command), then execute the filter. Perform any
remaining administration.

I have successfully used this many times as I am sure you have yourself. So
apparently my suggestion was not clear or was vague as to what I meant.
Thanks for pointing that out.

--
Regards,
Tom Ogilvy


"Nate Oliver" wrote in message
...
"wtpcomplab" wrote:
I need to keep this in excel if I can.


Right, this is all in Excel. Excel can act as a Jet DB as well. I wrote

the
code in Excel for Excel and even tested it. One potential issue is that

your
data table would really need to look like a DB.

Tom's suggestion is more straightforward although I'm not sure about
looping. You could pass the copied, filtered data to a new workbook, save

and
close.

Bon chance,
Nate Oliver



  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 71
Default filter with vb code

Tom, I apologize, I missed the following in the original post:

Then do again with different value.


Which is exactly where looping comes into play as you have mentioned. Thank
you for following up.

Regards,
Nate Oliver
  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 593
Default filter with vb code


Nate Oliver wrote:
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _
ThisWorkbook.FullName & ";Extended Properties=Excel 8.0;"


More info on why this is a bad idea can be found he

BUG: Memory leak occurs when you query an open Excel worksheet by using
ActiveX Data Objects (ADO)

http://support.microsoft.com/default...;en-us;Q319998

Jamie.

--

  #9   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 71
Default filter with vb code

Interesting,

Note they're speaking to querying an open Excel file. One of the benefits of
using ADO to query a spreadsheet is that the file can (and apparently should)
be closed.

Not to mention that ADO is extremely fast.

Cheers,
Nate

"Jamie Collins" wrote:


Nate Oliver wrote:
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _
ThisWorkbook.FullName & ";Extended Properties=Excel 8.0;"


More info on why this is a bad idea can be found he

BUG: Memory leak occurs when you query an open Excel worksheet by using
ActiveX Data Objects (ADO)

http://support.microsoft.com/default...;en-us;Q319998

Jamie.

--


  #10   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 593
Default filter with vb code

Nate Oliver wrote:
Note they're speaking to querying an open Excel file.


*You* were suggesting to the OP to query an open Excel file by using
ThisWorkbook.FullName in the connection string.

Jamie.

--



  #11   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 71
Default filter with vb code

Hello Jamie,

Where's the ADO Recordset in my post?

Quote by MS:
"When you retrieve a Microsoft ActiveX Data Objects (ADO) Recordset from an
Excel worksheet that is open in Excel, a memory leak occurs in the Excel
process. Repeated queries may eventually cause Excel to run out of memory and
raise an error, or cause Excel to stop responding."

Workaround by MS:
"Use the SELECT INTO syntax of the Jet OLE DB Provider to export the Excel
data to a new worksheet. For additional information about using the SELECT
INTO syntax to export data, click the following article number to view the
article in the Microsoft Knowledge Base:
295646 How To Transfer Data from ADO Data Source to Excel with ADO"

My Original Post:
Sub ExToEx()
Dim cn As Object
Set cn = CreateObject("ADODB.Connection")
cn.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _
ThisWorkbook.FullName & ";Extended Properties=Excel 8.0;"
cn.Execute "SELECT Col1, Col2 INTO [Excel
8.0;Database=c:\temp\book1.xls].[Sheet1]" _
& " FROM [Sheet1$a:b] Where Col2 = " & Sheets(2).Range("a1").Value
cn.Close: Set cn = Nothing
End Sub

Look familiar?

Regards,
Nate Oliver
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Filter isn't working with a VBA Code [email protected] Excel Worksheet Functions 8 June 15th 06 01:20 PM
vba code to filter rbekka33[_3_] Excel Programming 5 September 14th 04 09:22 AM
filter code help scrabtree[_2_] Excel Programming 2 August 1st 04 10:09 PM
Filter Code Ray Kanner[_2_] Excel Programming 4 April 29th 04 02:53 PM
vba filter code Gabriel[_3_] Excel Programming 3 February 22nd 04 06:24 PM


All times are GMT +1. The time now is 07:16 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"