Delete Table Rows - vba

I have a Table in my Excel. The table is called Table1.
I want to delete the entire row except 1st row of the table which it can be done manually like select the row then delete table row, but I couldn't imagine if the row is reaching more than a million records. I tried the following code:
Sheet3.Range("A20","E500000").Delete 2
But the code above is error. The error said:
This won't work because it would move cells in a table on your worksheet.
Does anyone have a same problem with me? Suggestion please.

Try below if you only have one Table object in your sheet.
Dim lo As ListObject
With Sheet3 '/* sheet code name */
Set lo = .ListObjects(1)
On Error Resume Next
lo.DataBodyRange.Delete xlUp
On Error GoTo 0
End With
If not, you can explicitly identify your table like:
Set lo = .ListObjects("Table1")

I suppose you want to delete all the rows except the header, if so, you can use DataBodyRange like this:
ActiveSheet.ListObjects("Table1").DataBodyRange.Delete

so the second row of the table start from cell A20?
if so, you just want to delete row till the last cell, then the code:
Sub dr()
Dim a As Integer
a = ActiveSheet.UsedRange.Rows.Count
Range(Range("A20"), Range("A" & a)).EntireRow.Delete
End Sub

Select the 2nd row. Press Shift+ctrl+down. This will select all the records except the header.
Right click and select delete.

Since #Haminteu want to keep the header and first row of data, I provide the solution as below.
With Range("Table1")
.Offset(1).Resize(.Rows.Count - 1).Delete 2
End With

Related

VBA Insert rows at certain point in data

I have this pivot table I am inserting above my data. The data can be different lengths and so the pivot table and be different lengths. So what I do is put the data a little ways down on the sheet starting at say maybe row 30. Then I insert my pivot table up top. What I do next is I then have some code to delete all of the blank rows in between my data and the pivot table.
sub foo()
dim r As Range, rows As Long, i As Long
Set r = ActiveSheet.Range("A1:Z50")
rows = r.rows.Count
For i = rows To 1 Step (-1)
If WorksheetFunction.CountA(r.rows(i)) = 0 Then r.rows(i).Delete
Next
End Sub
This code will delete all of the blank rows and seems to work correctly. Since that puts everything together, what I want to do then is just put 2 blank rows in between my pivot table and my data but I don't know how to figure out where my pivot table ends or where my data begins.
So I want it to look like that ^^^^ , but right now Row 7 and 8 are not there and my data is pushed up onto my pivot table. Any help is appreciated please!
I was thinking someone like using Rows.Count + 1 or something to find where the pivot table ends. But I don't know how it would determine that since there is data in the row right after. SO maybe I need to find when there is data starting in say Column A then Insert the rows right before that. Thank you in advance for the help!
Try something along those lines:
Public Sub Answer()
Dim dataTopRowIndex As Integer
Dim ptBottomRowIndex As Integer
With ActiveSheet 'Note: use your worksheet's CodeName here instead, e.g. With Sheet1 (Active[anything], Activate, Select, Selection: evil).
dataTopRowIndex = .Cells(1, 1).End(xlDown).Row
With .PivotTables("PivotTable1").TableRange1 'Replace PivotTable1 by your PT's name.
ptBottomRowIndex = .Row + .Rows.Count - 1
End With
If (dataTopRowIndex - ptBottomRowIndex - 1) > 2 Then
.Range(.Rows(ptBottomRowIndex + 1), .Rows(dataTopRowIndex - 3)).EntireRow.Delete
End If
End With
End Sub

Deleting Columns based on header value, withut skipping columns. Using VBA

I am trying to delete columns in the data I have based on the header values, using VBA. I am definitely a novice at this, so would appreciate any help. At the moment I have managed to find some code that can do this, except every time I run the macro it deletes some of the columns, but appears to skip some of the columns. I think because when a column is deleted the column then moves to a new location, ie F5 gets deleted so G5 moves to F5 and then manages to escape the query. This is the code
Sub DeleteSpecifcColumn()
Set MR = Range("A1:D1")
For Each cell In MR
If cell.Value = "old" Then cell.EntireColumn.Delete
Next
End Sub
This is a classical problem ,If you want to delete rows or columns always begin deleting from the end .
Sub DeleteSpecifcColumn()
For i = 4 To 1 Step -1
If Cells(1, i) = "old" Then
Cells(1, i).EntireColumn.Delete
End If
Next i
End Sub

excel: correct column position

Actually, it is a simple question, although I don't know if it's possibl to do what I wanted to.
I'm just copying a column from another using this sub:
Sub copy_column()
Sheets("FROM").Columns("A").Copy Destination:=Sheets("TO").Columns("A")
End Sub
In fact, in my sheet "FROM" my first row with data is the row 3 and then when I copy to "TO" sheet it's starting from row 3 too. My idea was to place it at the row 1 at "TO" sheet.
Is there any way to write something like Columns("A"-2) to put it in the right place?
An alternative which avoids deleting rows (if you have data you want to keep in other columns for example) is to take an intersection of a range overlaid with itself, but offset...
The following code assumes you have some data in row 1 in the sheet so UsedRange would start from the correct row, but you can change the offset to accommodate missing data in the rows above
Dim CopyRange As Range
With Sheets("From")
Set CopyRange = Application.Intersect(.UsedRange, .Columns("A"))
Set CopyRange = Application.Intersect(CopyRange, CopyRange.Offset(2, 0))
CopyRange.Copy Destination:=Sheets("TO").Cells(1, 1)
End With

VBA Excel Delete Table rows and shift up, not entire row

I have a situation just like this image here:
How do I delete only the cells denoted as red and shift the below cells up?
There is a table below and that should not get affected. Can some one help?
Thanks
Jeevan
Delete the cells in the sheet and the table will be adjusted:
Range("B12:C12").Delete Shift:=xlUp
The answer can be found by recording a macro to delete a table row.
This example is from a for loop but can be modified to work however.
Select a cell in the table row you want to delete
Remember the table row is not the same as the worksheet row. in this case i have a blank row above my table and the header row so -2
Cells(i, 4).Select
Selection.ListObject.ListRows(i - 2).Delete
You could use
ListObject("TableName").ListRows(RowIndex).Delete
And add an empty row to the bottom.
ListObject("TableName").ListRows.Add

Removing a row in a table if it doesn't contain keyword

Right now I have a really long table in a Word doc which I populated from an Excel worksheet. It has 6 columns and I'm trying to code something in Word VBA that will go through all the rows in the table and delete the entire row if the cell in the first column DOES NOT start with an equal sign ("=").
For example, I'm only trying to keep the rows that has texts like,
"=1+S -03F7", "=1+M -06M1", etc. etc.
How would I code this? I can't give the code anything specific to look for since the parts after the equal sign will be different for every row.
So this wouldn't work, right?:
If Not ActiveDocument.Tables(83).Columns(1).Range.Text = "=" Then
EntireRow.Select
Selection.Delete
I guess I should reference to cells in column 1, not the column itself... Also, it doesn't work because it's only looking for things with just the equal sign... And I don't know how I can get it to select the row if it find the cell without the equal sign. I don't know how to match by case in the cell of the first column.
You can loop through the rows in the table using the Rows property. You can then find the first cell in that Row using the Cells property. You can then check just the first character of the Range:
Sub DeleteUnwantedRows()
Dim t As Table
Dim r As Row
Set t = ActiveDocument.Tables(1)
For Each r In t.Rows
If r.Cells(1).Range.Characters(1) <> "=" Then r.Delete
Next r
End Sub