上周,我多次运行了没有错误的Excel vba代码,但现在遇到运行时错误'91对象变量或未设置块变量的情况。在下面的代码中,它停止在LastRow=SummarySheet.ListObjects("Table1").ListColumns(3).DataBodyRange.Count
从研究来看,我认为错误告诉我 SummarySheet 从未设置过,但是可以在代码中看到。我是vba的新手,如何编码使其可以再次工作?
更新:我已经尝试ActiveWorkbook
ThisWorkbook
和Workbooks("Summary")
都无济于事。对错误的进一步研究使我相信我没有创建有效的对象引用。我已经确认工作表名称在实际的工作簿和代码之间匹配。从到目前为止的答案和我研究过的其他示例代码中,我认为我的观点是正确的。我觉得我错过了一些简单的事情,因为我是新手。
Update2:我知道了。我的错误实际上是在LastRow = SummarySheet.ListObjects("Table1").ListColumns(3).DataBodyRange.Count
行中。 3应该是1。页面上的第三列,但表中的第一列。谢谢您的帮助,它为我提供了解决问题所需的线索。
Sub MergeSelectedPowerSpec()
Dim SummarySheet As Worksheet
Dim FolderPath As String
Dim SelectedFiles() As Variant
Dim NRow As Long
Dim FileName As String
Dim NFile As Long
Dim WorkBk As Workbook
Dim SourceRange As Range
Dim DestRange As Range
Dim Tbl As ListObject
Dim LastRow As Long
Dim UnitNum As String
Dim RightPos As Long
Dim LeftPos As Long
Dim Odo As Long
' Sets Data Entry sheet as the location the merged data will go.
Set SummarySheet = ActiveWorkbook.Worksheets("Data Entry")
' Modify this folder path to point to the files you want to use.
FolderPath = "C:\Users\jziqsb\Desktop\FE Spreadsheet\"
' Set the current directory to the the folder path.
ChDrive FolderPath
ChDir FolderPath
' Open the file dialog box and filter on Excel files, allowing multiple files
' to be selected.
SelectedFiles = Application.GetOpenFilename( _
filefilter:="Excel Files (*.xl*), *.xl*", MultiSelect:=True)
' LastRow gets the number of rows in the table with data.
LastRow = SummarySheet.ListObjects("Table1").ListColumns(3).DataBodyRange.Count
' NRow keeps track of where to insert new rows in the destination workbook.
NRow = LastRow + 3