我为MS Access数据库中的按钮分配了一个宏,用于打开和更新链接到数据库的MS Excel电子表格。我已成功处理其他所有运行,这意味着在第一个按钮单击它成功运行100%。然后我关闭工作簿而不保存并尝试再次运行它,我收到错误91代码。我一直在研究并添加了范围等,但我仍然在完全相同的行中得到此运行时错误。 ActiveCell.Value = myDate错误后还有其他代码(因此有额外的定义)但是现在我主要关注这个。我希望用户基本上能够点击按钮并运行他们自己的报告,而无需我在办公室协助LOL
Private Sub BtnExport_Click()
Dim appExcel As Excel.Application
Dim myWorkbook As Excel.Workbook
Dim wsCharts As Worksheet, wsResults As Worksheet, wsTable As Worksheet
Dim OriginalSheet As String, myDate As String, FolderPath As String, _
DollarAccuracy As String, IRA As String, Dollars As String, _
Counts As String, Cell1 As String, Cell1Minus As String
Dim TotalDollarVar As Long, TotalDollar As Long, TotalSame As Long,
TotalAll As Long, _
lastRow1 As Long, lastRow2 As Long, lastRow3 As Long, lastRow4 As Long
'Definitions
FolderPath = "C:\MyPath\Reports"
myDate = Format(DateAdd("m", -1, Date), "mmmm yyyy") 'Set the date for last month
'Open the spreadsheet
Set appExcel = CreateObject("Excel.Application")
Set myWorkbook = appExcel.Workbooks.Open("C:\MyPath\Spreadsheet title.xlsx")
appExcel.Visible = True
Set wsCharts = myWorkbook.Worksheets("Charts")
Set wsResults = myWorkbook.Worksheets("Results")
Set wsTable = myWorkbook.Worksheets("Table")
wsResults.Range("A2").Value = "Results " & myDate 'Put last month's date with the results
wsResults.ListObjects("Table_Name1").Range.AutoFilter _
Field:=1, Criteria1:=xlFilterLastMonth, Operator:= _
xlFilterDynamic ' Filters the results table to last month
wsTable.ListObjects("Table_Name2").Range.AutoFilter _
Field:=1, Criteria1:=xlFilterLastMonth, Operator:= _
xlFilterDynamic 'Filters the data table to last month
wsCharts.Activate ' Make the charts tab active
wsCharts.Columns("B:C").EntireColumn.Hidden = False 'Unhide the columns for entry
Dim r As Range
Set r = wsCharts.ListObjects("DollarAccuracy").Range.Columns(1).Cells.Find _
("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
If Not r Is Nothing Then
lastRow1 = r.Row
DollarAccuracy = CStr(lastRow1)
Dim StrDollar As String
StrDollar = "A" & DollarAccuracy
Dim rDollar As Range
Set rDollar = wsCharts.Range(StrDollar)
If Not rDollar Is Nothing Then
rDollar.Offset(1, 0).Select
ActiveCell.Value = myDate
Selection.NumberFormat = "mmmm" 'Add last month to the "A" column for Dollar Accuracy
End If
End If
新代码:
Dim xlApp As Excel.Application
Dim xlWB As Excel.Workbook
Dim wsCharts, wsResults, wsTable As Worksheet
Dim myDate, FolderPath, strDAa As String
Dim SummaryRow, LastDARow, NextDARow As Long
Dim rDA, rDAa As Range
'Definitions
myDate = Format(DateAdd("m", -1, Date), "mmmm yyyy") 'Set the date for last month
Set xlApp = New Excel.Application
With xlApp
.Visible = True
Set xlWB = .Workbooks.Open("C:\Path\Workbook.xlsx", , False)
End With
Set wsCharts = xlWB.Worksheets("Charts")
Set wsResults = xlWB.Worksheets("Results")
Set wsTable = xlWB.Worksheets("Table")
wsResults.Range("A2").Value = "Results " & myDate 'Put last month's date with the results
wsResults.ListObjects("CycleCountResults").Range.AutoFilter _
Field:=1, Criteria1:=xlFilterLastMonth, Operator:= _
xlFilterDynamic ' Filters the results table to last month
wsTable.ListObjects("PartsData").Range.AutoFilter _
Field:=1, Criteria1:=xlFilterLastMonth, Operator:= _
xlFilterDynamic 'Filters the data table to last month
wsCharts.Columns("B:C").EntireColumn.Hidden = False 'Unhide the columns on the table tab for entry
Set rDA = wsCharts.ListObjects("DollarAccuracy").Range.Columns(1).Cells.Find _
("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious)
If Not rDA Is Nothing Then
LastDARow = rDA.Row 'Define the row number for the last entry
NextDARow = LastDARow + "1" 'Define the next blank row
strDAa = "A" & NextDARow 'Define the date cell
Set rDAa = Range(strDAa)
答案 0 :(得分:0)
您尚未使用ActiveCell
定义要引用的Excel实例,因此它尝试选择原始实例中的单元格,而不是第二次打开工作簿的实例
如果您不使用Select
,只需说出
rDollar.Offset(1, 0).Value = myDate
rDollar.Offset(1, 0).NumberFormat = "mmmm" 'Add last month to the "A" column for Dollar Accuracy
你可能会没事的。
您还应检查以确保正确关闭Excel实例(未显示如何或如果您在发布的代码中执行此操作)并且您应该考虑使用早期绑定而不是后期绑定(如果您打算)无论如何使用早期绑定方法定义所有变量。