VBA - 然后在C列中计算行数,然后用方程式填充A,B和P列

时间:2015-06-03 20:26:43

标签: excel vba excel-vba

我有5列。 C栏"帐号"和D"人"是我的数据集。

我想使用VBA来查看我有多少行数据,然后在1中填充该行数。列E"连接"连接"帐户"和"员工"用我的INDEX-MATCH方程填充2)A和B列中的行数。

..我试图在下面列出我的专栏,但它不是以我希望的方式格式化...抱歉

Owner | Comment | Account | Employee | Concatenate
Jay   | Done    | JSMA1   | Sally    | JSMA1 Sally
Will  | Not Done| KLLM4   | Jack     | KLLM4 Jack
Ken   | Done    | BM3R1   | Sam      | BM3R1 Sam

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

试试这个:

Option Explicit

Public Sub fillRanges()

    Dim ur As Range, hdr As Range, conCol As Variant, lRow As Long
    Dim ownCol As Variant, comCol As Variant
    Dim actCol As Variant, empCol As Variant

    Set ur = Sheet1.UsedRange   ' minimal range
    Set hdr = ur.Rows(1)        ' header row
    lRow = ur.Rows.Count        ' last row

    With Application
        ownCol = .Match("Owner", hdr, 0)
        comCol = .Match("Comment", hdr, 0)
        actCol = .Match("Account", hdr, 0)
        empCol = .Match("Employee", hdr, 0)
        conCol = .Match("Concatenate", hdr, 0)
    End With

    If Not IsError(ownCol) And _
        Not IsError(comCol) And _
        Not IsError(actCol) And _
        Not IsError(empCol) And _
        Not IsError(conCol) _
    Then
        With ur
            .Range(.Cells(2, ownCol), .Cells(lRow, ownCol)) = "INDEX-MATCH equation 1"
            .Range(.Cells(2, comCol), .Cells(lRow, comCol)) = "INDEX-MATCH equation 2"
            .Range(.Cells(2, conCol), .Cells(lRow, conCol)).Formula = _
                "=INDIRECT(ADDRESS(ROW()," & actCol & ")) & "" "" & " & _
                " INDIRECT(ADDRESS(ROW(), " & empCol & "))"
        End With
    End If
End Sub