title: Export Filtered Rows from Access Datasheet to Excel
body: <p>I'm using the VBA code below to export just the visible columns in an Access Datasheet to Excel. Works great, but I also want to export only the filtered rows, based on what the user has filtered for each column.
  i could use some help figureing out how to do that.
  Any help is appreciated,
  lq</p>
  
  <pre><code> Public Function ExportToExcel()
   On Error GoTo myErr
  
       Dim strDestFolder As String
       Dim strSQL As String
       Dim qdf As dao.QueryDef
       Dim ctl As Control
       Dim strRandomString As String
  
       strDestFolder = "MyDestinationPath"
       strRandomString = "AQueryDefName"
  
       For Each ctl In Me.Child.Form.Controls
  
           If Not ctl.ColumnHidden Then
               If Len(strSQL) = 0 Then
                   strSQL = "SELECT " &amp; ctl.ControlSource &amp; ", "
               Else
                   strSQL = strSQL &amp; ctl.ControlSource &amp; ", "
               End If
           End If
  
       Next ctl
  
       If Len(strSQL) &gt; 0 Then
           strSQL = Left(strSQL, Len(strSQL) - 2)
           strSQL = strSQL &amp; " FROM tblMyTableName WHERE Something = '" &amp; Me.Something &amp; "'"
       End If
  
       Set qdf = CurrentDb.CreateQueryDef(strRandomString, strSQL)
  
       DoCmd.OutputTo acOutputQuery, strRandomString, "MicrosoftExcel (*.xls)", strDestFolder &amp; ".xls", True
  
   myExit:
       CurrentDb.QueryDefs.Delete strRandomString
       Exit Function
   myErr:
       MsgBox Err.Number &amp; " - " &amp; Err.Description, vbCritical, "VBA Error"
       Resume myExit
   End Function
  </code></pre>