根据另一列

时间:2015-05-03 00:52:06

标签: excel vba excel-vba

我有以下列和值:

Begin Time  Other values    First Name  Last Name   other info
5041*       value1                                  info1
5041*       value2          firstname1  lastname1   info2
13089       value3                                  info3
16130       value4                                  info4
26391*      value5                                  info5
26391*      value6                                  info6
26391*      value7          firstname2  lastname2   info7
27878       value8                                  info8
27878       value9                                  info9
28234       value10                                 info10
63189*      value11                                 info11
63189*      value12                                 info12
63189*      value13                                 info13
63189*      value14         firstname3  lastname3   info14
64335       value15                                 info15
65423       value16                                 info16
72089*      value17                                 info17
72089*      value18         firstname4  lastname4   info18
73495       value19                                 info19
73495       value20                                 info20
74330       value21                                 info21
74877       value22                                 info22
76710       value23                                 info23
82599*      value24                                 info24
82599*      value25          firstname5 lastname5   info25
98712*      value26                                 info26
98712*      value27          firstname6 lastname6   info27
98725       value28                                 info28
100605      value29                                 info29
100605      value30                                 info30
100954      value31                                 info31


我希望如此:

Begin Time  Other values    First Name  Last Name   other info
5041        value1          firstname1  lastname1   info1
5041        value2          firstname1  lastname1   info2
13089       value3          firstname1  lastname1   info3
16130       value4          firstname1  lastname1   info4
26391       value5          firstname2  lastname2   info5
26391       value6          firstname2  lastname2   info6
26391       value7          firstname2  lastname2   info7
27878       value8          firstname2  lastname2   info8
27878       value9          firstname2  lastname2   info9
28234       value10         firstname2  lastname2   info10
63189       value11         firstname3  lastname3   info11
63189       value12         firstname3  lastname3   info12
63189       value13         firstname3  lastname3   info13
63189       value14         firstname3  lastname3   info14
64335       value15         firstname3  lastname3   info15
65423       value16         firstname3  lastname3   info16
72089       value17         firstname4  lastname4   info17
72089       value18         firstname4  lastname4   info18
73495       value19         firstname4  lastname4   info19
73495       value20         firstname4  lastname4   info20
74330       value21         firstname4  lastname4   info21
74877       value22         firstname4  lastname4   info22
76710       value23         firstname4  lastname4   info23
82599       value24         firstname5  lastname5   info24
82599       value25         firstname5  lastname5   info25
98712       value26         firstname6  lastname6   info26
98712       value27         firstname6  lastname6   info27
98725       value28         firstname6  lastname6   info28
100605      value29         firstname6  lastname6   info29
100605      value30         firstname6  lastname6   info30
100954      value31         firstname6  lastname6   info31


感谢@Jeeped's answer

,我正在使用以下代码
Sub FillColBlanksSpecial2()

    Dim wks As Worksheet
    Dim rng As Range
    Dim rng2 As Range
    Dim blnk As Range
    Dim LastRow As Long
    Dim col As Long
    Dim lRows As Long
    Dim lLimit As Long

    Dim lCount As Long
    On Error Resume Next

    lRows = 2
    lLimit = 1000

    Set wks = ActiveSheet
    For Each wks In Worksheets
        If Right(wks.Name, 2) = "-A" Or Right(wks.Name, 2) = "-B" Then
            With wks
                With .Cells(1, 1).CurrentRegion
                    With .Columns("C:D")
                        If CBool(Application.CountBlank(.Cells)) Then
                            For Each blnk In .SpecialCells(xlCellTypeBlanks)
                                blnk.FormulaR1C1 = "=if(countifs(r1c1:r[-1]c1, rc1, r1c:r[-1]c, ""<>""), index(r1c:r[-1]c, match(rc1, r1c1:r[-1]c1, 0)), if(countifs(r[1]c1:r9999c1, rc1, r[1]c:r9999c, ""<>""), index(r[1]c:r9999c, match(rc1, r[1]c1:r9999c1, 0)), r[-1]c))"
                                blnk.Value = blnk.Value
                            Next blnk
                        End If
                    End With
                End With
            End With
        End If
    Next wks
End Sub


使用xlR1C1样式的公式及其xlA1等效:

=IF(COUNTIFS(R1C1:R[-1]C1, RC1, R1C:R[-1]C, "<>"), INDEX(R1C:R[-1]C, MATCH(RC1, R1C1:R[-1]C1, 0)), IF(COUNTIFS(R[1]C1:R9999C1, RC1, R[1]C:R9999C, "<>"), INDEX(R[1]C:R9999C, MATCH(RC1, R[1]C1:R9999C1, 0)), R[-1]C))
=IF(COUNTIFS($A$1:$A1, $A2, C$1:C1, "<>"), INDEX(C$1:C1, MATCH($A2, $A$1:$A1, 0)), IF(COUNTIFS($A3:$A$9999, $A2, C3:C$9999, "<>"), INDEX(C3:C$9999, MATCH($A2, $A3:$A$9999, 0)), C1))



目标是根据条件使用现有值填充 C:D
1。取 C:D 中的值并向下填充,直到相同列中的下一个非空单元格。(这些非空单元格包含唯一值,firstname1,firstname2等)
2。如果上面的空行在 A列中与行共享相同的值,则取 C:D 中的值并填充向上低于我们正在应对的价值。

@ Jeeped的回答是正确的:
它从 C:D 列获取值并填充空行,直到包含新值的下一行;如果该空行在 A 列中与下面包含要复制的值的行共享相同的值,它也会填充一个空行。

但只有填满一行。我的示例数据(下面的链接)显示可能有 多个空行 < / strong>在包含 C:D 列中的值的行上方,需要填写。

如何修改此vba代码以适应这种情况?

Here is the excel worksheet with a longer example data. The workbook contains two sheets: what I have & what I expect.

PS 除了第一列中的代码之外的星号标记仅用于视觉可访问性,以识别在 A 列中具有相同值的空行下面的行包含列 C:D 中的值;否则它们不会出现在原始数据表中。

通过空行,我的意思是那些在 C:D 列中没有值的行。

0 个答案:

没有答案