Solved

Convert Excel macro to VBScript

Posted on 2013-01-20
4
809 Views
Last Modified: 2013-01-20
I'm trying to format excel from VBS  - I'm grouping several columns and it works fine but I need to collapse the column level. The last line (in bold) was recorded using the macro recorder. My question is: how do I translate this so it will run in VBS (how do I collapse the outline in VBS)?

Set myExcel = CreateObject("Excel.Application")
myExcel.visible = TRUE
Set myWorkbook=myExcel.Workbooks.Open("C:\Scripts\10_Macro_Format.xlsx")
Set mySheet = myWorkbook.sheets(1)
mySheet.Range("C1:H1").Group

ActiveSheet.Outline.ShowLevels RowLevels:=0, ColumnLevels:=1
0
Comment
Question by:mike_VBS
  • 2
  • 2
4 Comments
 
LVL 92

Expert Comment

by:Patrick Matthews
Comment Utility
Try this:

Dim myExcel, myWorkbook, mySheet

Set myExcel = CreateObject("Excel.Application")
myExcel.visible = TRUE
Set myWorkbook=myExcel.Workbooks.Open("C:\Scripts\10_Macro_Format.xlsx")
Set mySheet = myWorkbook.sheets(1)

With mySheet
    .Range("C1:H1").Group
    .Outline.ShowLevels RowLevels:=0, ColumnLevels:=1
End With

Open in new window

0
 

Author Comment

by:mike_VBS
Comment Utility
get the following error on line 10

Microsoft VBScript compilation error: Expected statement
0
 
LVL 92

Accepted Solution

by:
Patrick Matthews earned 500 total points
Comment Utility
Sorry, forgot that VBScript does not support passing arguments by name.

Dim myExcel, myWorkbook, mySheet

Set myExcel = CreateObject("Excel.Application")
myExcel.visible = TRUE
Set myWorkbook=myExcel.Workbooks.Open("C:\Scripts\10_Macro_Format.xlsx")
Set mySheet = myWorkbook.sheets(1)

With mySheet
    .Range("C1:H1").Group
    .Outline.ShowLevels 0, 1
End With

Open in new window

0
 

Author Comment

by:mike_VBS
Comment Utility
Yes, that works - hacking around a could have sworn I tried that - I guess not.

Hey, thanks!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Outlook Free & Paid Tools
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
The viewer will learn how to  create a slide that will launch other presentations in Microsoft PowerPoint. In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide: …

772 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now