Excel formula for deleting rows - excel-formula

I am looking for an Excel formula to automatically delete all even or odds rows.
Example: I want to delete row A2, A4, A6, ... , and A500, so doing it manually is a pain!

Try to add the column with the formula:
X2 = isEven(row(X2); Any row is fine
Then:
Select the range.
Sort by the column X.
Delete the true or false accord your need.

Try in Row1:
=ISODD(ROW())
copied down to suit. Filter that column and uncheck the rows you wish to retain (eg uncheck FALSE to keep the even numbered rows). Delete all visible or, depending upon your choice, all visible other than Row1.

Deleting rows may not be possible using excel formula, use vba for automating this process; use the following code if you want to delete odd rows then change startatrow to 1 and if even number then leave it at 2. I am assuming you have a column with complete dataset, lets say column A (you can change it accordingly)
Sub deleteAlternateRow()
Dim startAtRow, rowCounter, lastrow As Long
lastrow = Cells(Rows.Count, 1).End(xlUp).Row
startAtRow = 2
For rowCounter = startAtRow To lastrow step 2
Rows(rowCounter).Select
Selection.Delete Shift:=xlUp
Next
End Sub

Related

Delete row (column A to K) if cell blank in column D (or filter and delete all (Blanks)

Looking for some help here since most answers delete the entire row. Thanks in advance!
This is what my table looks like, I'm looking to delete a row (but only from column A to K, not entire row), if a cell is blank in column D.
Data is dynamic, will change each time, but it should always have a blank in cell D. If not, looking for the code to just continue on.
So highlighted D3 is blank, I want it to delete A3 to K3.
.
.
You could filter cell D2 for (Blanks) to have them all -- mind you there most likely will be a blank cell in column B, but I essentially want to delete all this data filtered.
Is is ColumnK or ColumnF. Something like this, maybe. Just change to suit your needs.
Option Explicit
Sub BlankRowDeletion()
'Declaring variables
Dim LastRow As Long
Dim Rng As Range
'Getting row number of last cell
LastRow = Range("A3").SpecialCells(xlCellTypeLastCell).Row
'Selecting all data
Set Rng = Range("A3:K" & LastRow)
'Selecting Blank cells
Rng.SpecialCells(xlCellTypeBlanks).Select
'Deleting complete row
Selection.EntireRow.Delete
Range("A3").Select
End Sub

Fill formula until the last row automatically

IF(ISBLANK(B5),"",IF(ISBLANK(O5)=TRUE,"Missing PSD",TODAY()-O5))
This is my formula that calculates the difference between the date in column O and current date. My first filled row with values is 5. The row in which the formula calculation is being done is AC.
I want to automatically calculate this until the last filled row and the row values should also increment like it does while dragging down.
I am not good with VBA so any help would be highly appreciated.
Try the next code, please:
Sub testFilFormulaDown()
Dim sh As Worksheet, lastRow As Long
Set sh = ActiveSheet 'use here the necessary sheet
lastRow = sh.Range("O" & rows.count).End(xlUp).row 'chosen O:O column, being involved in the formula...
sh.Range("AC5:AC" & lastRow).Formula = "=IF(ISBLANK(B5),"""",IF(ISBLANK(O5)=TRUE,""Missing PSD"",TODAY()-O5))"
End Sub
In order to properly calculate the last row, you must choose a fully filled column (A:A, C:C etc.). I used one involved in the formula, but since there is a check for blank cells, column O:O could not be the most appropriate one...

Excel How to automatically choose the whole column data?

How to automatically choose the whole column data? For example theSUM,
When data is A1:A20, then the result is =SUM(A1:A20), when we change data into A1:A30, then the result automatically become =SUM(A1:A30), i.e choose the all effective data this column or A1:End.
Simply try using: =SUM(A:A)
It should ideally work.
Using VBA, you can use the method .End(xlDown) with .Select to select the last non-empty row in a column.
Range("A1").End(xlDown).Select
Or instead of .Select, you can use .Row to get the last non-empty row number.
Example code:
Sub SumData()
Dim LastRow As Long
LastRow = Range("A1").End(xlDown).Row
Cells(LastRow + 2, "A").Formula = "=SUM(A1:A" & LastRow & ")"
End Sub
This will work to find the last row used in the range:
MAX(IF(A:A<>"",ROW(A:A))
To incorporate that in the SUM formula:
=SUM(INDIRECT("A1:A"&MAX(IF(A:A<>"",ROW(A:A)))
Please note that this formula will need to be entered as an array (When in the formula bar hit Ctrl+Shift+Enter) and will slow down your spreadsheet if you have a lot of data in there

Insert blank rows in excel if row above is not empty

If A1 is not empty (contains actual data), is there a way to if-then so that I can insert a blank row under A1. This should apply to any cell/row as I have a spreadsheet with about 65000 rows and they want to have blanks to separate the rows that contain different identifiers. I looked on here, right before I signed up, and I saw some things about empty cells or making cells empty depending on other cells, but that doesn't seem to be what I'm looking for. Google wasn't too much help either.
thanks.
Is this what you want?
Sub helping()
Dim count As Long
For count = ActiveSheet.UsedRange.Rows.count To 1 Step -1
If Information.IsEmpty(Cells(count, 1)) = False Then Rows(count + 1).Insert
Next count
End Sub

Excel 2007 - Formula changes to #REF

So I've got this Workbook which contains a lot of data. And I've got this one sheet which basically copies the data based on certain conditions.
Each cell in each row looks like this (the last specified cell is the one where the formula is in):
=IF(Numbers1!E2<>0;Numbers1!A2;"")
=IF(Numbers1!E3<>0;Numbers1!A3;"")
=IF(Numbers1!E4<>0;Numbers1!A4;"")
=IF(Numbers1!E2<>0;Numbers1!B2;"")
=IF(Numbers1!E3<>0;Numbers1!B3;"")
=IF(Numbers1!E4<>0;Numbers1!B4;"")
So the formula in cell A2 is the first one, formula in A3 is the second line etc.
I want to copy the value from the same column and row from the sheet Numbers1, IF the value in the same row of column E is not 0. This seems to be working just fine.
But, when I update the data in Numbers1 sheet, the formulas are all of a sudden invalid and the formula now looks like this:
=IF(Numbers1!#REF!<>0;Numbers1!#REF!;"")
Each formula in each cells look identical to the formula above. And I can't have that, why can't Excel just keep the formula as it is without "helping" me?
Since you may be better off using a macro to rewrite your formulas, here are the basics:
Sub RewriteFormulas()
Dim row, col As Integer
row = 1 'row you want your target formulas to be on
For row = 1 To 60
For col = 1 To 13
ActiveSheet.Cells(row, col).Formula = "=IF(Numbers1!" & Cells(row,col).Address & "<>0,Numbers1!" & Cells(row+2,col).Adddress & ","""")"
Next row
Next col
End Sub
You can play around with using different sheets (or different workbooks) instead of just ActiveSheet so you can have 1 workbook that stores the macro and alters data in whatever workbooks provide your updated datasets.
Hope that helps...

Resources