Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.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
创建VBA函数_Vba_Excel - Fatal编程技术网

创建VBA函数

创建VBA函数,vba,excel,Vba,Excel,在为VBA项目创建此函数时,我确实需要一些帮助。我不确定我在这里做错了什么。我正在尝试创建一个函数,但是,我不断收到一条错误消息function not valid。我一直在寻找什么可能是错误的,但我无法找出我的错误可能在哪里 Function Duplicate(FirstCell) As Object Dim SecondCell Dim ThirdCell Dim FourthCell SecondCell = ActiveCell.Offset(1, 1).Select ThirdC

在为VBA项目创建此函数时,我确实需要一些帮助。我不确定我在这里做错了什么。我正在尝试创建一个函数,但是,我不断收到一条错误消息function not valid。我一直在寻找什么可能是错误的,但我无法找出我的错误可能在哪里

Function Duplicate(FirstCell) As Object

Dim SecondCell
Dim ThirdCell
Dim FourthCell

SecondCell = ActiveCell.Offset(1, 1).Select
ThirdCell = ActiveCell.Offset(0, 2).Select
FourthCell = ActiveCell.Offset(1, 2).Select


If FirstCell = SecondCell And ThirdCell = FourthCell Then
   Duplicate = "Dup"
Else
   Duplicate = ""

End If
End Function
这个怎么样

Function Duplicate(byVal FirstCell as Range) As String

Dim SecondCell as Range, ThirdCell as Range, FourthCell as Range

Set SecondCell = FirstCell.Offset(1, 1)
Set ThirdCell = FirstCell.Offset(0, 2)
Set FourthCell = FirstCell.Offset(1, 2)


If FirstCell.Value = SecondCell.Value And ThirdCell.Value = FourthCell.Value Then
   Duplicate = "Dup"
Else
   Duplicate = ""

End If
End Function
使用类似于
=replicate(A1)
的方法


我不确定的事情需要澄清:如果
FirstCell=SecondCell
ThirdCell=FourthCell
,您希望返回什么?或者,不管怎样,如果
ThirdCell=FourthCell
你什么都不想返回吗?

也许你会告诉我你想要实现什么??无论如何,如果函数返回一个
对象
,那么要么
设置重复=…
,要么将函数声明为
字符串
变量
嘿,感谢您的反馈。我要做的是打印出Dup,如果FirstCell等于SecondCell,那么第三个Cell等于第四个Cell。我已经将我的函数声明为字符串和变量,但这似乎不起作用。第二个单元格、第三个单元格和第四个单元格应该是当前活动单元格的引用。嘿,谢谢你的帮助。我只想设置一个条件,仅当
FirstCell=SecondCell
ThirdCell=FourthCell
时才返回“Dup”。如果它们不满足这些条件,那么即使
ThirdCell=FourthCell
,我也不想返回任何内容。第一个单元格、第二个单元格、第三个单元格和第四个单元格都是字符串。谢谢!它确实奏效了。我真的很感谢你的帮助。