从Access的Excel格式化每隔一段时间(对象variables/块未设置)

我有一个点击子例程,导出两个表格,以Excel。 然后,我logging了一个Excelmacros,并将代码复制到相应的格式化工作表。

我的问题是,我每隔一段时间运行代码,我得到这个错误:“运行时错误'91':对象variables或块variables未设置”

这发生在我的代码的这一点:

With .ActiveSheet .Range("B" & .Cells.Rows.Count).End(xlUp).Offset(1, 0).Activate End With With ActiveCell '~~~~~~~~ERROR OCCURS HERE~~~~~~~~~~~~ .formula = "=SUM(B2:" & ActiveCell.Offset(-1, 0).Address & ")" '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ .Font.Bold = True End With 

我想要SUM的范围是dynamic的,这就是为什么我在公式中抵消了单元格1,我试过了:

  .formula = "=SUM(B2:B3)" 

但是这给出了同样的错误。

完整的代码如下,我已经收到来自“Excel工作表格式”注释下的第一行的这个错误,并且已经通过在代码中添加了块,但是无法避开.formula =行。

我真的坚持这一点,任何帮助将不胜感激

完整代码:

  Private Sub cmdExport_Click() Dim xl As Excel.Application Dim wb As Object Dim todaysDate As String Dim fd As FileDialog Dim FolderChosen As Integer DoCmd.SetWarnings False todaysDate = Replace(Date, "/", "") Set fd = Application.FileDialog(msoFileDialogFolderPicker) FolderChosen = fd.Show If FolderChosen <> -1 Then MsgBox ("Export Cancelled") Else CurrentDb.QueryDefs("plates_sold").SQL = "SELECT plates.Plate, plates. [Price In], plates.[Price Out], (plates.[Price Out] - plates.[Price In]) AS Income, dbo_Sales.[Date Deposit Rec] AS [Deposit Paid], dbo_Sales.[Comm By Who] AS [Sold By] " & _ "FROM plates INNER JOIN dbo_Sales ON plates.Plate = dbo_Sales.Plate " & _ "WHERE plates.[Plate Status] = 'F' AND dbo_Sales.[Date Deposit Rec] IS NOT NULL" DoCmd.OutputTo acOutputQuery, "plates_sold", "Excel Workbook (*.xlsx)", fd.SelectedItems(1) & "/" & Me.Text24.Value & "_Recovery_" & todaysDate & ".xlsx" DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "plates_stock", fd.SelectedItems(1) & "/" & Me.Text24.Value & "_Recovery_" & todaysDate & ".xlsx" Set xl = CreateObject("Excel.Application") Set wb = xl.Workbooks.Open(fd.SelectedItems(1) & "/" & Me.Text24.Value & "_Recovery_" & todaysDate & ".xlsx") xl.DisplayAlerts = False xl.Visible = True '========================================== Excel Worksheet Formatting ========================================== With wb .Sheets("plates_stock").Select .ActiveSheet.Columns("A:G").Cut .Sheets("plates_sold").Select .Sheets(1).Name = Me.Text24.Value & " Recovery" .ActiveSheet.Range("I1").Select .ActiveSheet.Paste .Sheets("plates_stock").Delete With .ActiveSheet.Range("I1:O1") .Font.Bold = True .Borders(xlDiagonalDown).LineStyle = xlNone .Borders(xlDiagonalUp).LineStyle = xlNone With .Borders(xlEdgeLeft) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With .Borders(xlEdgeTop) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With .Borders(xlEdgeBottom) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With .Borders(xlEdgeRight) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With .Borders(xlInsideVertical) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With .Borders(xlInsideHorizontal) .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With .Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .Color = 11711154 .TintAndShade = 0 .PatternTintAndShade = 0 End With End With 'format stock prices to price With .ActiveSheet .Range(.Range("K2:L2"), .Range("K2:L2").End(xlDown)).NumberFormat = "$#,##0.00" End With 'sold total price in With .ActiveSheet .Range("B2").Select End With With .ActiveSheet .Range("B" & .Cells.Rows.Count).End(xlUp).Offset(1, 0).Activate End With '~~~~~~~~~~~WITH BLOCK NOT SET~~~~~~~~~~~ With ActiveCell .formula = "=SUM(B2:" & ActiveCell.Offset(-1, 0).Address & ")" .Font.Bold = True End With '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 'sold total price out Range("C2").Select ActiveCell.End(xlDown).Offset(1, 0).Select ActiveCell.formula = "=SUM(C2:" & ActiveCell.Offset(-1, 0).Address & ")" ActiveCell.Font.Bold = True 'sold total income Range("D2").Select ActiveCell.End(xlDown).Offset(1, 0).Select ActiveCell.formula = "=SUM(D2:" & ActiveCell.Offset(-1, 0).Address & ")" ActiveCell.Font.Bold = True 'stock total price in Range("K2").Select ActiveCell.End(xlDown).Offset(1, 0).Select ActiveCell.formula = "=SUM(K2:" & ActiveCell.Offset(-1, 0).Address & ")" ActiveCell.Font.Bold = True 'Insert ROS % Range("C2").End(xlDown).Offset(2, 0).Select ActiveCell.Value = "ROS" ActiveCell.Offset(0, 1).Select ActiveCell.formula = "=(" & ActiveCell.Offset(-2, 0).Address & "/" & ActiveCell.Offset(-2, -1).Address & ")" ActiveCell.NumberFormat = "0.00%" ActiveCell.Font.Bold = True 'Insert Total Spent ActiveCell.Offset(3, -1).Select ActiveCell.Value = "Total Spent" ActiveCell.Offset(0, 1).Select ActiveCell.formula = "=(" & ActiveCell.Offset(-5, -2).Address & "+" & Range("K2").End(xlDown).Address & ")" ActiveCell.NumberFormat = "$#,##0.00" ActiveCell.Font.Bold = True 'Insert Recovery % ActiveCell.Offset(-2, -1).Select ActiveCell.Value = "Recovery" ActiveCell.Offset(0, 1).Select ActiveCell.formula = "=(" & ActiveCell.Offset(-3, 0).Address & "/" & ActiveCell.Offset(2, 0).Address & ")" ActiveCell.NumberFormat = "0.00%" ActiveCell.Font.Bold = True 'autofit columns Columns("A:A").EntireColumn.AutoFit Columns("B:B").EntireColumn.AutoFit Columns("C:C").EntireColumn.AutoFit Columns("D:D").EntireColumn.AutoFit Columns("E:E").EntireColumn.AutoFit Columns("F:F").EntireColumn.AutoFit Columns("I:I").EntireColumn.AutoFit Columns("J:J").EntireColumn.AutoFit Columns("K:K").EntireColumn.AutoFit Columns("L:L").EntireColumn.AutoFit Columns("M:M").EntireColumn.AutoFit Columns("N:N").EntireColumn.AutoFit Columns("O:O").EntireColumn.AutoFit Range("A1").Select End With '====================================================================================================== wb.Save Set xl = Nothing End If DoCmd.SetWarnings True End Sub 

没有保证我得到了所有偏移正确的,但这应该给你一个如何操作单元格而不select它们的想法:

 Private Sub cmdExport_Click() Dim xl As Excel.Application Dim wb As Object Dim todaysDate As String Dim fd As FileDialog Dim FolderChosen As Integer DoCmd.SetWarnings False todaysDate = Replace(Date, "/", "") Set fd = Application.FileDialog(msoFileDialogFolderPicker) FolderChosen = fd.Show If FolderChosen <> -1 Then MsgBox ("Export Cancelled") Else CurrentDb.QueryDefs("plates_sold").Sql = "SELECT plates.Plate, plates.[Price In], plates.[Price Out], (plates.[Price Out] - plates.[Price In]) AS Income, dbo_Sales.[Date Deposit Rec] AS [Deposit Paid], dbo_Sales.[Comm By Who] AS [Sold By] " & _ "FROM plates INNER JOIN dbo_Sales ON plates.Plate = dbo_Sales.Plate " & _ "WHERE plates.[Plate Status] = 'F' AND dbo_Sales.[Date Deposit Rec] IS NOT NULL" DoCmd.OutputTo acOutputQuery, "plates_sold", "Excel Workbook (*.xlsx)", fd.SelectedItems(1) & "/" & Me.Text24.Value & "_Recovery_" & todaysDate & ".xlsx" DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel12Xml, "plates_stock", fd.SelectedItems(1) & "/" & Me.Text24.Value & "_Recovery_" & todaysDate & ".xlsx" Set xl = CreateObject("Excel.Application") Set wb = xl.Workbooks.Open(fd.SelectedItems(1) & "/" & Me.Text24.Value & "_Recovery_" & todaysDate & ".xlsx") xl.DisplayAlerts = False xl.Visible = True '========================================== Excel Worksheet Formatting ========================================== With wb .Sheets("plates_stock").Columns("A:G").Cut Destination:=.Sheets("plates_sold").Range("I1") .Sheets("plates_sold").Select .Sheets(1).Name = Me.Text24.Value & " Recovery" .Sheets("plates_stock").Delete With .ActiveSheet.Range("I1:O1") .Font.Bold = True With .Borders .LineStyle = xlContinuous .ColorIndex = 0 .TintAndShade = 0 .Weight = xlThin End With With .Interior .Pattern = xlSolid .PatternColorIndex = xlAutomatic .Color = 11711154 .TintAndShade = 0 .PatternTintAndShade = 0 End With End With 'format stock prices to price With .ActiveSheet .Range(.Range("K2:L2"), .Range("K2:L2").End(xlDown)).NumberFormat = "$#,##0.00" With .Range("B" & .Rows.Count).End(xlUp).Offset(1, 0).Resize(, 3) .FormulaR1C1 = "=SUM(R2C:R[-1]C)" .Font.Bold = True End With '~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 'stock total price in With .Range("K2").End(xlDown).Offset(1, 0) .FormulaR1C1 = "=SUM(R2C:R[-1]C)" .Font.Bold = True End With 'Insert ROS % With .Range("C2").End(xlDown).Offset(2, 0) .Value2 = "ROS" With .Offset(0, 1) .FormulaR1C1 = "=R[-2]C/R[-2]C[-1]" .NumberFormat = "0.00%" .Font.Bold = True End With 'Insert Total Spent With .Offset(3, 0) .Value2 = "Total Spent" With .Offset(0, 1) .FormulaR1C1 = "=R[-5]C[-2]+" & .Range("K2").End(xlDown).Address(ReferenceStyle:=xlR1C1) .NumberFormat = "$#,##0.00" .Font.Bold = True End With End With 'Insert Recovery % With .Offset(1, 0) .Value2 = "Recovery" With .Offset(0, 1) .FormulaR1C1 = "=R[-3]C/R[2]C" .NumberFormat = "0.00%" .Font.Bold = True End With End With End With 'autofit columns .Columns("A:F").EntireColumn.AutoFit .Columns("I:O").EntireColumn.AutoFit End With End With '====================================================================================================== wb.Save Set xl = Nothing End If DoCmd.SetWarnings True End Sub