vba 填充空格

来源:百度知道 编辑:UC知道 时间:2024/05/30 03:12:05
自定义函数实现 “=if(A2<>"",A2,B1)”功能,急!!!
是这样的,假设A2=1231,A5=1232,A8=1233,那么运用该公式后的结果应该是B2=1231,B3=1231,B4=1231,B5=1232,B6=1232,B7=1232,B8=1233,B9=1233,B10=1233
不过实际函数定义的时候,不一定会是在A列和B列,所以要定义一个在任意行列都可以执行的函数,还有就是一定是个自定义函数,比如:Function ci(x as range)as string ,这样我就可以直接使用ci(x)来实现我要的结果,拜托各位大虾了~

没看懂
还是没说清楚,这两列的相对位置是不是不变?(一直为相邻列吗?)
是不是可以这样理解,如果当前单元格的左一格为空,就取其左一格的上一格,然后再上一格,直到有值为止?如果是这样的话很简单.
等下发上来

'很有趣的问题
Function ci(ColFrom As Range) As String '两列不相邻情况,输入另一列任一单元格,如=ci(A1)
Dim ColofLeftCell As Integer
Dim RowofLeftCell As Integer
Dim i As Integer
ColofLeftCell = ColFrom.Column

RowofLeftCell = Application.Evaluate("row()")(1)

For i = RowofLeftCell To 1 Step -1
If Cells(i, ColofLeftCell).Text <> "" Then
ci = Cells(i, ColofLeftCell).Text
Exit For

End If

Next i

End Function

Function cii() '两列相邻情况,不需要参数=cii()
Dim ColofLeftCell As Integer
Dim RowofLeftCell As Integer
Dim i As Integer

ColofLeftCell = Application.Evaluate("column()")(1)
RowofLeftCell = Application.Evaluate("row()")(1)

For i = RowofLeftCell To 1 Step -1
If Cells(i, ColofLeftCell - 1