Delete rows with vba

mmn1000

Board Regular
Joined
Mar 17, 2020
Messages
77
Office Version
  1. 2019
  2. 2013
Platform
  1. Windows
Greetings and good time dear teachers:

In an Excel file that has entered a series of specifications and also has a row, if we want to enter a specific and fixed cell, when we enter the row number, that desired row will be deleted and also pasted in another sheet, of course with VBA.

What formula should be used?
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
if we want to enter a specific and fixed cell
Which cell?
What is the sheet name of the sheet the row is being copied from?
and also pasted in another sheet
What is the sheet name of the sheet the row is being pasted to?
in which row, the next available row? if the next available row what column is that row based on?

What formula should be used?
VBA is code not a formula ;)
 
Upvote 0
If we have a table.
For example, 100 rows from the range of a3 to m100, how can it be deleted in cell A1 when a row number is written using row coding?

and be pasted in sheet two
 
Upvote 0
Please answer all the questions asked, so far you have only answered possibly 2 of the 5 questions and with one of the answers do you mean the destination sheet is actually named sheet two or is it actually named Sheet2?

how can it be deleted in cell A1
Do you mean A1 one is the cell holding the number to be used?
 
Upvote 0
We have two sheets in the file, sheet1 and sheet2.

In cell A1, enter the number of the row to be deleted.
 

Attachments

  • Untitled.png
    Untitled.png
    70.7 KB · Views: 15
Upvote 0
See where you get with the code below

VBA Code:
Sub MoveRows()
    Dim i As Long
    Application.ScreenUpdating = False

    With Sheets("Sheet1")
        i = .Cells(1, "A").Value
        .Rows(i).Copy Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Offset(1)
        .Rows(i).Delete
    End With

    Application.ScreenUpdating = True
End Sub
 
Upvote 0
@mmn1000
For your future posts, please use the standard forum font, colour etc unless there is a specific need to highlight some particular thing(s). It is covered by the first sentence of #14 of the Forum Rules
 
Upvote 0
See where you get with the code below
VBA Code:
 Sub MoveRows() Dim i As Long Application.ScreenUpdating = False With Sheets("Sheet1") i = .Cells(1, "A").Value .Rows(i).Copy Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Offset(1) .Rows(i).Delete End With Application.ScreenUpdating = True End Sub

Thank you for your beautiful code
There is a small problem here, this code uses the rows of Excel itself, if I want it to use the rows that I entered in the table.
 
Upvote 0
Based on your image. The table name in red needs changing to your actual table name as you haven't told us what it is.

Rich (BB code):
Sub MoveRows()
    Dim i As Long
    Application.ScreenUpdating = False

    With Sheets("Sheet1")
        i = .Cells(1, "A").Value + 3
        Intersect(.Rows(i), .ListObjects("Table1").DataBodyRange).Copy Sheets("Sheet2").Cells(Rows.Count, "A").End(xlUp).Offset(1)
        Intersect(.Rows(i), .ListObjects("Table1").DataBodyRange).Delete
    End With

    Application.ScreenUpdating = True
End Sub
 
Upvote 0
This is what I came up with that also allows for the table location to be changed:

VBA Code:
Sub CopyDeleteTableRow()
'
    Dim MyTable                 As ListObject
    Dim MyTableDataStartRow     As Long, TableRowToCopyDelete   As Long
    Dim wsDestination           As Worksheet, wsSource          As Worksheet
'
    Set wsSource = Sheets("Sheet1")                                                                                 ' <--- Set this to the name of sheet containing the table
    Set wsDestination = Sheets("Sheet2")                                                                            ' <--- Set this to the name of sheet that you will copy row to
    Set MyTable = wsSource.ListObjects(1)                                                                           ' <--- Set this to the proper Table #
'
    MyTableDataStartRow = MyTable.Range.Cells(1, 1).Row                                                             ' Get the Row # of the table header
'
    TableRowToCopyDelete = wsSource.Range("A1").Value                                                               ' Get the table row # to copy/delete
'
    With wsSource.Rows(TableRowToCopyDelete + MyTableDataStartRow)
        .Copy wsDestination.Range("A" & wsDestination.Range("A" & wsDestination.Rows.Count).End(xlUp).Row + 1)      '   Copy the row # to the Destination sheet
        .Delete                                                                                                     '   Delete the row # from the table
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,038
Messages
6,128,447
Members
449,453
Latest member
jayeshw

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top