Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Excel Autofilter
Message
From
08/03/2008 07:14:24
Cetin Basoz
Engineerica Inc.
Izmir, Turkey
 
 
To
07/03/2008 20:27:51
Ed Hizon
Skswood Sdn. Bhd
Kuala Belait, Brunei
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Environment versions
Visual FoxPro:
VFP 9 SP2
OS:
Windows XP SP2
Network:
Windows 2000 Server
Database:
Visual FoxPro
Miscellaneous
Thread ID:
01299897
Message ID:
01299927
Views:
12
>Dear all,
>
>What should be the command to programatically autofilter an excel file. I've tried to use the macro output but can't get the syntax right.
>
>Can you advise where I can get the best reference in working it out from VBA to VFP syntax.
>
>Thanks a lot.
>
>Ed

You need to install (if you haven't done already) VBA help from Office intallation CDs. Best reference is there. Then Object browser is your friend, it can create constants header file for you, show available PEM and interfaces. Intellisense helps to a point.
About understanding macro codes and translating check:
Re: Excel Subtotals - syntax in VFP Thread #608830 Message #608856

Here is another AutoFilter sample in addition to what Borislav gave you:
Local oExcel,oSheet,oDataRange
oExcel = Createobject("Excel.Application")
With oExcel
  .WorkBooks.Add
  .Visible = .T.
  oSheet = .ActiveWorkBook.ActiveSheet
  m.oSheet.Name = "Customers"
  VFP2Excel(_samples+'data\testdata.dbc', 'select * from customer', m.oSheet)

  oDataRange = oSheet.UsedRange
  * Add a dummy sheet and copy unique country values
  WITH .ActiveWorkBook.WorkSheets.Add
    .Name = 'dummysheet'
  endwith

  * "Hardcoded" value 9 - Country is 9th field
  oDataRange.Columns(9).AdvancedFilter(2,'',.ActiveWorkbook.Worksheets('dummysheet').Range('a1'),.t.)

  laVals = .ActiveWorkbook.Worksheets('dummysheet').UsedRange.Value
  * Done with dummy sheet - delete
  .DisplayAlerts = .F.
  .ActiveWorkbook.Worksheets('dummysheet').Delete

  * For each country in laVals (if not empty) create a new sheet and copy data
  FOR ix=2 TO ALEN(laVals)
    IF !EMPTY(laVals[m.ix])
      loSheet = .ActiveWorkBook.WorkSheets.Add
      loSheet.name = TRIM(laVals[m.ix])
      oDataRange.AutoFilter(9,laVals[m.ix],,,.f.)
      oSheet.UsedRange.Copy(loSheet.Range('a1'))
      loSheet.UsedRange.Columns.Autofit
    endif
  endfor
  oSheet.AutoFilterMode = .f.
  oSheet.Activate
Endwith


Function VFP2Excel
  Lparameters tcDataSource, tcSQL, toSheet
  If Type('m.toSheet.Application.Name') = 'C' And Atc('Excel',m.toSheet.Application.Name) > 0
    Local loConn As AdoDB.Connection, ;
      loRS As AdoDB.Recordset,ix
    loConn = Createobject("Adodb.connection")
    loConn.ConnectionString = "Provider=VFPOLEDB;Data Source="+m.tcDataSource
    loConn.Open()
    loRS = loConn.Execute(m.tcSQL)
    With m.toSheet
      For ix=1 To loRS.Fields.Count
        .Cells(1,m.ix).Value = Proper(loRs.Fields(m.ix-1).Name)
      Endfor
      .Range('A2').CopyFromRecordSet( loRS )
      .UsedRange.Rows(1).Font.Bold = .T.
      .UsedRange.Columns.Autofit
    Endwith
    loRs.Close
    loConn.Close
  Endif
Endfunc
Cetin
Çetin Basöz

The way to Go
Flutter - For mobile, web and desktop.
World's most advanced open source relational database.
.Net for foxheads - Blog (main)
FoxSharp - Blog (mirror)
Welcome to FoxyClasses

LinqPad - C#,VB,F#,SQL,eSQL ... scratchpad
Previous
Reply
Map
View

Click here to load this message in the networking platform