使用差异单元格中多个工作表的值更新摘要表

时间:2017-02-06 10:28:03

标签: excel vba excel-vba

我对VBA很新,我试图在下面编写代码,但是我遇到了类型不匹配错误(下面突出显示)。我想要实现的目标如下:

我在第一个名为“工作表”的摘要表上列出了A列的属性(所有链接到各自的工作表)

  1. 对于每个属性,请转到该属性表
  2. 将值3单元格右侧复制到包含字符串“此属性的总计”
  3. 的单元格
  4. 切换回“工作表”并将值粘贴到D列,位于A列中相应的属性名称旁边。
  5. 我认为问题是我引用其他工作表中的值的方式,但我似乎无法找到任何地方如何引用相对于具有特定文本的单元格的某个位置的值。

    先谢谢你了!

    Sub Summary()
    
        Dim MasterBook As Workbook
        Dim Sht As Worksheet
        Dim Rng, Rng2 As Range
    
        Set MasterBook = ThisWorkbook
        Set Sht = MasterBook.Worksheets("Sheet")
        Set Rng = Sht.Range("A6:A" & Sht.Cells(Sht.Rows.Count, "A").End(xlUp).Row)
    
        Dim Cell As Range
    
        For Each Cell In Rng
        Cell.Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True
        Cell.Offset(0, 3).Value = Cell.Value("Total for this Property").Offset(0, 3).Value  '<---- This line is giving the error
    
        Next Cell
    End Sub
    

4 个答案:

答案 0 :(得分:4)

试试这个

Sub Summary()

Dim MasterBook As Workbook
Dim Sht As Worksheet
Dim Rng As Range, Rng2 As Range, Rng3 As Range

Set MasterBook = ThisWorkbook
Set Sht = MasterBook.Worksheets("Sheet")
Set Rng = Sht.Range("A6:A" & Sht.Cells(Sht.Rows.Count, "A").End(xlUp).Row)

Dim Cell As Range

For Each Cell In Rng
    Set Rng3 = MasterBook.Sheets(Cell.Text).Cells.Find(What:="Total for this Property", LookIn:=xlValues, LookAt:=xlWhole, _
                 MatchCase:=False, SearchFormat:=False)
    If Not Rng3 Is Nothing Then
        Cell.Offset(0, 3).Value = Rng3.Offset(, 3).Value
    Else
        MsgBox "not found"
    End If
Next Cell

End Sub

答案 1 :(得分:3)

难怪这条线给你带来麻烦:

Cell.Offset(0, 3).Value = Cell.Value("Total for this Property").Offset(0, 3).Value

Value属性不会带这样的参数!我假设你正在尝试找到带有文本Total for this Property并从中偏移3的单元格?

在这种情况下,您应该查看Find命令。

Dim myFoundCell as Range
Set myFoundCell = ActiveWorkbook.ActiveSheet.UsedRange.Find(what:="Total for this Property", lookat:=xlWhole)
If Not myFoundCell Is Nothing Then
    Cell.Offset(0, 3).Value = myFoundCell.Offset(0,3).Value
End If

我认为您在处理了大量超链接之后也无法处理您(ActiveWorkbook)中的哪个工作簿,因此请查看ActiveWorkbook和{{1} }。我的意思是,每个范围必须在给定工作簿的表单内。您尝试复制工作簿,因此必须指定数据的来源/来源。

ThisWorkbook的文档:https://msdn.microsoft.com/en-us/library/office/ff839746.aspx

或许你被困住的另一个问题。

答案 2 :(得分:1)

我猜你是在这之后:

Sub Summary()
    Dim Cell As Range, foundCell As Range

    With ThisWorkbook.Worksheets("Sheet")
        For Each Cell In .Range("A6", .Cells(.Rows.count, "A").End(xlUp))
            Cell.Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True
            Set foundCell = ActiveSheet.UsedRange.Find(what:="Total for this Property", lookat:=xlWhole, LookIn:=xlValues)
            If Not foundCell Is Nothing Then Cell.Offset(0, 3).Value = foundCell.Offset(0, 3).Value  '<---- This line is giving the error
        Next Cell
        .Activate
    End With
End Sub

答案 3 :(得分:0)

由于您要从证书Worksheet(名称放在单元格中)而不是整个Workbook中“获取”值,您需要使用Find特定Worksheet

Find方法的语法是:

Set FindRng = Worksheets(Cell.Value).Cells.Find("Total for this property")

子循环代码

Dim Cell As Range, FindRng As Range

For Each Cell In Rng
    Cell.Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True

    Set FindRng = Worksheets(Cell.Value).Cells.Find("Total for this property")

    If Not FindRng Is Nothing Then    ' verify that Find was successful
        Cell.Offset(0, 3).Value = FindRng.Offset(0, 3).Value          
    End If
Next Cell