Need VBS Script to clean up spread sheet

Hi all,

I am in need of a VBS (Not VBA) script that fill open D:\MYFILE.XLS, Select the entire workbook by name (i.e. "My Sheet") and remove (trim) all white spaces from every column including hide all unused columns and rows then unselect the entire worksheet and position me back to A:2 then save to the same file.


Thanks!
LVL 1
smyers051972Asked:
Who is Participating?
 
StellanRosengrenConnect With a Mentor Commented:
Hi again,

Now I have done some more testing. A much faster method to trim the cells is to use a worksheet array formula with the worksheet Trim function. Then you can treat a whole column at a time. The formula is evaluated by using the Evaluate method of the Excel.Application object.
When I run the attached script on your sample worksheet it finished after 15 seconds.
I hope this will work for you.

Kind regards,
Stellan
Dim objWorkbook
Dim objWks
Dim rngCol
Dim objExcel
 
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("E:\newplayers.xls")
Set objWks = objWorkbook.Worksheets("newplayers.xls")
    
    
    
    For Each rngCol In objWks.UsedRange.Columns
        rngCol.Value = objExcel.Evaluate("IF(ROW(" & rngCol.Address & "),IF(" & rngCol.Address & "<>"""",TRIM(" & rngCol.Address & "),""""))")
    Next
	objExcel.Visible=True

Open in new window

0
 
Eoin RyanI.T. ContractorCommented:
is it that you want to launch the script external to excel? You can embed the macro, and have the VBS open and start the macro.

So the code would sit in the excel file but it would be the VBS that launched it. let me know if that's an option.
0
 
smyers051972Author Commented:
Id have to load it every time so its just easier to use VBS :)
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
Krys_KCommented:
Hi There
Is it possible you could upload a sample spreadhseet you want to clean up then it will save me a bit of time when i write something in VBS for you.
Cheers
Krystian
It doesn't have to be the actual spreadsheet, just a sample will do but explain what its to look like at the end result too. :-)
0
 
smyers051972Author Commented:
0
 
smyers051972Author Commented:
looking just to trim all white spaces from all columns, this will reduce the spreadsheet size by a little, maybe not a lot however when you pull this data across a satellite connection every little bit can help!
0
 
StellanRosengrenCommented:
Hi
Could this be something useful?

Kind regards,
Stellan
Dim objWorkbook
Dim objWks
Dim rngCell
Dim objExcel
 
Set objExcel = CreateObject("Excel.Application")
Set objWorkbook = objExcel.Workbooks.Open("D:\MYFILE.xls")
Set objWks=objWorkbook.Worksheets(1)
    
For Each rngCell In objWks.UsedRange
	rngCell.Value = Trim(rngCell.Value)
Next rngCell

Open in new window

0
 
smyers051972Author Commented:
Hi there,

Sorry for the late responce, I will test it and update accordingly.

Thank you for your help!
0
 
smyers051972Author Commented:
I tested and got an expected end of statement error on line 12 char 6.
0
 
StellanRosengrenCommented:
Sorry,
I am not so used to vbscript. Please remove 'rngCell' from that line.

Stellan
0
 
smyers051972Author Commented:
Hello,

I did what was recommended and tested now we arent erroring out, but oddly now I see no CPU consumption and it stays open indefinately.

Thanks!
0
 
StellanRosengrenCommented:
Hi smyers051972,

Would it be possible for you to upload a sample workbook that you have tried this on?

Kind regards,
Stellan
0
 
smyers051972Author Commented:
http://filedb.experts-exchange.com/incoming/2009/05_w20/137798/1.newplayers.xls
to be more spcific, names removed to protect the innocent ;)
0
 
StellanRosengrenCommented:
Hi,
The code is looping through all cells in the used range of the sheet and reads each value, trims it and writes it back to the cell. It seems to be working alright, even on your sheet. BUT, it is not fast enough. You have more than 200 000 cells in the sheet and it looks like it takes hours to finish. Of course not acceptable. I will try to find something better. Please be patient.

Kind regards,
Stellan
0
 
smyers051972Author Commented:
Thank you very much I will keep checking back over the next day or two!
0
 
smyers051972Author Commented:
Hello

Close! The only two things I dont see it doing is saving the workbook and excel is popping up on me, I dont remember the way to make it stay hidden with no prompts or how to make it save the file (same file)

Thanks!
0
 
StellanRosengrenCommented:
Hi,
No problems,
you see the last line 'objExcel.Visible=True' is making Excel pop up.
I used it to be able to examine the results of the script.
You can replace that line by
objWorkbook.Close True
where 'True' is for SaveChanges
Then you should add
objExcel.Quit
set objWks=Nothing
set objWorkbook=Nothing
set objExcel=Nothing
which should prevent this Excel instance from continue running in the background.
I am not sure if it is necessary to set the variables to Nothing but possibly it does free up memory. So, it will not hurt to do that.

Kind regards,
Stellan

0
 
smyers051972Author Commented:
Thank you again for your help!
0
 
StellanRosengrenCommented:
I am glad that I could help. Actually, this is a solution that I will use myself since it is much faster than the method I normally use.
Thanks for the grade and the points.

Kind regards,
Stellan
0
 
smyers051972Author Commented:
Not a problem... I am really interested in creating pie graphs from the data in the spreadsheet through VBS code but last time I opened the case no one touched it LOL How about you?
0
 
Krys_KCommented:
Hi there
Sorry i didn't input on this since my last post, however, when i saw that stellan had given you the answer i decided to stand back and allow him the honours. However, in readinfg your last postabout pie chart, i would be happy to work with Stellan on getting something together if he was interested, if not then i could always give it a go myself. :-)
Regards
Krystian
0
 
StellanRosengrenCommented:
Hi Krystian,
I will be glad to assist.

smyers051972, please post a link here to the question about pie charts.

Kind regards,
Stellan
0
 
smyers051972Author Commented:
Ill open a new question on it and try to explain what I can, if I dont put enough just request more info and discuss further :)

Thanks!

A link to the new question should be my next follow up on this.
0
 
Krys_KCommented:
Nice 1. :-)
Will collaborate on the new post.
Krystian
0
 
smyers051972Author Commented:
0
 
smyers051972Author Commented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.