How to select and "SORT" Excel column with keystro

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

bstafford
Newbie
Posts: 10
Joined: Sun Jul 17, 2011 5:57 am
Location: United Kingdom

Post by bstafford » Tue Jul 19, 2011 4:13 pm

Marcus, I appreciate your help, seems more study is required of me when it comes to this, my first time with this vbscript, I have pasted, highlighted and then pressed code, hope your can see the entire script I have done.

Thanks again for the help.

Code: Select all

VBSTART
Dim xlApp
Dim xlBook
Sub OpenBook(workbook)
  Set xlApp = CreateObject("Excel.Application")
  Set xlBook = xlApp.Workbooks.Open(workbook)
  xlApp.visible = True
End Sub
Sub SortRange(Sheet,Range,Key)
 xlBook.Worksheets(Sheet).Range(Range).Sort xlBook.Worksheets(Sheet).Columns(Key), , , , , , , 0
End Sub
VBEND
Sub Modify_Format()
'
' Modify_Format Macro
' Modify the Excel format to read and flow better
'

'
    Columns("B:B").Select
    With Selection
        .HorizontalAlignment = xlCenter
        .VerticalAlignment = xlBottom
        .WrapText = False
        .Orientation = 0
        .AddIndent = False
        .IndentLevel = 0
        .ShrinkToFit = False
        .ReadingOrder = xlContext
        .MergeCells = False
    End With
End Sub

VBRun>OpenBook,C:\MO\Excel Data\MO Data.xls
Wait>2
VBRun>SortRange,Export_MO_Data,A1:CI9999,G



End
Always trying to learn new things

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue Jul 19, 2011 5:43 pm

Here you go:

Code: Select all

VBSTART
Dim xlApp
Dim xlBook
Sub OpenBook(workbook)
  Set xlApp = CreateObject("Excel.Application")
  Set xlBook = xlApp.Workbooks.Open(workbook)
  xlApp.visible = True
End Sub
Sub SortRange(Sheet,Range,Key)
 xlBook.Worksheets(Sheet).Range(Range).Sort xlBook.Worksheets(Sheet).Columns(Key), , , , , , , 0
End Sub

Sub Modify_Format(Sheet,Range)
    xlBook.Worksheets(Sheet).Select
    xlBook.Worksheets(Sheet).Columns(Range).Select
    xlApp.Selection.HorizontalAlignment = -4108
    xlApp.Selection.VerticalAlignment = -4107
    xlApp.Selection.WrapText = False
    xlApp.Selection.Orientation = 0
    xlApp.Selection.AddIndent = False
    xlApp.Selection.IndentLevel = 0
    xlApp.Selection.ShrinkToFit = False
    xlApp.Selection.ReadingOrder = -5002
    xlApp.Selection.MergeCells = False
End Sub


VBEND

VBRun>OpenBook,C:\MO\Excel Data\MO Data.xls

VBRun>SortRange,Export_MO_Data,A1:CI9999,G

VBRun>Modify_Format,Export_MO_Data,B:B
I've added the final line which calls the function. You didn't have anything actually calling the function or specifying which sheet it should operate on so I've made a few guesses there, but hopefully you can fill in any blanks.

Note how I've moved the VBScript subroutine (the block that begins "Sub") into the VB block and also modified the code so that the Excel object references xlApp and xlBook created in the first OpenBook subroutine are used. This then gives the objects and properties you were referring to context which are meaningless to VBScript otherwise.

This is all explained here:
http://www.mjtnet.com/blog/2008/04/28/c ... -vbscript/
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

Post Reply
cron
Sign up to our newsletter for free automation tips, tricks & discounts