I am trying to create my first VBScript that creates an Excel spreadsheet.
I got most of the code from an Excel Macro but the project changed so now the VBScript needs to live on the outside.
I was crashing on rStart = .Range but I changed the code to
Set rStart = objExcel.Range("A1")
Now I am crashing on Also if someone can explain why some of the code starts with .ColumnWidth, .Range, .Cells
I looked up some of these things and it can come from a number of places. Not sure how to determine which one.
For example
.Range can come from Excel.Application or Excel.Application.AutoFilter or Global.Range, etc.
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set objWorkbook = objExcel.Workbooks.Add()
objWorkbook.SaveAs(strFileName)
Dim lMonth 'As Variant
Dim strMonth 'As String
Dim rStart 'As Range
Dim strAddress 'As String
Dim rCell 'As Range
Dim lDays 'As Long
Dim dDate 'As Date
'Add new sheet and format
'Worksheets.Add
objExcel.Worksheets.Add
'ActiveWindow.DisplayGridlines = False
objExcel.ActiveWindow.DisplayGridlines = False
With objExcel.Cells
.ColumnWidth = 6
.Font.Size = 8
End With
'Create the Month headings
For lMonth = 1 To 4
Select Case lMonth
Case 1
strMonth = "January"
Set rStart = .Range("A1")
Case 2
strMonth = "April"
Set rStart = .Range("A8")
Case 3
strMonth = "July"
Set rStart = .Range("A15")
Case 4
strMonth = "October"
Set rStart = .Range("A22")
End Select
'Merge, AutoFill and align months
With rStart
rStart.Value = strMonth
.HorizontalAlignment = xlCenter // Crashing here
.Interior.ColorIndex = 6
.Font.Bold = True
With .Range("A1:G1")
.Merge
.BorderAround xlContinuous
End With
.Range("A1:G1").AutoFill Destination=.Range("A1:U1")
End With
Next
I've tried using
rStart.HorizonalAlignment = xlCenter
objExcel.Range.HorizontalAlignment = xlCenter
I am trying to create an External VBScript that will create an Excel file (no Module Macro or VBA Projects within an Excel document).
The error message says "Unable to set the HorizontalAlignment property of the Range class"
Withstatement. - Heinzi