VBA Excel - 如何从主管理中的超链接文件中检索单元格

时间:2017-06-29 05:27:29

标签: excel vba

我有两个文件夹,其中包含许多租户提交的信息。一个包含报告(excel文件),另一个包含声明(pdfs)。我创建了一个excel管理文件,列出了所有租户。我有另一个列使用以下VBA代码来指示他们是提交报告还是声明,然后超链接到它。

Sub IfFileInFolder()

Dim folderPath As String
Dim folderPath2 As String
Dim Ereports As Range, cell As Range


folderPath = "G:\Programs\Ereports\2017\Reports\"

folderPath2 = "G:\Programs\Declarations\"

With Sheets("Tenant Log")
    Set tenant = .Range("B5", .Cells(.Rows.Count, "B").End(xlUp))
End With

For Each cell In tenant
    If Dir(folderPath & cell.Value & "-report.xlsx") <> "" Then
        cell.Parent.Hyperlinks.Add Anchor:=cell.Offset(0, 14), Address:=folderPath & cell.Value2 & "-report.xlsx"
        cell.Offset(0, 14).Value = "REPORTED"

    ElseIf Dir(folderPath2 & cell.Value & "-declaration.pdf") <> "" Then
        cell.Parent.Hyperlinks.Add Anchor:=cell.Offset(0, 14), Address:=folderPath2 & cell.Value2 & "-declaration.pdf"
        cell.Offset(0, 14).Value = "DECLARED"

    Else
        cell.Offset(0, 14).Value = "INCOMPLETE"

    End If
Next

End Sub

然而,对于报告,我想要复制并粘贴到我的管理文件中列出“REPORTED”的旁边的钱值。我尝试了以下代码(和许多变体)来获取数字,但收到运行时错误'1004'。

Sub retrieve()

With Sheets("Tenant Log")
Set fee = .Range("P5", .Cells(.Rows.Count, "P").End(xlUp))
End With

For Each cell In fee
If cell = "REPORTED" Then
cell.Select
Selection.Hyperlinks(1).Follow NewWindow:=False, AddHistory:=True
Sheets("4. Estimated fees").Range("I15").Select
Selection.Copy
ActiveWindow.Close
cell.Offset(0, 2).PasteSpecial Format:="Unicode Text", Link:=False, _
    DisplayAsIcon:=False, NoHTMLFormatting:=True
Else
    cell.Value2 = "N/A"
End If
Next

End Sub

有人可以告诉我如何避免此错误并获取从每个租户报告中提取的值吗?

1 个答案:

答案 0 :(得分:0)

想出来。由于某种原因,不得不将宏移动到宏表而不是管理文件中。