I need a script to auto sort by specific column(s) and format every worksheet in an excel file

Hi all,

I am seeking a new .VBS script (please no VBA) for automation purposes that will take a specific excel spread sheet and sort all work sheets by a specific column(s). After the sorting is completed I need the script to also auto set the page to either letter or legal (also like to set portrait or landscape) and set the work sheets to auto fit to 1 page wide by any number of pages long, the # pages long doesnt matter much so long as it all fits on 1 page wide when printed.

I think that the lines for the choices should perhaps be a comment and just uncomment my choices manually with no actual prompting.

after all the work is done, the worksheet should be saved automatically.

LVL 1
smyers051972Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RobSampsonCommented:
Hi, try this out.

Regards,

Rob.
strExcelFile = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "Test1.xls"
strSortColumn = "A"
boolHeaders = True
 
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Const xlAscending = 1
Const xlYes = 1
Const xlNo = 2
Const xlTopToBottom = 1
Const xlSortNormal = 0
Const xlPortrait = 1
Const xlLandscape = 2
Const xlPaperLetter = 1
Const xlPaperLegal = 5
Set objWB = objExcel.Workbooks.Open(strExcelFile)
For Each objSheet In objWB.Sheets
	objSheet.Select
	objSheet.Cells.Select
	objSheet.Range("A1").Select
	If boolHeaders = True Then
		objExcel.Selection.Sort objSheet.Range(strSortColumn & "2"), xlAscending, , , , , , xlYes, _
    		1, False, xlTopToBottom, xlSortNormal
    Else
		objExcel.Selection.Sort objSheet.Range(strSortColumn & "2"), xlAscending, , , , , , xlNo, _
			1, False, xlTopToBottom, xlSortNormal
    End If
	With objSheet.PageSetup
	    .Orientation = xlPortrait
'	    .Orientation = xlLandscape
	    .PaperSize = xlPaperLetter
'	    .PaperSize = xlPaperLegal
	    .FitToPagesWide = 1
        .FitToPagesTall = False
	End With
Next
objWB.Sheets(1).Select
objWB.Save
objWB.Close
objExcel.Quit

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
smyers051972Author Commented:
I will try it out now.

With regards to:
Const xlAscending = 1
Const xlYes = 1
Const xlNo = 2
Const xlTopToBottom = 1
Const xlSortNormal = 0
Const xlPortrait = 1
Const xlLandscape = 2
Const xlPaperLetter = 1
Const xlPaperLegal = 5

Could you explain what the options would be with respect to each other?

Thank you!
0
RobSampsonCommented:
Those constants just set values for the different options available in Excel.

When you record a macro in Excel, such constants are used to specify parameters. In VBScript, the scripting engine does not natively know what "xlAscending" is, so you need to define it, that's all.

The options are used later in the code in the certain methods that require them.

Your page layout options, for example, are here:
      With objSheet.PageSetup
          .Orientation = xlPortrait
'          .Orientation = xlLandscape
          .PaperSize = xlPaperLetter
'          .PaperSize = xlPaperLegal
          .FitToPagesWide = 1
        .FitToPagesTall = False
      End With


and I have commented out a couple of them, because they conflict, so can swap the commenting when required.

Regards,

Rob.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

smyers051972Author Commented:
Thanks so much

I will try it out in the morning when I get back into work and close this soon as possible.

Thank you again!
0
smyers051972Author Commented:
Worked perfectly.
Thanks a lot!
0
RobSampsonCommented:
Glad to help.  Thanks for the grade.

Regards,

Rob.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

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.