这是我试过的代码。但它被困在寻找细胞(Set RangeObj = Cells.find
)。Excel查找VBA无法正常工作
请检查下面的代码:
Sub CopyFromFile()
Dim MyFile As String
Dim erow
Dim Filepath As String
Filepath = "C:\Users\Nazmul Hossain Akash\Desktop\Play Excel\Final\"
MyFile = Dir(Filepath)
Do While Len(MyFile) > 0
If MyFile = "zmaster.xlsm" Then
Exit Sub
End If
Workbooks.Open (Filepath & MyFile)
Range("B2:D18").Copy
Set RangeObj = Cells(1, "A")
ActiveWorkbook.Close
Set RangeObj = Cells.Find(What:=RangeObj, After:=ActiveCell, _
LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, _
SearchDirection:=xlNext, MatchCase:=False)
If RangeObj Is Nothing Then MsgBox "Not Found" Else RangeObj.Select
ActiveCell.Offset(rowOffset:=2, columnOffset:=0).Activate
ActiveSheet.PasteSpecial
MyFile = Dir
Loop
End Sub
** 1。**编码时始终使用'Option Explicit'!你从来没有''Dim'你的'RangeObj' ** 2 **引用范围时总是**显式**。当你声明'Set RangeObj = Cells(1,“A”)'时,VBA不知道你在讨论哪个工作簿和工作表,所以它会尽力猜测。你应该这样做:'Set RangeObj = ThisWorkBook.Sheets(“Sheet1”)。Cells(1,“A”)'。 ** 3。**我相信你会遇到错误,因为在设置RangeObj之后,你关闭了该工作簿......在引用你需要的工作簿时更加明确。 – ZygD
我不能在这里定义工作表名称,因为每个文件都有一个名称和他们的文件名。所以我现在用这个http://prntscr.com/7gtdn3 –