zequestioner
asked on
VB Script to 'hide' columns in Excel file
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!
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!
If you put that in an Excel macro, then you can call it from vbscript like this
Set xl = CreateObject("Excel.applic ation")
xl.Application.Workbooks.O pen "C:\temp\junk.xlsm"
xl.Application.Visible = True
xl.Application.run "'junk.xlsm'!macronametoru n"
Set xl = Nothing
Set xl = CreateObject("Excel.applic
xl.Application.Workbooks.O
xl.Application.Visible = True
xl.Application.run "'junk.xlsm'!macronametoru
Set xl = Nothing
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
ASKER
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!
Tommy, your code worked perfectly.
Thanks!
If you replace .Range by .Columns, it should work. Tommy's code contains some unnecessary stuff, but that doesn't do any harm.
Open in new window