VBA新手,并且对于应该计算两个数组的斜率并将其放在具有给定地址的列中的代码得到此限定符错误。
到目前为止打败了我,所以得到一些帮助真好!数组的大小是匹配的。
编译错误在.Slope
TargetSheet.Cells(n, (MyRange.Columns.Count) + 1).Value = Application.WorksheetFunction.Slope(TargetSheet.Range(TargetSheet.Cells(n, 5), TargetSheet.Cells(n, MyRange.Columns.Count)), TargetSheet.Range(TargetSheet.Cells(1, 5), TargetSheet.Cells(1, MyRange.Columns.Count))).Value
处给出
Dim n As Long
Dim MyRange As Range
Set MyRange = ActiveSheet.UsedRange
Dim TargetSheet As Worksheet, SourceSheet As Worksheet
Dim TargetBook As Workbook
Set TargetBook = Application.ActiveWorkbook
Set TargetSheet = Application.ActiveSheet
For n = 3 To MyRange.Rows.Count
TargetSheet.Cells(n, (MyRange.Columns.Count) + 1).Value = Application.WorksheetFunction.Slope(TargetSheet.Range(TargetSheet.Cells(n, 5), TargetSheet.Cells(n, MyRange.Columns.Count)), TargetSheet.Range(TargetSheet.Cells(1, 5), TargetSheet.Cells(1, MyRange.Columns.Count))).Value
Next n
End With
End Sub
此版本之前的版本(在此之前无法撤消:/)正在处理,直到它停止并给出限定符错误。我在数据集上使用了Excel的SLOPE函数并进行了比较:它在#DIV / 0结果时给出了这个错误(但是VBA没有显示出来)。
答案 0 :(得分:0)
如上所述,从斜率调用中删除.Value允许它通过编译器。
否则,似乎有效。
此输入......
提供此输出......
使用此代码(稍加修改以获取正确的列)...
Sub main()
Dim n As Long
Dim MyRange As Range
Set MyRange = ActiveSheet.UsedRange
Dim TargetSheet As Worksheet, SourceSheet As Worksheet
Dim TargetBook As Workbook
Set TargetBook = Application.ActiveWorkbook
Set TargetSheet = Application.ActiveSheet
For n = 2 To MyRange.Rows.Count
TargetSheet.Cells(n, (MyRange.Columns.Count) + 1).Value = _
Application.WorksheetFunction.Slope(TargetSheet.Range(TargetSheet.Cells(n, 1), TargetSheet.Cells(n, MyRange.Columns.Count)), _
TargetSheet.Range(TargetSheet.Cells(1, 1), TargetSheet.Cells(1, MyRange.Columns.Count)))
Next n
End Sub
下面是修改后的代码,用于捕获仅存在1 Y值以避免除以零的情况。这假设工作表中的第1行在标题的右侧没有任何内容。
Option Explicit
Sub main()
Dim n As Long
Dim MyRange As Range
Dim nRows As Long, nCols As Long, ColCount As Long
Set MyRange = ActiveSheet.UsedRange
Dim TargetSheet As Worksheet, SourceSheet As Worksheet
Dim TargetBook As Workbook
Set TargetBook = Application.ActiveWorkbook
Set TargetSheet = Application.ActiveSheet
nRows = MyRange.Rows.Count
nCols = MyRange.Rows(1).End(xlToRight)
For n = 2 To nRows
ColCount = Application.CountIf(TargetSheet.Range(TargetSheet.Cells(n, 1), Sheet1.Cells(n, nCols)), """<>""""""")
If ColCount > 1 Then
TargetSheet.Cells(n, (MyRange.Columns.Count) + 1).Value = _
Application.Slope(TargetSheet.Range(TargetSheet.Cells(n, 1), TargetSheet.Cells(n, MyRange.Columns.Count)), _
TargetSheet.Range(TargetSheet.Cells(1, 1), TargetSheet.Cells(1, MyRange.Columns.Count)))
End If
Next n
End Sub
答案 1 :(得分:0)
谢谢@OldUgly!管理以修复错误。 VBA的原因:以相同的格式自动化多个数据表,其中包含不同的编号。列和行。
当只有一个数据点并且一个数组的其余部分为空时,WorksheetFunction.Slope不喜欢它。
我创建了另一个循环来计算每行For
循环内的非空单元格。如果没有。非空细胞的数量是&lt; 2对于特定行,输出将是“数据不足”。
如果不是,则计算斜率值。
解决此问题的另一种不那么费心的方法是在这种特定情况下使用On Error Resume Next
。
Dim n As Long, o As Range, CurrentRow As Range, NonEmptyCellCountRow As Integer
For n = 3 To MyRange.Rows.Count `Within each row, counting non-empty cells
Set CurrentRow = TargetSheet.Range(TargetSheet.Cells(n, 5), TargetSheet.Cells(n, MyRange.Columns.Count))
NonEmptyCellCountRow = 0
For Each o In CurrentRow
If o.Value <> "" Then NonEmptyCellCountRow = NonEmptyCellCountRow + 1
Next o
If NonEmptyCellCountRow < 2 Then _
TargetSheet.Cells(n, (MyRange.Columns.Count) + 1) = "Insufficient Data"
Else
TargetSheet.Cells(n, (MyRange.Columns.Count) + 1) = Application.WorksheetFunction.Slope(TargetSheet.Range(TargetSheet.Cells(n, 5), TargetSheet.Cells(n, MyRange.Columns.Count)), TargetSheet.Range(TargetSheet.Cells(1, 5), TargetSheet.Cells(1, MyRange.Columns.Count)))
End If
Next n