LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2
Default UDF in Excel 2007 for Filter or Query

Thank You for your response. Unforetunately my main problem is converting
the code that I have into a function.

This is what I have and it works... as a sub procedu

Sub FinalTest2()
'
Dim wksht As Worksheet
Dim tblResults, sMax As Variant
Dim rngData, tblRow As Range
Dim tbFilterData As ListObject
Dim rngResults As Range
Dim rngRow, rngCol As Integer
Dim vaFields As Variant
Dim vaCondition1 As Variant, vaCondition2 As Variant
Dim i As Long, r As Long
Dim cNum As Integer
Dim vaResult()
''
'Populate the arrays
vaFields = Array(16, 28, 4)
vaCondition1 = Array("6", "4", "<")
vaCondition2 = Array("6", "6", "4000")

Sheets("test1").Range("rngname").Select
'Set range parameters
Set wksht = ActiveWorkbook.Worksheets("test1")
Set rngData = wksht.Range("rngName")

wksht.ListObjects.Add(xlSrcRange, Range("rngName"), , xlYes). _
Name = "fltTbl"
Set tbFilterData = wksht.ListObjects("fltTbl")
'turn off Table styles
With tbFilterData
.TableStyle = ""
.ShowTableStyleRowStripes = False
.ShowTableStyleColumnStripes = False
'Use Autofilter to find criterias; field = Column#,
'[multiple] criteria(#) = Array("1","2")
i = 0
For i = 0 To UBound(vaFields, 1)
If Not IsMissing(vaCondition2) Then
.Range.AutoFilter Field:=vaFields(i), _
Criteria1:=vaCondition1(i), _
Operator:=xlOr, Criteria2:=vaCondition2(i)
Else
.Range.AutoFilter Field:=vaFields(i), _
Criteria1:=vaCondition1(i)
End If
Next i
End With

rngRow = tbFilterData.ListRows.Count
'rngCol = tbFilterData.ListColumns.Count

cNum = 18
'Set new Array for printing
Set rngResults = tbFilterData.ListColumns(cNum).DataBodyRange. _
SpecialCells(xlCellTypeVisible)

'Loop to re-number rngResults array count
ReDim vaResult(1 To rngRow, 1 To 1)
For Each tblRow In rngResults.Rows
If tblRow.EntireRow.Hidden = False Then
For r = 1 To rngRow
vaResult(r, 1) = rngResults(r, 1)
Next r
End If
Next tblRow
'Name the Array
Names.Add Name:="StoredFilterResults", _
RefersTo:=rngResults

'Test results
sMax = WorksheetFunction.Max(rngResults)
MsgBox sMax

'Convert Table back to Range & Undo Style/Filter
tbFilterData.ShowAutoFilter = False
tbFilterData.Unlist

End Sub

------------------------------------------------------------------------

But I am unable to convert it into a Public Function. The end user will NOT
be able make any changes in VBA. They have to be able to call a function and
input the filter criteria within the worksheet cell.
Please Help

 
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
Excel 2007 Query Wizard problem with Access 2007 extensions Showdad Excel Discussion (Misc queries) 0 December 4th 08 05:57 PM
DataFilterAuto Filter in excel 2007? TIJ New Users to Excel 2 November 13th 08 03:28 AM
Excel 2007 Auto Filter Filter Django Excel Discussion (Misc queries) 2 September 9th 08 10:52 PM
Excel 2007 / MS Query - editing existing query to another sheet Hotpepperz Excel Discussion (Misc queries) 0 June 13th 08 06:53 PM
Query from microsoft query- Excel 2007 טבלאות אקסל 2007 Excel Discussion (Misc queries) 0 December 24th 07 10:47 PM


All times are GMT +1. The time now is 12:20 PM.

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"