0

我是 VBA 编程的新手,并且已经编写了一个用于插值的 VBA 函数。我尝试从单元格 C17 和 C 列中的其他单元格获取输入到函数“WindPressure”

然后该函数从 C 列(地表高度 z (m) )获取输入并进行插值以获得设计风压,但由于圆形误差而失败。

代码如下:

Function WindPressure(z As Double) As Double

      Dim Row_Nos As Integer

      Dim x1 As Double
      Dim x2 As Double
      Dim x3 As Double

      Dim y1 As Double
      Dim y2 As Double
      Dim y3 As Double

      ' Select first line of data.
      Range("T13").Select

      ' Set Nos of row to interploate
      Row_Nos = 12

      ' Interpolation for Design Wind pressure
      For i = 0 To Row_Nos

         ' Case 1: <= 5m
         If i = 0 And z <= ActiveCell.Value Then

            WindPressure = ActiveCell.Offset(0, 1).Value

            ' Shifting Back
            ActiveCell.Offset(0, -1).Select

            'Exit If Enter this if statement
            Exit Function

         ElseIf i >= 0 And z <= ActiveCell.Value Then


         ' Case 2: > 5m
            x1 = z

            x2 = ActiveCell.Offset(-1, 0).Value

            x3 = ActiveCell.Offset(2, 0).Value

            y2 = ActiveCell.Offset(-2, 1).Value

            y3 = ActiveCell.Offset(2, 0).Value

            y1 = ((x1 - x3) / (x2 - x3) * (y2 - y3)) + y3
            WindPressure = y1

            ' Shifting Back
            ActiveCell.Offset(-1, -1).Select

            'Exit If Enter this if statement
            Exit Function

         End If
            ActiveCell.Offset(1, 0).Select
      Next i


End Function
  1. 谁能告诉我脚本中的哪一步是错误的
  2. 无论如何方便测试一个功能?因为它不像直接通过单击 F5 按钮执行的程序

非常感谢您的关注和帮助。

4

2 回答 2

2

这是使用F5F8作为过程调试它的方法:

Public Sub TestMe()

    Debug.Print windpressure(7)

End Sub

Function windpressure(z As Double) As Double

    Stop
    'the rest of the function
'    Dim Row_Nos As Integer
'    Dim x1 As Double
'    Dim x2 As Double
'    Dim x3 As Double

End Function

现在TestMe通过按下运行F8并享受调试。


另一种方法是在即时窗口中写入?WindPressure(7)并在 VB 编辑器中放置一个停止符号。它将逐行进行。


关于错误,请Select从函数中删除部分,VBA 不允许您在那里使用它。

于 2017-09-12T08:47:09.890 回答
1

正如@YowE3K 所指出的,当函数尝试修改环境时,调试无济于事。

因此,您的代码尝试选择T13它在 UDF 中无法执行的范围。在此之后,任何对 的引用ActiveCell都是错误的,并且尝试更改活动单元格会失败,因为您无法修改环境。

附带说明一下,您也没有放在Option Explicit模块的顶部,否则它也会抱怨您没有声明变量i

因此,与其更改ActiveCell只是设置对您需要的单元格的引用。每当您使用ActiveCell该范围参考时。

Option Explicit

Function WindPressure(z As Double) As Double

      Dim Row_Nos As Integer

      Dim x1 As Double
      Dim x2 As Double
      Dim x3 As Double

      Dim y1 As Double
      Dim y2 As Double
      Dim y3 As Double

      Dim i As Long
      Dim rT13 As Range

      ' Select first line of data.
      'Range("T13").Select '**** Won't work in a UDF ****
      Set rT13 = Range("T13") 'Set a reference to T13 instead.


      ' Set Nos of row to interploate
      Row_Nos = 12

      ' Interpolation for Design Wind pressure
      For i = 0 To Row_Nos

         ' Case 1: <= 5m
         If i = 0 And z <= rT13.Value Then

            WindPressure = rT13.Offset(0, 1).Value

            'Exit If Enter this if statement
            Exit Function

         ElseIf i >= 0 And z <= rT13.Value Then


         ' Case 2: > 5m
            x1 = z

            x2 = rT13.Offset(-1, 0).Value

            x3 = rT13.Offset(2, 0).Value

            y2 = rT13.Offset(-2, 1).Value

            y3 = rT13.Offset(2, 0).Value

            y1 = ((x1 - x3) / (x2 - x3) * (y2 - y3)) + y3
            WindPressure = y1

            'Exit If Enter this if statement
            Exit Function

         End If
      Next i

End Function

要检查您的代码,您可以放置​​一个断点Range("T13").Select并添加一个手表ActiveCell.Address- 当代码到达该行时,您会看到ActiveCell仍然是您输入公式的单元格。

于 2017-09-12T09:18:00.063 回答