Link to home
Start Free TrialLog in
Avatar of smyers051972
smyers051972Flag for United States of America

asked on

Looking for a .VBS Script to create a header onto excel 2003.

I need a script in VB (.VBS) that when run will populate the 1st row and each column going left to right on every worksheet in a single excel file such as this:

     ColA     ColB                  ColC
1   ITEM    DESCRIPTION    PRICE  
2
3

The sole purpose for thie script is to create a header only, there is more than 3 columns to add but if I could get the three and shown how to add in more columns that would be great!  Please note, not needing a macro, this will all be run from a script at the command prompt for automation purposes.

Thank you!
Avatar of ben_staples
ben_staples

Do you want to shift the current data down + right one cell or overtype the headings?
Avatar of zorvek (Kevin Jones)
Public Sub SetUpHeaderRows()

   Dim Worksheet As Worksheet
   For Each Worksheet In Worksheets
      Worksheet.[A1:C1].Value = Array("ITEM", "DESCRIPTION", "PRICE")
   Next Worksheet

End Sub

Kevin
This is more compatible with VBScript. Assuming you have the workbook object defined as oWorkbook:

   For Each Worksheet In oWorkbook.Worksheets
      Worksheet.Range("A1:C1").Value = Array("ITEM", "DESCRIPTION", "PRICE")
   Next

Kevin
That's just a macro, for a VBScript file it would be:

Option Explicit
On Error Resume Next
Dim directory, objExcel, workbook, worksheet
Set objExcel = CreateObject("Excel.Application")
'Gets the directory where our script is running from
directory = CreateObject("Scripting.FileSystemObject").GetParentFolderName(Wscript.ScriptFullName)
'Open XLS file
Set workbook = objExcel.Workbooks.Open(directory & "\myfile.xls")
 
For Each Worksheet In Worksheets
	Worksheet.[A1:C1].Value = Array("ITEM", "DESCRIPTION", "PRICE")
Next Worksheet
 
objExcel.Workbooks.Close
objExcel.quit
objExcel = Empty
workbook = Empty

Open in new window

Avatar of smyers051972

ASKER

Ben:
Will try it Monday when I get to work. Thank you for your reply!

I am going to replace  "\myfile.xls" portion with the name of the actual spread sheet.  Or should I do "C:\mydirectory\myfile.xls" ?

Thanks again!
One last thing, does the workbook have to be empty or will it auto insert above existing data? I would probably prefer it to insert it above existing data.

Thanks!
ok curiosity was killing me so I tried it here at home and ran the file on myfile.xls (a file i created) the bad news was i got an error on line 8 char 54 Expected ')'

Not sure where its wanting the ) to be

Thanks :)
Try this sequence:

Set ExcelApplication = CreateObject("Excel.Application")
Folder = CreateObject("Scripting.FileSystemObject").GetParentFolderName(Wscript.ScriptFullName)
Set Workbook = ExcelApplication.Workbooks.Open(Folder, "\Workbook.xls")
For Each Worksheet In Workbook.Worksheets
   Worksheet.Range("1:1").Insert
   Worksheet.Range("A1:C1").Value = Array("ITEM", "DESCRIPTION", "PRICE")
Next Worksheet
Workbook.Save
Workbook.Close False
ExcelApplication.Quit

Kevin
Error: Expected End of statement line 7 char 6
Fixed:

Set ExcelApplication = CreateObject("Excel.Application")
Folder = CreateObject("Scripting.FileSystemObject").GetParentFolderName(Wscript.ScriptFullName)
Set Workbook = ExcelApplication.Workbooks.Open(Folder, "\Workbook.xls")
For Each Worksheet In Workbook.Worksheets
   Worksheet.Range("1:1").Insert
   Worksheet.Range("A1:C1").Value = Array("ITEM", "DESCRIPTION", "PRICE")
Next
Workbook.Save
Workbook.Close False
ExcelApplication.Quit

Kevin
The code zorvek posted should work.
Going to test in the morning tomorrow when I get to work. Thank!
*Thanks!
Now I have an error on line 3, unable to get the Open property of the workbook class

Not sure what this would mean.
ASKER CERTIFIED SOLUTION
Avatar of zorvek (Kevin Jones)
zorvek (Kevin Jones)
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Kevin: Awesome!

Worked perfectly! Thanks again :)