根据特定条件将行移到另一行的末尾

时间:2014-01-09 20:06:41

标签: excel vba rows

我的工作表有不同的行数。列从A到R,基本上包含名称和地址。在客户端名称中包含共同签名者,其可以从无到有10变化。我需要将Co-signer(1)行移动到客户端行的末尾。如果该客户端包含多个Cosigner,则下一个Co-signer(2)行将被移动到Co-signer(1)信息的末尾。我可以让第一个工作,但无法弄清楚如何通过工作表循环并让所有的共同签名者在正确的客户端行。这就是我到目前为止所拥有的。实施例

CLIENT#   FIRST NAME   LAST NAME      DEBT_SSN        STREET                

00001     MICKEY       MOUSE          000-00-0000     Address Number 1              
          (CS) DONALD  DUCK           000-00-0001     Address Number 2              
00002     MINNIE       MOUSE          000-00-0002     Address Number 3              
          (CS) DAFFEY  DUCK           000-00-0003     Address Number 4              
          (CS) BARNIE  RUBBEL         000-00-0004     Address Number 5      

在此示例中,(CS)Donald Duck的信息将通过AI移至第2行第S列 (CS)Daffey Duck将通过AI进入第4排。然后(CS)Barnie Rubbel将转到第4行AJ到AZ。

Sub MOVECS()

Dim Rng As Range

Set Rng = Range("B2:B6000").Find(What:="*(CS)*", LookAt:=xlWhole, _
                                          LookIn:=xlValues)
Rng.Resize(1, 17).Cut Rows(1).End(xlDown).Offset(0, 18)

End Sub

我尝试添加“Nxt Rng”,但这会占用我的最后一条(CS)记录并将其移至第二行。

1 个答案:

答案 0 :(得分:1)

这是我的解决方案:

    Sub append_cs_to_end_of_rows()
        'once cs row is appended to end of client row, it deletes the source cs row
        r = 2
        num_columns = 17 'this is the number of columns in the cs rows. would need to add one to it to get the number of columns in the client rows.
        Do While Not IsEmpty(Range("b" & r))
            client_r = r
            r = r + 1
            cur_offset = num_columns
            Do While IsEmpty(Range("a" & r)) And Not IsEmpty(Range("b" & r))
                For c = 2 To 1 + num_columns
                    Cells(client_r, c + cur_offset).Value = Cells(r, c).Value
                Next c
                Rows(r).Delete shift:=xlUp
                cur_offset = cur_offset + num_columns
            Loop
        Loop
    End Sub

我避免使用复制/粘贴或剪切,因为这两个都需要范围,并且没有num_to_col函数很难增加列。

请注意,列数最多,因此每个客户端的cs不能太多。如果您的每个客户端保持在900以下,那么您应该没问题(假设您使用的是Office 2010或更高版本)。

祝你好运。