Excel 通过使用Cut+;插入法

Excel 通过使用Cut+;插入法,excel,vba,Excel,Vba,如何剪切和插入匹配行(根据共享的第一个元素标准) 这个问题是通过匹配第一个元素来对齐行问题的扩展。当意识到VBA允许程序员剪切+插入时,很明显我对这个问题的理解是错误的。然而,为了实现这个想法,我真的把代码弄得乱七八糟。必须有一种方法来简化rmatch函数下面和嵌套的delete row循环上面的行,以实现这个目标-我只是不知道如何实现 With wsR For r = startRow To wsR.Cells(.Rows.Count, t1s).End(xlUp).R

如何剪切和插入匹配行(根据共享的第一个元素标准)

这个问题是通过匹配第一个元素来对齐行问题的扩展。当意识到VBA允许程序员剪切+插入时,很明显我对这个问题的理解是错误的。然而,为了实现这个想法,我真的把代码弄得乱七八糟。必须有一种方法来简化
rmatch
函数下面和嵌套的delete row循环上面的行,以实现这个目标-我只是不知道如何实现

    With wsR
        For r = startRow To wsR.Cells(.Rows.Count, t1s).End(xlUp).Row
            If Application.WorksheetFunction.CountIf(.Columns(t2s), wsR.Cells(r, t1s).Value) > 0 Then
    
                rMatch = Application.WorksheetFunction.Match(.Cells(r, t1s).Value, .Columns(t2s), 0)
            
                .Range(wsR.Cells(rMatch + 1, t2s), wsR.Cells(rMatch + 1, t2s + t2l)).Insert shift:=xlDown
                .Range(wsR.Cells(rMatch + 1, t2s), wsR.Cells(rMatch + 1, t2s + t2l)).Value2 = "*"
            
                .Range(wsR.Cells(rMatch, t1s), wsR.Cells(rMatch, t1s + t1l)).Insert shift:=xlDown
                '.Range(wsR.Cells(rMatch + 1, t1s), wsR.Cells(rMatch + 1, t1s + t1l)).Value2 = "*"
                .Range(wsR.Cells(rMatch, t1s), wsR.Cells(rMatch, t1s + t1l)).Value2 = .Range(wsR.Cells(r + 1, t1s), wsR.Cells(r + 1, t1s + t1l)).Value2
                
                r = r + 1
                                    
                'If .Cells(r, t1s).Value <> .Cells(r, t2s) Then
                    '.Range(wsR.Cells(rMatch + 1, t2s), wsR.Cells(rMatch + 1, t2s + t2l)).Insert shift:=xlDown
                    '.Range(wsR.Cells(rMatch + 1, t2s), wsR.Cells(rMatch + 1, t2s + t2l)).Value2 = "*"
                'End If
                                    
                If wsR.Cells(r, t1s).Value = "*" & wsR.Cells(r, t2s).Value = "*" Then
                    wsR.Cells(r, "A").EntireRow.Delete
                End If
                                    
            End If
        Next r
    End With
使用wsR的

对于r=startRow到wsR.Cells(.Rows.Count,t1s).End(xlUp).Row
如果Application.WorksheetFunction.CountIf(.Columns(t2s)、wsR.Cells(r、t1s).Value)大于0,则
rMatch=Application.WorksheetFunction.Match(.Cells(r,t1s).Value、.Columns(t2s),0)
.范围(wsR.单元格(RTACH+1,t2s),wsR.单元格(RTACH+1,t2s+t2l))。插入移位:=xlDown
.范围(wsR.单元格(rMatch+1,t2s),wsR.单元格(rMatch+1,t2s+t2l))。值2=“*”
.Range(wsR.Cells(rMatch,t1s),wsR.Cells(rMatch,t1s+t1l))。插入移位:=xlDown
“.Range(wsR.Cells(rMatch+1,t1s),wsR.Cells(rMatch+1,t1s+t1l))。Value2=“*”
.Range(wsR.Cells(rMatch,t1s),wsR.Cells(rMatch,t1s+t1l)).Value2=.Range(wsR.Cells(r+1,t1s),wsR.Cells(r+1,t1s+t1l)).Value2
r=r+1
'如果.Cells(r,t1s).Value.Cells(r,t2s)则
“.Range(wsR.Cells(rMatch+1,t2s),wsR.Cells(rMatch+1,t2s+t2l))。插入移位:=xlDown
'.Range(wsR.Cells(rMatch+1,t2s),wsR.Cells(rMatch+1,t2s+t2l))。Value2=“*”
"完"
如果wsR.Cells(r,t1s).Value=“*”&wsR.Cells(r,t2s).Value=“*”则
wsR.Cells(r,“A”).EntireRow.Delete
如果结束
如果结束
下一个r
以

如有任何建议,将不胜感激。

更新2

    With wsR
    For r = startRow To wsR.Cells(.Rows.Count, t1s).End(xlUp).Row
        If Application.WorksheetFunction.CountIf(.Columns(t2s), wsR.Cells(r, t1s).Value) > 0 Then

            rMatch = Application.WorksheetFunction.Match(.Cells(r, t1s).Value, .Columns(t2s), 0)
            
            .Range(.Cells(r - 1, t2s), .Cells(r - 1, t2s + t2l)).Insert shift:=xlDown
            .Range(.Cells(rMatch + 1, t2s), .Cells(rMatch + 1, t2s + t2l)).Cut
            .Select
            .Range(.Cells(r, t2s), .Cells(r, t2s + t2l)).Select
            .Paste
            .Range(.Cells(rMatch + 1, t2s), .Cells(rMatch + 1, t2s + t2l)).Delete

        End If
    Next r
End With
因此,我在这里所做的是:

  • 将空白单元格插入表2中与表1中所需值相同的行中
  • 切割表2中的单元格
  • Paste把它们送到空白的细胞。
  • 删除现在为空的旧单元格

  • 不完全是。该算法需要找到匹配项,剪切然后插入其中一对以与其匹配项对齐。希望这是有意义的。你能上传一张你想要达到的目标的照片吗?我认为你的任务过于复杂了,但我不能完全理解你到底想做什么。是的,努力表达清楚。我没有足够的声誉来重现“通过匹配第一个元素对齐行”(?!)的图像。简单地说:想象两个表A(左)和B(右)并排。B是固定的,A不是固定的,A和B的对应行匹配,但只有当它们行的第一个元素相同时才匹配。为此,A剪切并插入其行以与其匹配的B行对齐。谢谢,这使我对您的问题有了更好的了解。请看我的最新答案:-)这看起来更好-干净。谢谢你,@Matthias Fuglsang Damgaard。点击438错误“对象不支持此属性或方法”,似乎连接到.paste行…读取长变量以确定原因。