在Excel中开发一个函数,创建一个简单的回归

5sxhfpxr  于 2023-10-22  发布在  其他
关注(0)|答案(1)|浏览(108)

我试图在Excel中创建一个函数,当接收到一列数据和图形标题时,返回Excel中简单回归的图形。这是密码我已经尝试更改代码,但我仍然有相同的值错误。

Function ModeloEstimacionUnicaColumna(ColumX As Range, TitleGraphic As String) As Boolean
    
    If ColumnaX Is Nothing Then
        MsgBox "La columna de datos está vacía."
        ModeloEstimacionUnicaColumna = False
        Exit Function
    End If
    
    
    Dim DatosX As Variant
    Dim Modelo As Object
    Set Modelo = CreateObject("Excel.Application")

    
    DatosX = ColumnaX.Value

    
    Modelo.Workbooks.Add
    Modelo.ActiveSheet.Cells(1, 1).Resize(UBound(DatosX), 1).Value = Application.Transpose(DatosX)
    Modelo.Charts.Add
    Modelo.ActiveChart.SetSourceData Source:=Modelo.ActiveSheet.Range("Sheet1!$A$1:$A$" & UBound(DatosX))
    Modelo.ActiveChart.ChartType = xlXYScatterLines
    Modelo.ActiveChart.Location Where:=xlLocationAsNewSheet
    Modelo.ActiveChart.HasTitle = True
    Modelo.ActiveChart.ChartTitle.Text = TitleGraphic

    
    ModeloEstimacionUnicaColumna = True
End Function

我不确定是否有一些Excel库,我没有申请,或者如果我在代码中犯了一些严重的语法错误。我希望,一旦我给给予函数所需的变量,它将返回一个简单的回归所需的图形。

a9wyjsp7

a9wyjsp71#

它似乎有一些问题;
1.“列X”和“列X”我用“列X”是为了保持一致
1.“Modelo”对象,使用现有的Excel应用程序,而不是创建一个新的。
1.源数据参考“Sheet 1!$A$1:$A$”& UBound(DatosX)。

Function ModeloEstimacionUnicaColumna(ColumnX As Range, TitleGraphic As String) As Boolean
    
        If ColumnX Is Nothing Then
            MsgBox "La columna de datos está vacía."
            ModeloEstimacionUnicaColumna = False
            Exit Function
        End If
        
       
        If ColumnX.Columns.Count > 1 Then
            MsgBox "La columna de datos no es válida. Debe ser una única columna."
            ModeloEstimacionUnicaColumna = False
            Exit Function
        End If
        
        Dim DatosX As Variant
        Dim ChartSheet As Worksheet
        Dim MyChart As ChartObject
        
        DatosX = ColumnX.Value
        Set ChartSheet = ThisWorkbook.Sheets.Add
        Set MyChart = ChartSheet.ChartObjects.Add(Left:=10, Width:=375, Top:=75, Height:=225)
        
        ' Set source data directly.
        MyChart.Chart.SetSourceData Source:=ColumnX
        
        
        MyChart.Chart.ChartType = xlXYScatterLines
        MyChart.Chart.HasTitle = True
        MyChart.Chart.ChartTitle.Text = TitleGraphic
        
        ModeloEstimacionUnicaColumna = True
    End Function

相关问题