我想将工作表特定列的最后一个单元格复制到母版表,但这给了我未定义对象的错误。我不确定我在哪里弄错了吗?
If fso.GetExtensionName(wbFile.Name) = "xls" Then
Set wb = Workbooks.Open(wbFile.Path)
wsLR = wb.Sheets("financial_report").Cells(Rows.Count, 1).End(xlUp).Row
ThisWorkbook.Sheets("sheet1").Cells(y, 2) = wb.Sheets("financial_report").Cells(wsLR,7)
答案 0 :(得分:1)
以下问题实际上产生了 1004:应用程序定义或对象定义错误,但我在发布此信息时是作为提醒,提醒您在处理较旧的XLS工作簿时应使用正确的显式父级引用。以及较新的XLSX工作簿。
您可能希望运行此代码,以查看其有助于识别原始错误的行号。
您的外部工作簿是具有65536行的XLS。如果ThisWorkbook是XLSX或类似的较新的工作簿,则它具有1048576行。 Rows.Count没有显式的父工作表,并且可能正在尝试从只有65536行的工作表中的第1048576行开始查找。
'at this point, y should be defined as a number greater than zero
If fso.GetExtensionName(wbFile.Name) = "xls" Then
with Workbooks.Open(wbFile.Path)
with .workSheets("financial_report")
wsLR = .Cells(.Rows.Count, 1).End(xlUp).Row
ThisWorkbook.Sheets("sheet1").Cells(y, 2) = .Cells(wsLR, 7).value
end with
.close savechanges:=false
end with
end if
我知道打开外部工作簿应该使它成为ActiveWorkbook和Rows.Count应该默认为ActiveWorkbook的工作表之一上的最大行数,但隐含Rows.Count父项是解决问题的方法。
答案 1 :(得分:1)
检查常数,就可以开始了。如果工作簿没有指定的工作表(“ financial_report”),则会产生错误。
'*******************************************************************************
' Purpose: Copies a cell value from all workbooks in a folder to a column
' in this workbook.
'*******************************************************************************
Sub CopyCellFromWorkbooks()
' Source Folder Path
Const cStrPath As String = _
"C:\"
' Source Worksheet Name/Index
Const cStrSource As Variant = "financial_report"
Const cVntSource As Variant = 7 ' Source Column Letter/Number
' Target Worksheet Name/Index
Const cStrTarget As Variant = "Sheet1"
Const cVntTarget As Variant = 1 ' Target Column Letter/Number
' FSO Objects
Dim objFSO As Object, objFolder As Object, objFile As Object
Dim objTarget As Worksheet ' Target Worksheet (ThisWorkbook)
Dim lngTarget As Long ' Target Column
Dim lngSource As Long ' Source Column
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(cStrPath)
Set objTarget = ThisWorkbook.Sheets(cStrTarget)
For Each objFile In objFolder.Files
If objFSO.GetExtensionName(objFile.Name) = "xls" Then
With Workbooks.Open(objFile.Path).Worksheets(cStrSource)
With objTarget
lngTarget = _
.Cells(.Rows.Count, cVntTarget).End(xlUp).Row + 1
End With
lngSource = .Cells(.Rows.Count, cVntSource).End(xlUp).Row
objTarget.Cells(lngTarget, cVntTarget) _
= .Cells(lngSource, cVntSource).Value
.Parent.Close False
End With
End If
Next
End Sub
'*******************************************************************************
Sub CopyCellFromWorkbooksEDIT()
' Source Folder Path
Const cStrPath As String = _
"C:\"
' Source Worksheet Name/Index
Const cStrSource As Variant = "financial_report"
Const cVntSource As Variant = "Y" ' Source Column Letter/Number
' Target Worksheet Name/Index
Const cStrTarget As Variant = "Sheet1"
Const cVntTarget As Variant = 1 ' Target Column Letter/Number
' FSO Objects
Dim objFSO As Object, objFolder As Object, objFile As Object
Dim objTarget As Worksheet ' Target Worksheet (ThisWorkbook)
Dim lngTarget As Long ' Target Column
Dim lngSource As Long ' Source Column
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(cStrPath)
Set objTarget = ThisWorkbook.Sheets(cStrTarget)
For Each objFile In objFolder.Files
If objFSO.GetExtensionName(objFile.Name) = "xls" Then
With Workbooks.Open(objFile.Path).Worksheets(cStrSource)
With objTarget
lngTarget = _
.Cells(.Rows.Count, cVntTarget).End(xlUp).Row + 1
End With
lngSource = .Cells(.Rows.Count, cVntSource).End(xlUp).Row
objTarget.Cells(lngTarget, cVntTarget) _
= .Cells(2, 7).Value ' (G2)
objTarget.Cells(lngTarget, cVntTarget + 1) _
= .Cells(lngSource, cVntSource).Value
.Parent.Close False
End With
End If
Next
End Sub
'*******************************************************************************
答案 2 :(得分:0)
Sub Macro1_Query()
Dim wb As Workbook, ws As Worksheet
Set fso = CreateObject("Scripting.FileSystemObject")
Set fldr = fso.GetFolder("C:
y = ThisWorkbook.Sheets("sheet1").Cells(Rows.Count, 1).End(xlUp).Row + 1
For Each wbfile In fldr.Files
If fso.GetExtensionName(wbfile.Name) = "xls" Then
With Workbooks.Open(wbfile.Path)
With .Worksheets("financial_report")
wsLR = .Cells(.Rows.Count, 1).End(xlUp).Row
ThisWorkbook.Sheets("sheet1").Cells(y, 2) = .Cells(wsLR, 7).Value
End With
.Close savechanges:=False
End With
wb.Close
End if
Next wbfile
End sub