删除行后,使用宏在excel中将行值上移

删除行后,使用宏在excel中将行值上移,excel,vba,Excel,Vba,因此,我使用这个宏按id(序列号)搜索,然后删除特定行的行元素。 删除该行的代码为: Sub DeleteMe() 'declare the variables Dim ID As Range, c As Range, orange As Range Dim wb As Workbook Set wb = ThisWorkbook Set Fws = wb.Sheets("Data") Set Bws = wb.Sheets("Bookings") Dim lastrow As Long 'se

因此,我使用这个宏按id(序列号)搜索,然后删除特定行的行元素。 删除该行的代码为:

Sub DeleteMe()
'declare the variables
Dim ID As Range, c As Range, orange As Range
Dim wb As Workbook
Set wb = ThisWorkbook
Set Fws = wb.Sheets("Data")
Set Bws = wb.Sheets("Bookings")
Dim lastrow As Long
'set the object variable
Set ID = Bws.Range("B3")
'stop screen flicker
Application.ScreenUpdating = False
lastrow = Fws.Range("A" & Rows.Count).End(xlUp).Row
Set orange = Fws.Range("A2:A" & lastrow)
'find the value in the range
For Each c In orange
    If c.Value = ID.Value Then
'delete the row
        c.Cells(1, 2).Clear
        c.Cells(1, 3).Clear
        c.Cells(1, 4).Clear
        c.Cells(1, 5).Clear
        c.Cells(1, 6).Clear
        c.Cells(1, 7).Clear
        c.Cells(1, 8).Clear
        c.Cells(1, 9).Clear
        c.Cells(1, 10).Clear
        c.Cells(1, 11).Clear
        c.Cells(1, 12).Clear
        c.Cells(1, 13).Clear
        c.Clear

    End If

Next c

Sheet2.Select
端接头

我不会删除整行,因为在其他列中存在我不想触及的值

运行代码后,我的数据库如下所示:

我想要的是将所有剩余的行向上移动以填充空行。我还希望序列号相应地改变。在这种情况下,7变成6,8变成7


提前谢谢

试试这个。。向上循环有助于确保覆盖所有行。因为一旦我们删除一行,如果我们向下循环,那么下一行号就会变成i-1

Sub DeleteMe()
'declare the variables
Dim Fws As Worksheet, Bws As Worksheet
Dim ID As Range
Dim wb As Workbook
Set wb = ThisWorkbook
Set Fws = wb.Sheets("Data")
Set Bws = wb.Sheets("Bookings")
Dim lastrow As Long
'set the object variable
Set ID = Bws.Range("B3")
'stop screen flicker
Application.ScreenUpdating = False
lastrow = Fws.Range("A" & Rows.Count).End(xlUp).Row
'find the value in the range
Fws.Select
For i = lastrow To 2 Step -1
    If Fws.Cells(i, 1) = ID.Value Then
'delete the row
        Fws.Range(Cells(i, 1), Cells(i, 13)).Delete Shift:=xlUp
    End If
Next

With Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)
    .Formula = "=ROW() - 1"
    .Value = .Value
End With

End Sub

试试这个。。向上循环有助于确保覆盖所有行。因为一旦我们删除一行,如果我们向下循环,那么下一行号就会变成i-1

Sub DeleteMe()
'declare the variables
Dim Fws As Worksheet, Bws As Worksheet
Dim ID As Range
Dim wb As Workbook
Set wb = ThisWorkbook
Set Fws = wb.Sheets("Data")
Set Bws = wb.Sheets("Bookings")
Dim lastrow As Long
'set the object variable
Set ID = Bws.Range("B3")
'stop screen flicker
Application.ScreenUpdating = False
lastrow = Fws.Range("A" & Rows.Count).End(xlUp).Row
'find the value in the range
Fws.Select
For i = lastrow To 2 Step -1
    If Fws.Cells(i, 1) = ID.Value Then
'delete the row
        Fws.Range(Cells(i, 1), Cells(i, 13)).Delete Shift:=xlUp
    End If
Next

With Range("A2:A" & Range("A" & Rows.Count).End(xlUp).Row)
    .Formula = "=ROW() - 1"
    .Value = .Value
End With

End Sub

请尝试此代码。它应该做你想做的

Sub DeleteMe()
    ' 018

    'declare the variables
    Dim Wb As Workbook
    Dim Fws As Worksheet, Bws As Worksheet
    Dim DelRng As Range
    Dim Id As String
    Dim Rl As Long                              ' last row
    Dim R As Long                               ' row counter

    'set the object variables
    Set Wb = ThisWorkbook
    Set Fws = Wb.Sheets("Data")
    Set Bws = Wb.Sheets("Bookings")

    'stop screen flicker
    Application.ScreenUpdating = False

    Set Id = Bws.Range("B3")
    With Fws
        ' loop through all cells from bottom to top
        ' because row numbers will change as you delete cells
        For R = .Range("A" & .Rows.Count).End(xlUp).Row To 2 Step -1
            'find the value in each row
            If .Cells(R, "A").Value = Id.Value Then
                Set DelRng = .Range(.Cells(R, 1), .Cells(R, 13))
                DelRng.Delete Shift:=xlUp
            End If
        Next R

        Rl = .Cells(.Rows.Count, "A").End(xlUp).Row
        For R = 2 To Rl
            .Cells(R, "A").Value = R - 1
        Next R
    End With
End Sub

请尝试此代码。它应该做你想做的

Sub DeleteMe()
    ' 018

    'declare the variables
    Dim Wb As Workbook
    Dim Fws As Worksheet, Bws As Worksheet
    Dim DelRng As Range
    Dim Id As String
    Dim Rl As Long                              ' last row
    Dim R As Long                               ' row counter

    'set the object variables
    Set Wb = ThisWorkbook
    Set Fws = Wb.Sheets("Data")
    Set Bws = Wb.Sheets("Bookings")

    'stop screen flicker
    Application.ScreenUpdating = False

    Set Id = Bws.Range("B3")
    With Fws
        ' loop through all cells from bottom to top
        ' because row numbers will change as you delete cells
        For R = .Range("A" & .Rows.Count).End(xlUp).Row To 2 Step -1
            'find the value in each row
            If .Cells(R, "A").Value = Id.Value Then
                Set DelRng = .Range(.Cells(R, 1), .Cells(R, 13))
                DelRng.Delete Shift:=xlUp
            End If
        Next R

        Rl = .Cells(.Rows.Count, "A").End(xlUp).Row
        For R = 2 To Rl
            .Cells(R, "A").Value = R - 1
        Next R
    End With
End Sub

如果id保留在第一行,您可以使用c.entirerow.delete?@nareshbople我不能。我不想再碰其他列中的值。可能是这样的
如果c.Value=ID.Value然后是range(c.offset(0,1),c.offset(0,12))。Delete Shift:=xlUp
这将删除从B列到M列的行,然后向上移动下面的行。假设A列中的数字不是一个公式,则该数字不会发生任何变化。然后,您可以添加
范围(“A1000000”).end(xlup).clearcontents
以删除最后一个数字。如果id保留在第一行,您可以使用c.entirerow.delete?@NareshBhople我不能。我不想再碰其他列中的值。可能是这样的
如果c.Value=ID.Value然后是range(c.offset(0,1),c.offset(0,12))。Delete Shift:=xlUp
这将删除从B列到M列的行,然后向上移动下面的行。假设A列中的数字不是一个公式,则该数字不会发生任何变化。然后,也许你可以添加
范围(“A1000000”).end(xlup).clearcontents
,删除最后一个数字。也许你也可以在答案中加入解释,以便OP和其他未来读者了解你更正了什么以及为什么。这里不赞成只使用代码来回答,告诉人们“试试这个”。@braX。。再次感谢。这也让我意识到了我的错误:)@nareshbople除了序列号部分,它工作得很好。非常感谢你,它给了我一个出路:)也许你也可以在你的答案中加入一个解释,这样OP和其他未来的读者会理解你纠正了什么以及为什么。这里不赞成只使用代码来回答,告诉人们“试试这个”。@braX。。再次感谢。这也让我意识到了我的错误:)@nareshbople除了序列号部分,它工作得很好。非常感谢,它给了我一条出路:)