|
楼主 |
发表于 2023-1-8 16:41
|
显示全部楼层
目前我想到的办法是加入On Error Resume Next....
不知道导师们还有什么好方法
Sub T()
Dim WorkbookToOpen As Workbook
Dim fileName As String, filePath As String
Dim fso As Object, folder As Object
fileName = ThisWorkbook.Worksheets("范本").Range("B4").Value
filePath = "C:\Users\User\Desktop\TEST"
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(filePath)
ThisWorkbook.Worksheets("范本").Range("A15:N200").Clear
Dim img As Object
For Each img In ThisWorkbook.Worksheets("范本").Shapes
If Not img Is Nothing And Not img.TopLeftCell Is Nothing Then
If img.TopLeftCell.Row >= 15 And img.TopLeftCell.Row <= 200 And _
img.TopLeftCell.Column >= 1 And img.TopLeftCell.Column <= 14 Then
img.Delete
End If
End If
Next
If SearchFolder(folder, fileName) <> "" Then
Set WorkbookToOpen = Workbooks.Open(SearchFolder(folder, fileName))
WorkbookToOpen.Worksheets(1).Range("A15:N200").Copy _
Destination:=ThisWorkbook.Worksheets("范本").Range("A15:N200")
WorkbookToOpen.Close
Else
MsgBox "NO " & fileName & " DATA。"
End If
End Sub
Function SearchFolder(ByVal folder As Object, ByVal fileName As String) As String
Dim subfolder As Object, subsubfolder As Object
Dim file As Object
For Each file In folder.Files
If file.Name = fileName & ".xlsx" Then
SearchFolder = file.path
Exit Function
End If
Next file
For Each subfolder In folder.subfolders
Dim path As String
path = SearchFolder(subfolder, fileName)
If path <> "" Then
SearchFolder = path
Exit Function
End If
For Each subsubfolder In subfolder.subfolders
path = SearchFolder(subsubfolder, fileName)
If path <> "" Then
SearchFolder = path
Exit Function
End If
Next subsubfolder
Next subfolder
End Function
|
|