使用Access 2010 VBA格式化Excel。我的代码第一次工作。

使用Access 2010 VBA格式化Excel。我的代码第一次工作。,excel,ms-access-2010,vba,Excel,Ms Access 2010,Vba,我正在使用Access 2010 VBA创建一个Excel工作表,其中包含cols A到m和未知行数。我的格式很简单,可以更改字体、添加边框、列宽、打印选项等。它第一次运行,但如果我再次尝试,它将返回以下错误:“Err=91对象变量或未设置块变量”。我的困惑是,当它第一次运行时,它会出错,但之后它就不再工作了。这是我的密码: `Option Compare Database 'Option Explicit 'Dim objXLApp As Excel.Application

我正在使用Access 2010 VBA创建一个Excel工作表,其中包含cols A到m和未知行数。我的格式很简单,可以更改字体、添加边框、列宽、打印选项等。它第一次运行,但如果我再次尝试,它将返回以下错误:“Err=91对象变量或未设置块变量”。我的困惑是,当它第一次运行时,它会出错,但之后它就不再工作了。这是我的密码:

`Option Compare Database
'Option Explicit

'Dim objXLApp         As Excel.Application
'Dim objXLBook        As Excel.Workbook
'Dim objXLSheet       As Excel.Worksheet
'Dim objXLRange       As Object
'Dim xlLastRow        As Long
'Dim xlCell           As String

`Public Sub FormatTaskCalendar(fileIn As String, sheetIn As String)
 On Error GoTo Err_FormatTaskCalendar

        'open the Excel spreadsheet with the exported data
Set objXLApp = CreateObject("Excel.Application")
        'for testing, make the applicaiton visible
objXLApp.Visible = True
Set objXLBook = objXLApp.Workbooks.Open(fileIn)
        'make the "Orders" worksheet to be the active worksheet
Set objXLSheet = objXLBook.Sheets(sheetIn)
        'find the last used cell in Column "A" (Center)
xlLastRow = objXLSheet.Range("A65536").End(xlUp).Row

        'show the cell borders for entire sheet
With objXLSheet.Range("A1:M" & xlLastRow).Borders
    .LineStyle = xlContinuous
    .Weight = xlThin
End With

With objXLSheet.Range("B1:M" & xlLastRow).Font
    .Name = "Verdana"
    .Size = 11
End With

        'select first row and apply formating
objXLSheet.Activate
objXLSheet.Range("A1:M1").Select
With Selection.Interior
    .Pattern = xlSolid
    .PatternColorIndex = xlAutomatic
    .Color = 5287936
    .TintAndShade = 0
    .PatternTintAndShade = 0
End With
Selection.Font.Name = "Verdana"
Selection.Font.Size = 13
Selection.Font.Bold = True
Selection.HorizontalAlignment = xlCenter
With Selection
    .HorizontalAlignment = xlCenter
    .VerticalAlignment = xlBottom
    .WrapText = True
    .Orientation = 0
    .AddIndent = False
    .IndentLevel = 0
    .ShrinkToFit = False
    .ReadingOrder = xlContext
    .MergeCells = False
End With

    'freeze the pane so the header row doesn't scroll
objXLSheet.Activate
objXLSheet.Range("A2", "A2").Select
objXLApp.ActiveWindow.FreezePanes = True

'autofit the columns
objXLSheet.Activate
objXLSheet.Range("A1", "M1").Select
objXLSheet.Cells.EntireColumn.AutoFit
Columns("B:B").ColumnWidth = 15.5           'Neovia Team Member
Columns("D:D").ColumnWidth = 26.5           'Client Name
Columns("E:E").ColumnWidth = 13             'Renewal Date
Columns("F:F").ColumnWidth = 14.44          'Stage
Columns("G:G").ColumnWidth = 12.7           'Channel
Columns("H:H").ColumnWidth = 36.5           'Task Name
Columns("I:I").ColumnWidth = 12.3           'Task Due Date
Columns("J:J").ColumnWidth = 16             'Task Completion Date
Columns("K:K").ColumnWidth = 14.11          'Days To Complete
Columns("L:L").ColumnWidth = 10.56          'Actual Work Hours
Columns("M:M").ColumnWidth = 15.3           'Comments

        ''do some settings for the page layout when printing
objXLApp.PrintCommunication = True
With objXLSheet.PageSetup
    .Orientation = xlLandscape
    .PrintTitleRows = "$1:$1"
    .PrintTitleColumns = ""
    .LeftHeader = "&""Verdana,Bold""&13page &P of &N"
    .CenterHeader = "&""Verdana,Bold""&13Task Calendar"
    .RightHeader = "&""Verdana,Bold""&13&D&T"
    .LeftFooter = ""
    .CenterFooter = ""
    .RightFooter = ""

    .LeftMargin = objXLApp.InchesToPoints(0.25)
    .RightMargin = objXLApp.InchesToPoints(0.25)
    .TopMargin = objXLApp.InchesToPoints(0.75)
    .BottomMargin = objXLApp.InchesToPoints(0.5)
    .HeaderMargin = objXLApp.InchesToPoints(0.5)
    .FooterMargin = objXLApp.InchesToPoints(0.5)
    .Zoom = False
    .FitToPagesWide = 1
    .FitToPagesTall = False

End With
objXLApp.PrintCommunication = False

'save the changes
objXLBook.Save
objXLApp.Quit
Set objXLApp = Nothing
“我的代码”在执行这行代码时创建并出错:

          'select first row and apply formating
objXLSheet.Activate
objXLSheet.Range("A1:M1").Select
With Selection.Interior

我打开了visible属性,这样我可以看到它确实选择了A1:M1,但我不明白为什么下一行只工作一次。这是我第一次尝试从Access格式化Excel。非常感谢您的帮助。

由于您的代码已被注释,我不确定您使用的是早期绑定还是晚期绑定

但是,试试这个。避免使用
。选择
选择
。激活

换行

objXLSheet.Activate
objXLSheet.Range("A1:M1").Select
With Selection.Interior


您可能还想查看链接。

因为您对代码进行了注释,所以我不确定您使用的是早期绑定还是晚期绑定

但是,试试这个。避免使用
。选择
选择
。激活

换行

objXLSheet.Activate
objXLSheet.Range("A1:M1").Select
With Selection.Interior


您可能还想查看链接。

谢谢,这很有效。我还更改了其余代码,以消除.Select.Selection和.Activate的使用。除了一行以外,其他一切都很好。我想我对Excel对象不是很了解。我不知道如何冷冻玻璃。我能找到的每一个例子都使用了你告诉我不要做的事情。你能再帮我一次,给我指出正确的方向吗。您还询问了有关早绑定或晚绑定的问题。如果我理解正确,我认为我使用的是早期绑定,因为我已经设置了对“Microsoft Excel 15.0对象库”的引用。在这种情况下,我建议您访问MSDN并搜索
Excel对象库。它有很好的记录。如果你还有问题,我会要求你把代码贴在你被卡住的地方,然后我们就可以从那里开始了?非常感谢你的帮助和你的快速反应。我有冰箱要用。这是我使用的代码:objXLSheet.Range(“A2”)。选择objXLBook.Windows.Application.ActiveWindow.FreezePanes=True如果您有更好的建议,请告诉我。再次感谢,谢谢,这很有效。我还更改了其余代码,以消除.Select.Selection和.Activate的使用。除了一行以外,其他一切都很好。我想我对Excel对象不是很了解。我不知道如何冷冻玻璃。我能找到的每一个例子都使用了你告诉我不要做的事情。你能再帮我一次,给我指出正确的方向吗。您还询问了有关早绑定或晚绑定的问题。如果我理解正确,我认为我使用的是早期绑定,因为我已经设置了对“Microsoft Excel 15.0对象库”的引用。在这种情况下,我建议您访问MSDN并搜索
Excel对象库。它有很好的记录。如果你还有问题,我会要求你把代码贴在你被卡住的地方,然后我们就可以从那里开始了?非常感谢你的帮助和你的快速反应。我有冰箱要用。这是我使用的代码:objXLSheet.Range(“A2”)。选择objXLBook.Windows.Application.ActiveWindow.FreezePanes=True如果您有更好的建议,请告诉我。再次感谢。