Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open XLS add Filter to selected columns

Status
Not open for further replies.

Leosy

Technical User
Apr 13, 2012
49
0
0
PL
Hello.

I need open XLS file and add "filter"/"autofilter" columns.
Could you help me with this ?

Code:
Function OK()

Const ForReading = 1
Const ForWriting = 2

Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True

Set objFile = bjFSO.objExcel("C:\07-05-2012.xls", ForReading)

Excel.Range("A1:Z23").Autofilter 

Set objFile = objFSO.objExcel("C:\Result.xls", ForWriting)

objFile.Write strNewContents
objFile.Close

Set objFile = Nothing
Set objFSO = Nothing

End Function

but it doesnt work :(

could you help me ?
 
Have a look at faq329-7301 for an example of opening an instance of Excel and manipulating a worksheet.
 
can't see there anything about autofilter :(
 
Not specifically about autofilter, no; but it does demonstrate how to create an instance of Excel and write information to the file (which your current script does not do correctly).
 


What are you trying to do here...
Code:
Set objFile = bjFSO.objExcel("C:\07-05-2012.xls", ForReading)
This seems as if you are trying to READ a TEXT file. You can certainly do that, but if you do, there is NOTHING that relates to RANGE and AUTOFILTER. You only get these objects when opening the file using the Excel application like...
Code:
with objExcel.Workbooks.Open("C:\07-05-2012.xls")
   .worksheets(1).Range("A1:Z23").Autofilter 
   .save
   .close
end with

Set objExcel = Nothing


Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Code:
  Function OK() 
 
 Const ForReading = 1 
 Const ForWriting = 2 
 
 Set objExcel = CreateObject("Excel.Application") 
 Set objWorkbook = objExcel.Workbooks.Open("C:\test.xls", ForReading) 
 
 objWorkbook.Cells(1, 100).Autofilter  
 
 Set objWorkbook = objExcel.Workbooks.Open("C:\07-05-2012.xls", ForWriting) 
 
 objExcel.Workbooks.Save
 objExcel.Workbooks.Close
 objExcel.Quit  
 
 
 End Function

I was trying to using this modification but nothing is happening...
 


There is no ForReading or ForWriting constant in the Open method.

Your approch is STRANGE!

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
SkipVought

I used

Code:
Set objExcel = CreateObject("Excel.Application") 
with objExcel.Workbooks.Open("C:\test1.xls")
   .worksheets(1).Range("A1:Z23").Autofilter
   .save
   .close
end with

Set objExcel = Nothing

and it works perfectly.

But When I wanted to put it in Function (my soft needs this)

it doesn't work.

Code:
Function OK()

Set objExcel = CreateObject("Excel.Application") 
with objExcel.Workbooks.Open("C:\2.xls")
   .worksheets(1).Range("A1:Z23").Autofilter
   .save
   .close
end with

Set objExcel = Nothing

End Function

 


"it doesn't work."

Meaning.....

1) nothing happens at all, no indications, utter silence, etc.

2) an error occurs

3) something different happens from what you expected

4) something else not included in 1) - 3)

Please be SPECIFIC!

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
answer 1)
It looks like script was executed but no "autofilter was added in to xls"

It's work when VBS looks like this
Code:
Set objExcel = CreateObject("Excel.Application")
with objExcel.Workbooks.Open("C:\test1.xls")
   .worksheets(1).Range("A1:Z23").Autofilter
   .save
   .close
end with

Set objExcel = Nothing

but doesn't work when I put it in to function:

Code:
[b]Function OK()[/b]

Set objExcel = CreateObject("Excel.Application")
with objExcel.Workbooks.Open("C:\2.xls")
   .worksheets(1).Range("A1:Z23").Autofilter
   .save
   .close
end with

Set objExcel = Nothing

[b]End Function[/b]

 


You realize that you have consistently used TWO SEPARATE workbooks for what works and what does not.

Is that by design?

Did you try BOTH workbooks in BOTH procedures?

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
Yes.I used 2 workbooks.I tried both workbooks in both procedures
:(
 
I've tested again with different XLS... maybe it problem with with

Code:
with ...
end with

Is it possible to "change it" somewhow to different "source code" ?
 



Code:
Function OK()
  dim wb as object

  Set objExcel = CreateObject("Excel.Application")

  set wb = objExcel.Workbooks.Open("C:\2.xls")

  wb.worksheets(1).Range("A1:Z23").Autofilter
  wb.save
  wb.close

  set wb = nothing
  Set objExcel = Nothing
End Function

Skip,
[sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
crap it's not my day...

Error: Expected and statesment
code: 800A0401
Compilation error

according google

Introduction to Code 800A0401

Error code, 800A0401 occurs when you execute a VBScript. This is a compilation error, therefore check your punctuation. A wild guess, you have left out an ampersand (&), or possibly a comma.
 
Referencing line numbers won't help us unless you also post the code you are using 'as-is'.
 
Hi.
vbscript

Code:
Function OK()
  dim wb as object

  Set objExcel = CreateObject("Excel.Application")

  set wb = objExcel.Workbooks.Open("c:\Copy.xls")

  wb.worksheets(1).Range("A1:Z23").Autofilter
  wb.save
  wb.close

  set wb = nothing
  Set objExcel = Nothing
End Function
 
Get rid of the "as object" in the variable declaration. In VBScript there are no types only variants.

Code:
Function OK()
  dim wb [COLOR=red][s]as object[/s][/color]

  Set objExcel = CreateObject("Excel.Application")

  set wb = objExcel.Workbooks.Open("c:\Copy.xls")

  wb.worksheets(1).Range("A1:Z23").Autofilter
  wb.save
  wb.close

  set wb = nothing
  Set objExcel = Nothing
End Function
 
I see no error now but script didn't add autofilter to xls doc :(
my case depressed me now :(
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top