简体   繁体   English

Workbook.Open 基于包含在 ThisWorkbook 中的文件路径

[英]Workbook.Open based on filepath contained in ThisWorkbook

when I step through my code (below), it gives me the following error notice:当我单步执行我的代码(如下)时,它会给我以下错误通知:

'Run-time error '1004': '运行时错误'1004':

Application-defined or object-defined error应用程序定义或对象定义的错误

Set MEF_ITD = Workbooks.Open(Workbook.Worksheets("Dashboard").Range("F39").Value)

Set myRangeITD = ActiveWorkbook.Worksheets("Summary").Range("A915:AJ1033")

I substituted "Workbook" with this "ThisWorkbook" on the first line thinking it might be a qualifier issue, however, I received the same error notice.我在第一行用这个“ThisWorkbook”替换了“Workbook”,认为这可能是一个限定符问题,但是,我收到了同样的错误通知。 I am trying to open up a separate workbook whose filepath is contained in cell F39 of ThisWorkbook.我正在尝试打开一个单独的工作簿,其文件路径包含在 ThisWorkbook 的单元格 F39 中。 The second line is attempting to define the range based on the ActiveWorkbook.第二行尝试根据 ActiveWorkbook 定义范围。

Is anyone aware of a potential solution here?有人知道这里有潜在的解决方案吗? Thank you!谢谢!

Except of the missing ThisWorkbook your code looks ok.除了缺少ThisWorkbook之外,您的代码看起来还不错。

BUT: as we don't know your data (eg does file really exist, does sheet exist) it is hard to tell you the reason for the error.但是:由于我们不知道您的数据(例如文件是否真的存在,工作表是否存在),所以很难告诉您错误的原因。

I would suppose to have a generic function that takes workbook-fullname, worksheets name and range-adress - does the necessary validations (file exists, sheet exists) and returns the range you are looking for.我想有一个通用的 function 接受工作簿全名、工作表名称和范围地址 - 进行必要的验证(文件存在,工作表存在)并返回您正在寻找的范围。

Public Function getRangeFromWorkbook(wbSourceFullfilename As String, _
    wsSourceSheetName As String, _
    rgSourceAddress As String) As Range

On Error GoTo err_getRangeFromWorkbook

If Dir(wbSourceFullfilename, vbNormal) = vbNullString Then
    err.Raise vbObjectError, , wbSourceFullfilename & " not found."
End If

Dim wbSource As Workbook
Set wbSource = Workbooks.Open(wbSourceFullfilename)


If Not wbSource Is Nothing Then
    
    Dim wsSource As Worksheet
    On Error Resume Next    'an error is thrown if worksheet doesn't exist - this is the only error that can happen.
    Set wsSource = wbSource.Worksheets(wsSourceSheetName)
    If err <> 0 Then
        err.Raise vbObjectError, , "Sheet " & wsSourceSheetName & " not found in workbook " & wbSource.Name
    End If
    On Error GoTo err_getRangeFromWorkbook

    Dim rgSource As Range
    Set rgSource = wbSource.Worksheets(wsSourceSheetName).Range(rgSourceAddress)
End If

Set getRangeFromWorkbook = rgSource

exit_getRangeFromWorkbook:
    Exit Function
    
err_getRangeFromWorkbook:
    MsgBox err.Description, vbCritical, "get range from  workbook"
    Resume exit_getRangeFromWorkbook
    
End Function

You can then call this function like this:然后你可以像这样调用这个 function:

Public Sub testGetRange()

Dim rgITD As Range
Set rgITD = getRangeFromWorkbook( _
    ThisWorkbook.Worksheets("Dashboard").Range("F39").value, _
    "Summary", "A915:AJ1033")
     
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM