Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Function VBA函数未引用正确的工作表_Function_Vba_Excel - Fatal编程技术网

Function VBA函数未引用正确的工作表

Function VBA函数未引用正确的工作表,function,vba,excel,Function,Vba,Excel,我正在用VBA制作一个数据解析器,用于在另一个程序中输出图表。我制作了这个vba代码,但它没有引用正确的范围 以下是函数: Function HCAlt(Rng1 As Range, Rng2 As Range, Rng3 As Range, Rng4 As Range) As String ' this function will take columns of data and properly format them for highcharts scatter pairs Dim ret

我正在用VBA制作一个数据解析器,用于在另一个程序中输出图表。我制作了这个vba代码,但它没有引用正确的范围

以下是函数:

Function HCAlt(Rng1 As Range, Rng2 As Range, Rng3 As Range, Rng4 As Range) As String
' this function will take columns of data and properly format them for highcharts scatter pairs
Dim retVal As String
Dim i As Integer
Dim startRow As Integer
startRow = Rng1.Row

'if ranges doesn't contains one column and same rows count - return #VALUE error
If Rng1.Rows.count <> Rng2.Rows.count Or _
    Rng1.Columns.count <> 1 Or Rng2.Columns.count <> 1 Or Rng3.Rows.count <> Rng2.Rows.count Then
    HCAlt = CVErr(xlErrValue)
    Exit Function
End If

For i = 0 To Rng1.Rows.count - 1
    retVal = retVal & "{x:" & Rng3.Cells(startRow + i, 1) & ",y:" & Rng4.Cells(startRow + i, 1) & ",samp:'" & Rng1.Cells(startRow + i, 1) & "'" & ",Rock:'" & Rng2.Cells(startRow + i, 1) & "'},"
Next i
'remove last comma
If retVal <> "" Then retVal = Left(retVal, Len(retVal) - 1)

HCAlt = "[" & retVal & "]"
End Function
它所指的单元格是范围:

 Cell M46= PlotData!R55:R141  'this is a range on sheet "PlotData"
 Cell M47= PlotData!A55:A141  'this is a range on sheet "PlotData"
 Cell M61= StData!L55:L141    'this is a range on sheet "StData"
 Cell M65= StData!P55:P141    'this is a range on sheet "StData"
函数“起作用”,但它不是指我希望它指向的单元格(它将垃圾数据返回给我)

我需要申报它所指的表格吗?或者间接端口中的范围值是否已结束,以告知函数要查找它的工作表


谢谢,这是我的问题。我想我需要从范围开始的那一行开始。在我的示例中,它是55,所以我创建了一个变量来获取该行,然后将其添加到单元格行

Dim startRow As Integer
startRow = Rng1.Row

Rng3.Cells(startRow + i, 1) '  this is part of the code where I added this starting row- see above'
我删除了起始行变量并改为使用此变量:

Rng3.Cells(i, 1)
因为我的数据是指相同数据的“重复”表,这些表的排序不同,所以我可以更改我所指的单元格,使它们都在同一张表上

Cell M46= StData!R55:R141  'this is a range on sheet "StData"
Cell M47= StData!A55:A141  'this is a range on sheet "StData"
Cell M61= StData!L55:L141  'this is a range on sheet "StData"
Cell M65= StData!P55:P141  'this is a range on sheet "StData"

这是用正确的值计算出来的。我不确定最后一部分是否有什么不同,但我很谨慎。

我在Excel中遇到了一些使用R1C1表示法效果更好的方法。我无法对您的代码进行具体评论,因为我还没有尝试过。您可以尝试检查函数中的范围地址是否与传递到函数中的地址匹配。一切似乎都正常。如果存在这些表,则正确引用它们。尝试设置一个断点并查看InteractiveRive debug中的值。如何查看它在哪个工作表上。当我使用断点运行时,得到的数据不匹配。为
Rng1-Rng4
和其他变量添加手表。我很难说什么是错的,因为我不知道你期望这个函数返回什么以及它实际返回什么。我可以看到,当我的范围从55到141时,我从第109行而不是第55行获取数据。
Cell M46= StData!R55:R141  'this is a range on sheet "StData"
Cell M47= StData!A55:A141  'this is a range on sheet "StData"
Cell M61= StData!L55:L141  'this is a range on sheet "StData"
Cell M65= StData!P55:P141  'this is a range on sheet "StData"