【问题标题】:VBA-Excel -- Variable in Cells.FindVBA-Excel -- Cells.Find 中的变量
【发布时间】:2014-03-06 19:44:37
【问题描述】:

我有两个电子表格(wb1 和 wb2)。目标是选择 wb1 的 D 列中的每个值,找到 wb2 的 C 列中的值,然后将一系列单元格(与搜索值相同的行)复制回 wb1。

这是迄今为止我设法整理的代码:

    Dim rng1 As Range, rng2 As Range
    Dim cell as Variant
    Dim cell_val as String    
    Dim wb1 as Workbook, wb2 as Workbook
    Dim sh1 as Worksheet, sh2 as Worksheet

    Sub Find_Copy_Paste()

    set wb1 = Workbooks.Open("c:\explicit\path\to\wb1.xlsm")  <---This fails    
    set wb2 = Workbooks.Open("c:\explicit\path\to\wb2.xlsm")  <---This fails

    Set sh1 = wb1.Open("Inventory")    
    set sh2 = wb2.Open ("Sheet1")

    set rng1 = wb1.sh1.Range("D6:D1702")
    set rng2 = wb2.sh2.Range("C2:C3132")

    For Each cell In rng1
        ActiveCell.Select
        cell_val = Selection.Copy
        Windows(wb2).Activate
        Cells.Find(What:=(cell_val), After:=ActiveCell, 
        LookIn:=xlFormulas, LookAt:=xlPart, SearchOrder:=xlByRows, 
        SearchDirection:=xlNext, MatchCase:=False, SearchFormat:=False).Activate
        ActiveCell.Offset (0,1).Range("A1:AH1").Select
        Application.CutCopyMode = False
        Selection.Copy
        Windows(wb1).Activate
        ActiveCell.Offset(0,1).Range("A1").Select
        ActiveSheet.Paste
        cell_val=""
    Next

    End Sub

不幸的是,我遇到了一个挑战,我怀疑这与两件事有关:1) wb1 和 wb2 变量以及我如何分配它们,以及 2) Cells.Find 中的变量代码(但我对 VBA 还是很陌生,所以我的怀疑可能已经不存在了)。

【问题讨论】:

  • 激活/选择 ..OUCH
  • set wb1 = Workbooks.Open("c:\explicit\path\to\wb1.xlsm") &lt;---This fails - 你能澄清一下吗?仅当您的路径错误时才会失败。
  • 所有set 的大小写应该是正确的Set - 你有没有可能在代码中的某处声明了一个名为set 的变量?
  • set 语句在实际代码中全部大写(我只是在尝试在此窗口中格式化时输入错误)。文件的路径是直接从文件的属性中复制/粘贴的(所以我知道路径是正确的)。不幸的是,我在尝试调试时收到运行时错误“1004”:对象“工作簿”的方法“打开”失败错误消息。
  • 我也尝试将 .Open 从图片中删除(即 Set wb1 = Workbooks("c:\explicit\path\to\file.xlsm") 并且我得到运行时错误9:下标超出范围。

标签: excel vba


【解决方案1】:

试试下面这个,我只用 1 个工作簿模拟了你的目标。如果宏和路径不受信任,您可能无法打开 xlsm 文件。在这里,我只有其中一个处于只读模式(工作簿 2)。

Sub Find_Copy_Paste()
    Dim wb1 As Workbook, wb2 As Workbook
    Dim sh1 As Worksheet, sh2 As Worksheet
    Dim rng1 As Range, rng2 As Range
    Dim cell As Range, FoundCells As Range

    Set wb1 = Workbooks.Open(Filename:="c:\explicit\path\to\wb1.xlsm",ReadOnly:=False)
    Set wb2 = Workbooks.Open(Filename:="c:\explicit\path\to\wb2.xlsm",ReadOnly:=True)

    Set sh1 = wb1.Worksheets("Inventory")
    Set sh2 = wb2.Worksheets("Sheet1")

    Set rng1 = sh1.Range("D6:D1702")
    Set rng2 = sh2.Range("C2:C3132")

    For Each cell In rng1
        If Not IsEmpty(cell) Then
            Set FoundCells = rng2.Find(cell.Value)
            If Not FoundCells Is Nothing Then
                Debug.Print """" & cell.Value & """ found at " & FoundCell.Worksheet.Name & "!" & FoundCell.Address
                ' Copy Found cell to one column on right of cell being searched for
                FoundCells.Copy Destination:=cell.Offset(0, 1)
            End If
        End If
    Next
    Set rng1 = Nothing
    Set rng2 = Nothing
    Set sh1 = Nothing
    Set sh2 = Nothing
    Set wb1 = Nothing
    Set wb2 = Nothing
End Sub

有很多学习 VBA 的好地方,对于 Excel 2010,请查看Excel Developer Reference

【讨论】:

    猜你喜欢
    • 2013-02-02
    • 2011-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-22
    • 1970-01-01
    • 2014-03-15
    • 2017-11-01
    相关资源
    最近更新 更多