为什么VBA说在初始子对象上找不到方法或数据成员?
我尝试过启用解算器,但似乎不起作用。在代码中的第一个子代码处抛出错误,表示“未找到数据成员”(Private-sub-CmdLaunchStats_-Click)。我将在下面列出我的代码为什么VBA说在初始子对象上找不到方法或数据成员?,vba,Vba,我尝试过启用解算器,但似乎不起作用。在代码中的第一个子代码处抛出错误,表示“未找到数据成员”(Private-sub-CmdLaunchStats_-Click)。我将在下面列出我的代码 Option Explicit Private Sub cmdLaunchStats_Click() Dim shtThisSheet As Worksheet Set shtThisSheet = Application.WorksheetFunction shtThisSheet.Average ("
Option Explicit
Private Sub cmdLaunchStats_Click()
Dim shtThisSheet As Worksheet
Set shtThisSheet = Application.WorksheetFunction
shtThisSheet.Average ("A1:A100")
shtThisSheet.StDev ("A1:A100")
shtThisSheet.Min ("A1:A100")
shtThisSheet.Max ("A1:A100")
Set shtThisSheet.Average("A1:A100") = Average
Set shtThisSheet.StDev("A1:A100") = Standard
Set shtThisSheet.Min("A1:A100") = Min
Set shtThisSheet.Max("A1:A100") = Max
MsgBox Statistics, , False
End Sub
A不是A。您不能为Application.WorksheetFunction对象设置变量
此外,您不能将类似的工作表函数的结果设置为未声明和未分配的变量;甚至相反
您的选项Explicit
规定,在使用之前必须声明所有变量,但没有声明平均值、标准值、最小值和最大值变量。将变量命名为与VBA或工作表函数或保留名称相同的名称也被认为是不好的做法
Private Sub cmdLaunchStats_Click()
Dim shtThisSheet As Worksheet
Dim dAverage As Double, dStandard As Double
Dim dMin As Double, dMax As Double, sStatistics As String
Set shtThisSheet = Worksheets("Sheet1")
With shtThisSheet
With .Range("A1:A100")
dAverage = Application.Average(.Cells)
dStandard = Application.StDev(.Cells)
dMin = Application.Min(.Cells)
dMax = Application.Max(.Cells)
End With
End With
sStatistics = "average: " & dAverage & Chr(10) & _
"standard deviation: " & dStandard & Chr(10) & _
"minimum: " & dMin & Chr(10) & _
"maximum: " & dMax
MsgBox sStatistics, , False
End Sub
虽然可以使用完整的应用程序.WorksheetFunction
,但通常不需要。Application.Average(…
或WorksheetFunction.Average(…
通常都是必需的。A不是。您不能为Application.WorksheetFunction对象设置变量。