1

我有一个 excel 工作表文件夹,还有另一个工作表,其中有一列的条目对应于文件夹中工作表的文件名。

工作表名称右侧的列有一个数字,我想将其粘贴到每个相应的工作表中......但它不起作用......这是我到目前为止的代码:

Sub FraisRank()

    Dim folderPath As String
    Dim filename As String
    Dim filenameshort As String
    Dim wb As Workbook
    Dim fraislist As Workbook
    Dim find As Range
    Dim sel As Range

    folderPath = "C:\Users\richard\Desktop\temp" 

    If Right(folderPath, 1) <> "\" Then folderPath = folderPath + "\"

    Set fraislist = Workbooks.Open("C:\Users\richard\desktop\frais list.xlsx")

    filename = Dir(folderPath & "*.*")

    Do While filename <> ""
         Application.ScreenUpdating = False
         Set wb = Workbooks.Open(folderPath & filename)
         filenameshort = Left(filename, Len(filename) - 4)

         Set sel = fraislist.Sheets(1).Range("A1:A164")

             Set find = sel.find(What:=filenameshort, After:=ActiveCell, LookIn:=xlFormulas, _
        LookAt:=xlWhole, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False)

        If find Is Nothing Then
            MsgBox ("Cell " & filenameshort & " not found")

        Else
            find.Offset(, 1).Resize(1, 1).Copy
            ActiveSheet.Range("$H$5").PasteSpecial Paste:=xlPasteValues
        End If

         ActiveWorkbook.Save

         ActiveWorkbook.Close

        filename = Dir
     Loop

     End Sub

目前我收到运行时错误“13”,请在“Set find = ...”部分键入 mismatch。总的来说,我真的不明白如何在“fraislist”工作簿中的选定单元格上运行“.find”......

4

1 回答 1

1

问题ActiveCell在于它总是引用 the ,因此应该避免Activesheet这样的语句。Activecell/Select/Activate/ActiveSheet/Activeworkbook始终创建相关对象并使用它们

有趣的阅​​读

在您的情况下,可能不是指正确的工作表,因此最好完全限定ActiveSheetfraislist.Sheets(1)ActiveCell

如果您更改After:=ActiveCell为,After:=fraislist.Sheets(1).Range("A1")那么您的代码将引用正确的工作表并且它将起作用。

于 2013-11-14T10:24:06.913 回答