尝试将范围转换为数组时,如何解决“公式中使用的值是错误的数据类型”错误?

时间:2019-05-30 16:08:39

标签: excel vba

我正在制作udf,将输入的范围转换为数组,如果成功完成,该函数将“工作”返回单元格。但是,它一直返回#VALUE !,并说:

  

“公式中使用的值是错误的数据类型。”

library(tidyverse)
Data1=data.frame(A=runif(20, min = 0, max = 100), B=runif(20, min = 0, max = 250), C=runif(20, min = 0, max = 300))
Data2=data.frame(A=runif(20, min = -10, max = 50), B=runif(20, min = -5, max = 150), C=runif(20, min = 5, max = 200))

#method-1: using plot functions
par(mfrow=c(3,1))
plot(Data1$A, Data2$A)
abline(lm(Data1$A ~ Data2$A))
plot(Data1$B, Data2$B)
abline(lm(Data1$B ~ Data2$B))
plot(Data1$C, Data2$C)
abline(lm(Data1$C ~ Data2$C))
dev.off()

#method-2: using ggplot
ggplot()+
  geom_point(aes(Data1$A,Data2$A))

2 个答案:

答案 0 :(得分:3)

使用:

Function test(rng As Range) As String

    Dim Arr As Variant
    Arr = rng

    test = "worked"

End Function

答案 1 :(得分:1)

将其作为注释输入,但由于内容原因将作为答案。请注意,@ DisplayName提供了适当的答案来解决您的问题。

在您的代码中,您使用了语法错误的'Range()`。以下是适当范围参考的一些示例:

Sheets("Name).Range("A1")  'Uses cell A1 on sheets Name
Sheets("Name").Cells(1,1)  'Just like the above, calls cell A1

i = 1
Sheets("Name).Range("A" & i)  'Ampersand joins the variable i (typically used to iterate through a loop) with the column "A"

Sheets("Name").Range("Cat")  'Uses a named range, where cat is predefined and is on sheets Name

With Sheets("Name")
    Set rng = .Range(.Cells(1,1),.Cells(2,2)) 'Creates a range from A1 to B2... note the dots to make them use the appropriate sheet 
End With
rng.value = "Cat"  'Each cell in the range will have "Cat" input