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

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

Related

Delete Table Rows

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

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

Error - Duplicates in excel

I am trying to remove duplicates in excel and have been successful in doing that. I have certain gaps in my spreadsheet after running the code is where I need help. Below is the entire explanation:
1) I copy records from two different files in to a separate file and have 17 columns (A to R).
2) I look for duplicates and remove.
3) 3 Blank rows appear between the last record and second last record after removal of duplicates.(Picture attached)
With y.Sheets("RL Holdings").Activate
Set rng = Range("A2", Range("R2").End(xlDown))
rng.RemoveDuplicates Columns:=Array(1), Header:=xlYes
End With
I only see up to column J in your example. I am guessing cell R18 is empty, so that .End will not select the last row, but Q18 has something in it.

Excel VBA - selecting the range from first row to the last row

I have a problem with VBA code. I have a sheet with a lot of data around the cells I want. I need to select data in column F, but the position of the first and last cells between the range is to be selected is changing up and down. I created the non empty cells in row X where there is no data do the LastRow function has any refernece but now I dont know how to select the first row
thx for help
If F1 is empty, this selects the first cell with data in the F column
Worksheets("Sheet1").Range("F1").End(xlDown).Select
If F1 may be non-empty you can add a check to see whether it contains text.

How do I insert the formula into the cell when the formula keeps changing with an increase in row?

I have entered these formula in the second row of the Pth column:
=(COUNTIF(A$1:A1,A2)=0)+(COUNTIF(B$1:B1,B2)=0)+(COUNTIF(F$1:F1,F2)=0)
When I drag it to the third row of the Pth column, it gets like this:
=(COUNTIF(A$1:A2,A3)=0)+(COUNTIF(B$1:B2,B3)=0)+(COUNTIF(F$1:F2,F3)=0)
This is what I do manually. How do I make it using VBA? I have tried in the way below.
cells(Count,"M").formula= "=(COUNTIF(A$1:A1,A2)=0)+(COUNTIF(B$1:B1,B2)=0)+(COUNTIF(F$1:F1,F2)=0)"
But it's not working. It's not changing from
"=(COUNTIF(A$1:A1,A2)=0)+(COUNTIF(B$1:B1,B2)=0)+(COUNTIF(F$1:F1,F2)=0)"
to
"=(COUNTIF(A$1:A2,A3)=0)+(COUNTIF(B$1:B2,B3)=0)+(COUNTIF(F$1:F2,F3)=0)"
How do I insert the formula into the cell when the formula keeps changing with an increase in row?
You can do this in one line:
range("P2").Copy Destination:=range("P3:P10")
No need for variables, loops, anything!
As suggested by Joubarc
Cells(2, "P").Copy
For Row = 3 To 10
Cells(Row, "P").Select
ActiveSheet.Paste
Next Row