Wookbooks.open方法适用于编辑器但不适用于Excel

时间:2015-07-06 12:20:10

标签: excel vba

我正在尝试在VBA中开发一个函数,将结果返回到当前工作表。该函数旨在打开另一个电子表格,提取一些数据,进行一些处理并将值返回到调用该函数的工作表。

当我从"立即"调用该函数时,一切正常。 VBA编辑器中的窗口。但是,当我将调用转移到工作表时,当尝试打开另一个工作簿(AreaBook)时,函数行为会偏离预期。对象AreaBook仍然是指向任何东西的指针。

我已经尝试过编码文件名;再一次,对该函数的调用可以从即时窗口进行,但不能从工作簿调用。

有什么想法吗?

Public Function pointInWhichArea(FileName As String, SheetName As String, areaID As String, ByVal pointLong As Single, ByVal pointLat As Single) As Variant   ', testPointLon As Single, testPointLat As Single) As Variant

Dim a, b, c As Integer
Dim colAreaID, colLat, colLon As Integer
Dim AreaBook As Workbook
Dim AreaSheet As Worksheet
Dim polygonPoints() As pointType
Dim testPoint As pointType
Dim found As Boolean

' extract the point details
testPoint.x = pointLong
testPoint.y = pointLat

' set the workbook and sheet objects
FileName = filePath + FileName                                                  ' open the Area definition file
Set AreaBook = Workbooks.Open(FileName)               ' <<<< PROBLEM HERE
Set AreaSheet = AreaBook.Sheets(SheetName)

a = 1                                                                           ' identify the Polygon ID, latitude and longitude columns column
While AreaSheet.Cells(1, a).Value <> ""
    Select Case Worksheets(SheetName).Cells(1, a).Value
        Case Is = areaID
            colAreaID = a
        Case Is = "Latitude"
            colLat = a
        Case Is = "Longitude"
            colLon = a
    End Select
    a = a + 1
Wend

a = 2                                                                           ' loop through all points in the area list
b = a                                                                           ' remember the beginning of the polygon
found = False
While (AreaSheet.Cells(a, colAreaID).Value <> "" And found = False)

    If AreaSheet.Cells(a, colAreaID).Value <> AreaSheet.Cells(a + 1, colAreaID).Value Then  ' test for the end of this polygon
        c = a                                                                   ' remember the end of the polygon
        ReDim polygonPoints(b To c) As pointType                                ' array to capture the poylgon
        For a = b To c                                                          ' loop through each point
            polygonPoints(a).x = AreaSheet.Cells(a, colLon).Value               ' extract the longitude of the point
            polygonPoints(a).y = AreaSheet.Cells(a, colLat).Value               ' extract the latitude of the point
        Next a
        b = a                                                                   ' remember the beginning of the next polygon

        If pointInArea(testPoint, polygonPoints) = True Then                    ' test if the point is in the current polygon
            pointInWhichArea = AreaSheet.Cells(a - 1, colAreaID).Value          ' return the area label
            found = True
        End If

    Else
        a = a + 1
    End If


Wend
AreaBook.Close

End Function

2 个答案:

答案 0 :(得分:1)

我担心工作表功能无法用于影响其他单元格或工作簿 - 您无法将功能添加到单元格A1并期望结果显示在单元格B2中。 以同样的方式,您可以在单元格A1中添加一个函数,并期望它打开另一个工作簿以获得答案。

这就是为什么它在即时窗口中工作而不是Excel功能的原因。 您可以定义指向其他工作簿的链接,然后引用该工作簿,但是您无法使该功能实际打开其他工作簿。

答案 1 :(得分:0)

工作表函数(以及这些用户定义的变体)在它们可以做什么和不能做什么方面受到限制,这里有一篇关于此事的Microsoft文章的小摘录:

https://support.microsoft.com/en-us/kb/170787

  

工作表单元格中的公式调用的用户定义函数不能   更改Microsoft Excel的环境。这意味着这样的   功能不能执行以下任何操作:

     
      
  • 在电子表格中插入,删除或格式化单元格。
  •   
  • 更改另一个单元格的值。
  •   
  • 移动,重命名,删除或向工作簿添加工作表。
  •   
  • 更改任何环境选项,例如计算模式或屏幕视图。
  •   
  • 将名称添加到工作簿。
  •   
  • 设置属性或执行大多数方法。
  •   

该页面继续说明:

  

应通过使用Visual Basic子例程进行任何环境更改。

因此,简而言之,您无法使用工作表UDF ​​(用户定义函数)进行操作,并且需要将其更改为子例程。