简体   繁体   English

VBA#值! VBA功能错误

[英]VBA #value! error in VBA function

so I am attempting to write a function in VBA which will read the value of a cell (CE4) in another spreadsheet and if that value is equal to "Denied", then it will read the value of CE5 and on and on until it reaches a cell in column CE where the value is "Approved", at which point it will output the value of another cell in the same row (in column B). 因此,我尝试在VBA中编写一个函数,该函数将在另一个电子表格中读取单元格(CE4)的值,如果该值等于“拒绝”,则它将连续读取CE5的值,直到达到CE列中值“已批准”的单元格,此时它将输出同一行(B列)中另一个单元格的值。 The idea is that I will input the cell references for the starting cells into the formula and if the initial value for CE is "Denied" it will do the process I described above, otherwise it will output the value for the corresponding cell in column B. I have the following code: 想法是,我将在公式中输入起始单元格的单元格引用,如果CE的初始值为“拒绝”,则将执行上述过程I,否则将在B列中输出对应单元格的值我有以下代码:

Function transfer(ApplicationCell As String, ConditionCell As String) As String

Dim i As Integer

i = 1
With Worksheets("Application Report")
    If .Range(ConditionCell).Value = "Denied" Then
        Do While .Range(ConditionCell).Value = "Denied"
            .Range(ConditionCell).Value = .Range(ConditionCell).Offset(i, 0).Value
            .Range(ApplicationCell).Value = .Range(ApplicationCell).Offset(i, 0).Value
            i = 1 + 1
        Loop
    End If
    transfer = .Range(ApplicationCell).Value
End With

End Function

The initial value for ApplicationCell is B4 and the initial value for ConditionCell is CE4. ApplicationCell的初始值为B4,ConditionCell的初始值为CE4。 However, whenever I attempt to input the values into the function, I get #VALUE!. 但是,每当我尝试将值输入到函数中时,都会得到#VALUE!。 I know I'm doing something wrong but I am new to VBA so I am unsure of where I am going wrong. 我知道我做错了事,但是我是VBA的新手,所以我不确定自己要去哪里。 Any help would be appreciated! 任何帮助,将不胜感激!

You could always use the Worksheet_Change event to handle your updating. 您可以始终使用Worksheet_Change事件来处理您的更新。

Private Sub Worksheet_Change(ByVal Target As Range)

    Dim i As Integer
    Dim ApplicationCell As Range, ConditionCells As Range
    Set ApplicationCell = Range("B4")

    Application.EnableEvents = False
    If Not Intersect(Target, ApplicationCell) Is Nothing Then
        i = 1
        Set ConditionCells = Worksheets("Application Report").Range("CE4")

        Do While ConditionCells.Value = "Denied"
            ConditionCells.Value = ConditionCells.Offset(i, 0).Value
            ApplicationCell.Value = ApplicationCell.Offset(i, 0).Value
            i = 1 + 1
        Loop

        Worksheets("???").Range("???") = ApplicationCell.Value

    End If
    Application.EnableEvents = True
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM