I am in need to add a new row above Cell 1 in my workbook. I typed out the below syntax which I believe is correct, but I am getting an error of:
Object does not support this property or method
When the syntax hits the second line of my with block - .Rows("1:1").Select
What do I need to alter in order for this syntax to execute as expeceted?
Function AdddFormatting()
Dim ExportRecordSet As DAO.Recordset
Dim excelWS As Object, excelWS2 As Object
Dim xl As Object
Dim wb As Object
Dim TemplateWB As String
Set xl = CreateObject("Excel.Application")
TemplateWB = "C:\Test\Testwb.xlsx"
Set wb = xl.Workbooks.Add
Set excelWS = wb.Worksheets(1)
excelWS.Name = "AddedFromCode"
Set excelWS2 = wb.Sheets.Add(After:=wb.Sheets(wb.Sheets.Count))
excelWS2.Name = "AddedFromCode2"
xl.Application.Visible = True
With wb
.Sheets(1).Activate
.Rows("1:1").Select
'Using this syntax throws the same error
'.Rows(1).Select
.Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
.Range("A1:H1").Select
.Selection.Merge
.ActiveCell.FormulaR1C1 = "This is the text that will display as the header"
.Range("A1:H1").Select.Font.Name = "Arial"
.Range("A1:H1").Select.Font.Size = 15
.Range("A1").Activate
End With
End Function
EDIT
Per the comment posted by @user2676140 I altered my with block from with wb to with excelWS which now throws the same error on line 3 - this one:
.Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Excel.Application) and which global objects are available by default in Access (e.g.Access.Application). You can reference Excel objects either using late binding (as you are doing, with theCreateObjectfunction) or using early binding, by adding a reference (Tools -> References...) to the Microsoft Excel object library, as @JeremyThompson notes. TheWithstatement does something completely different -- it lets you call members on a ...