逐行获取数据的第一个单元格

时间:2017-02-14 19:35:17

标签: excel vba

Excel Data

图片是我正在玩的excel数据。我稍后会附上我的代码。但是我试图用H-A列中每行的第一个找到的单元填充H列。防爆。对于第1行,它应该找到“B”并将其放置到H,第2行应该将“c”放置到“H”,依此类推,第3行“是”到H,第4行“a”到H。

我不能为我的生活弄清楚这一点。 VBA从来就不是我最强的套装,我现在已经玩了2天了。这是我的代码。

{!! Form::model($mymodel,array('url'=>'myurl','method'=>'POST')) !!}
    {!!  Form::email('mail', $value = null, $attributes = array('class'=>'form-control')) !!}
    {!! Form::text('Username', '', array('class' => 'form-control')) !!}
{!! Form::close() !!}

非常感谢任何帮助。

3 个答案:

答案 0 :(得分:1)

公式为:

=INDEX(A1:E1,AGGREGATE(15,6,COLUMN(A1:E1)/(A1:E1<>""),1))

enter image description here

答案 1 :(得分:0)

如果这是一个UDF,我相信以下代码就是你的目标:

Function findValue() As String
    Application.Volatile = True
    Dim r As Long
    Dim c As Long
    r = Application.Caller.Row
    For c = 1 To 5
        If Not IsEmpty(Cells(r, c)) Then
            findValue = Cells(r, c).Value
            Exit Function
        End If
    Next
    findValue = ""
End Function

另一种方法,您传递要检查的范围而不是仅检查当前行,将是:

Function findValue(rng As Range) As String
    Dim c As Range
    For Each c In rng
        If Not IsEmpty(c) Then
            findValue = c.Value
            Exit Function
        End If
    Next
    findValue = ""
End Function

然后可以在单元格H2中将其用作=findvalue(A2:E2),并且具有不需要标记Volatile的优点。 (&#34;挥发性&#34;每次工作表上发生任何变化时都必须重新计算。)

P.S。我强烈建议您使用Excel公式(例如Scott's answer中的那个) - 为什么在Excel已经提供功能时重新发明轮子?

答案 2 :(得分:0)

我不在我的电脑上,所以无法测试,但你可以试试这个

Sub FindValue()
    Dim myRow As Range

    ' Sets Range of 5 columns to search in by column
    Set rng = Intersect(Range("A:E"),ActiveSheet.UsedRange)

    ' searches through count of rows
    For each myRow in rng.Rows
        Cells(myRow.Row, "H").Value = myRow.Cells.SpecialCells(xlCellTypeConstants).Cells(1)
    Next
End Sub