Solved

VB Script to 'hide' columns in Excel file

Posted on 2013-12-12
6
1,474 Views
Last Modified: 2013-12-12
Can someone provide an example vb script or macro which can do the following:

1. Open file.xlsx
2. Hide specific columns, for example A, B, D, F J. (delete the columns would be fine too)
3. Save file.xlsx
4. Close Excel.

Thanks experts!
0
Comment
Question by:zequestioner
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 39714592
This is what it could look like from VBA (within Excel)
    Dim book As Workbook
    Dim sheet As Worksheet
    Set book = Application.Workbooks.Open("C:\temp\junk.xls")
    Set sheet = book.Worksheets(1)
    sheet.Columns("A:A").Hidden = True
    sheet.Columns("B:B").Hidden = True
    sheet.Columns("D:D").Hidden = True
    sheet.Columns("F:F").Hidden = True
    sheet.Columns("J:J").Hidden = True
    book.Save
    Application.Quit

Open in new window

0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 39714605
If you put that in an Excel macro, then you can call it from vbscript like this
Set xl = CreateObject("Excel.application")

xl.Application.Workbooks.Open "C:\temp\junk.xlsm"
xl.Application.Visible = True
xl.Application.run "'junk.xlsm'!macronametorun"

Set xl = Nothing
0
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 500 total points
ID: 39714632
Actually, you can do it all from the vb script, sorry.

Set xl = CreateObject("Excel.application")

xl.Application.Workbooks.Open "C:\temp\junk.xls"
set book = xl.Application.Workbooks("junk.xls")
xl.Application.Visible = True
Set sheet = book.Worksheets(1)
sheet.Columns("A:A").Hidden = True
sheet.Columns("B:B").Hidden = True
sheet.Columns("D:D").Hidden = True
sheet.Columns("F:F").Hidden = True
sheet.Columns("J:J").Hidden = True
book.Save
xl.Application.Quit

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 70

Expert Comment

by:Qlemo
ID: 39714650
You wouldn't want to have to include the macro in each XLS file. The general steps are correct, but we can do all that in VBS directly:
Set xl = CreateObject("Excel.application")

set wb = xl.Workbooks.Open("C:\temp\junk.xlsm")
with wb.WorkSheets(1)
  .Range("A:B").Hidden = True
  .Range("D:D").Hidden = True
  .Range("F:F").Hidden = True
  .Range("J:J").Hidden = True
end with

wb.Save
xl.Quit
Set xl = Nothing   ' not necessary, but good style

Open in new window

0
 
LVL 1

Author Comment

by:zequestioner
ID: 39714750
Qlemo, your code gave this message: c:\ee\HideCol.vbs(5, 3) Microsoft Excel: Unable to set the Hidden property of the Range class

Tommy, your code worked perfectly.

Thanks!
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 39714836
If you replace .Range by .Columns, it should work. Tommy's code contains some unnecessary stuff, but that doesn't do any harm.
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial demonstrates in Microsoft Excel how to consolidate your marketing data by creating an interactive charts using form controls. This creates cool drop-downs for viewers of your chart to choose from.
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…

751 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