所以我试图在VBA中编写一个函数,它将在另一个电子表格中读取一个单元格(CE4)的值,如果该值等于“Denied”,那么它将读取CE5的值,然后打开直到它到达CE列中的单元格,其值为“已批准”,此时它将输出同一行中另一个单元格的值(在B列中)。我的想法是我将起始单元的单元格引用输入到公式中,如果CE的初始值为“Denied”,它将执行我上面描述的过程,否则它将输出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
ApplicationCell的初始值为B4,ConditionCell的初始值为CE4。但是,每当我尝试将值输入函数时,我都会得到#VALUE!。我知道我做错了,但我是VBA的新手,所以我不确定自己哪里出错了。任何帮助,将不胜感激!
答案 0 :(得分:0)
您始终可以使用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