我有一个on click子例程,可以将两个表导出到excel。 然后我记录了一个excel宏并复制了代码以便相应地格式化工作表。
我的问题是每次运行代码我都会收到此错误: “运行时错误'91':对象变量或未设置块变量”
这发生在我的代码的这一点上:
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的范围是动态的,这就是我在公式中将单元格偏移1的原因, 我试过了:
.formula = "=SUM(B2:B3)"
但这会产生同样的错误。
完整的代码如下所示,我一直在评论'Excel工作表格式化'的第一行收到此错误,并通过在代码中添加With块来绕过它,但无法绕过.formula = line
我对此非常感兴趣,非常感谢任何帮助
完整代码:
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
答案 0 :(得分:1)
不保证我得到的所有偏移都是正确的,但这应该让你知道如何操作单元而不选择它们:
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