Solved

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

Posted on 2008-10-29
6
281 Views
Last Modified: 2013-11-10
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.

0
Comment
Question by:smyers051972
  • 3
  • 3
6 Comments
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 22835358
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
 
LVL 1

Author Comment

by:smyers051972
ID: 22835887
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
 
LVL 65

Expert Comment

by:RobSampson
ID: 22837080
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 1

Author Comment

by:smyers051972
ID: 22837131
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
 
LVL 1

Author Closing Comment

by:smyers051972
ID: 31511284
Worked perfectly.
Thanks a lot!
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 22854505
Glad to help.  Thanks for the grade.

Regards,

Rob.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question