0
我试图删除矢量中的行,这取决于将返回向量的VBA函数中的另一个向量的值。谢谢。删除向量中的行VBA Excel
例如:
条件= [1; 0; 1; 1; 0](5x1)
Data = [0.6; 0.7; 0.75; 0.81; 0.94](5x1)
预期结果= [0.6; 0.75; 0.81(3X1)
Function RANGEIF(data As Range, condition As Range) As Range
'
' Inputs
'
' data: Vector Nx1 of data
' condition: Vector Nx1 of binairy conditions
'
' Outputs
'
' RANGEIF: Vector Nx1 of specific data
' Variable declaration
Dim nRowData As Integer
Dim nRowCond As Integer
' Input vector size
nRowData = data.Rows.Count
nRowCond = condition.Rows.Count
' Input validation
If nRowData <> nRowCond Then
msg = "Error: Input vector sizes do not match"
MsgBox msg, , "Error", Err.HelpFile, Err.HelpContext
GoTo endnow
End If
' Conditional range
For j = nRowCond To 1 Step -1
If condition(j, 1).Value <> 1 Then
data.Cells(j, 1).Delete
End If
Next j
Set RANGEIF = data
endnow:
End Function
你从工作表单元格运行此作为UDF,或者从另一个VBA程序? UDF不能删除工作表上的单元格。另外,你声明nRowData和nRowCond,但是不要设置它们中的任何一个的值... –
对不起。我忘记了包含矢量大小的代码(nRowData和nRowCond)。现在纠正了。 – Ali
至于功能,是的,我从工作表中运行这个。我想使用两个矢量来创建一个新的矢量RANGEIF。然后我将在一个函数中使用RANGEIF。 例如:AVERAGE(RANGEIF(A1:A5,B1:B5)) 谢谢蒂姆。 – Ali