Insert blank rows in excel if row above is not empty - excel

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

Related

Excel remove duplicates based on 2 columns case-sensitive

I need to remove duplicates from an Excel worksheet based on the values in 2 columns while taking case into account.
In the example below, Rows 1 and 2 are duplicates (Row 2 should be removed). Row 3, 4, and 5 are unique.
Row
Column A
Column B
1
Abc
Def
2
Abc
Def
3
ABC
DEF
4
ABC
DeF
5
Abc
DeF
I've done this with other datasets using Data > Remove duplicates, but since it is case-insensitive, it won't work for this.
I also found this question, which is very similar, but only identifies duplicates based on 1 column.
(How to remove duplicates that are case SENSITIVE in Excel (for 100k records or more)?)
Try this code:
Sub SubRemoveDuplicates()
'Declarations.
Dim RngData As Range
Dim RngDataToBeCompared
Dim RngCell As Range
Dim RngRow As Range
'Settings.
Set RngData = Range("A1:C6")
Set RngDataToBeCompared = Range("B2:C6")
'Covering each row of the data to be compared.
For Each RngRow In RngDataToBeCompared.Rows
CP_Rerun_For:
'Covering each cell of the given row.
For Each RngCell In RngRow.Cells
'Checking if any cell is different from the one under it.
If RngCell.Value <> RngCell.Offset(1, 0).Value Then
'If said cell has been found, skip to the next row.
GoTo CP_Next_Row
End If
Next
'Checking if the range to be targeted is within RngData.
If Not Intersect(RngRow.Offset(1, 0).EntireRow, RngData) Is Nothing Then
'Deleting the row of duplicates.
Intersect(RngRow.Offset(1, 0).EntireRow, RngData).Delete (xlShiftUp)
'Rerunning this cycle for the given row in order to catch duplicates that comes in more than 2.
GoTo CP_Rerun_For
End If
CP_Next_Row:
Next
End Sub
Note: if you are going to cover an entire column with presumably many empty cells, the macro will cover (and eventually delete) all those empty cells too. The macro can be modified so it will stop when it encounters and empty row, or to dynamically determinate the appropriate range to be covered. Otherwise it might take more time than necessary.
I don't like using macros until it's last hope.
For your situation, I would suggest adding new columns, and with function
=lower(Column A), etc. get values of column A in lower case. Then I would add one more new column and do the same for Column B.
And after that, I would use Data/Remove Duplicates (converting range to Table format first). And then I would delete unnecessary columns which were added for converting everything to lowercase.
Use this frmula to manually delete. It combines two columns on one row and compares them with the column above.
=B2&C2=B1&C1
You can then edit or filter on Col D and delete.

How to conditionally format a row in Excel VBA based on if first cell is empty?

What I'm trying to do is very simple, I just don't have much VBA / Excel experience to know how to do it.
I am working on a macro to do a few different things, I'm just stuck on this part.
What I want to do is go through all of my rows, and if the first cell in the row is empty, I want to select the entire row and UnMerge it. I know that if the first cell is empty, that row contains merged cells that I want unmerged.
I know that these two lines below will select row 2 and Unmerge it, but I need a way to loop through my data and find and unmerge the rows automatically, because the row numbers wont be the same every time.
Rows("2:2").Select
Selection.UnMerge
I want to do something like this:
For (each row) {
if (the first cell is empty) {
UnMerge all cells in that row;
}
}
I just don't know how to do that in VBA syntax.
Any help is appreciated!
This should do the job.
Sub UnmergeRows()
Dim R As Long ' loop counter: rows
With Worksheets("Sheet1") ' change tab name as required
' "B" should be the longest column in the worksheet
For R = 2 To .Cells(.Rows.Count, "B").End(xlUp).Row
If IsEmpty(.Cells(R, 1)) Then .Rows(R).MergeCells = False
Next R
End With
End Sub

How do I copy the rows of which certain cells contain text, and paste them to another location?

Some cells in column J contain texts and the others are empty.
I want to 1. find all the rows where column J contains text 2. copy these rows 3.paste them somewhere else (in the same order as the initial table)
Sub org1()
Dim a As Range
For Each a In Range("j2:j500")
If Not a.Value = "" Then
a.EntireRow.Copy
a.Offset(100, 0).Insert
End If
Next a
End Sub
error
There is no need to do a structure like that. Simply filter the column excluding the blank cells, then copy the filtered column to wherever you want. If you don't know how to do the filter in vba, record a macro and do the process.
If you don't want the column filtered after that, just remove any filters afterwards with Sheets("YourSheet").AutoFilterMode = False

Loop through column counting cells that are not blank

I am trying to loop through a column and count the number of cells that are not blank. The process that it must follow are:
Start at first cell and count non blank cells until you get to two consecutive blank cells
STOP at this point and return the value of the count to a cell on the sheet
Start Count again at next non blank cell and repeat 1. and 2. until you have reached the end of the data
Extra: if I could also return the date in the row that relates to the first non blank and also the first blank (from the point at which you find the first two blank cells)
If you need more information please ask. I presume VBA would be much better at doing this?
Thanks.
Code so far is:
Sub Test1()
Range("I3").Select
Do Until IsEmpty(ActiveCell) And IsEmpty(ActiveCell.Offset(1, 0))
Dim iVal As Integer
iVal = Application.WorksheetFunction.CountIf(Range("I:I"), "TRUE")
ActiveCell.Offset(2, 0).Select
Loop
End Sub
As you mention I presume VBA would be much better at doing this? I am assuming you would contemplate a formula solution.
Assuming data is in ColumnA starting with a single blank cell, in B2:
=IF(AND(ISBLANK(A2),ISBLANK(A3)),COUNTA(A$1:A2)-SUM(B$1:B1),"")
dragged down until 0 appears.

Link selective rows from one sheet to the next

So I've searched everywhere... I have an office 2007 excel spreadsheet with two pages, one labeled "i" and the other "t." I need to display selective rows (those rows that have a value in column A--not all do) from "i" in "t." I also need the rows in the "t" page to be in numerical order. I figured out how to do it across all rows, but not how to selectively add rows with values only in column A. Further, when I add new rows to "i," "t" doesn't automatically update. Any advice on how to accomplish this would be of immense help!
I have access to office 2010. I don't know if that makes the coding easier?
Thank you!
Jason
You could try a user-defined function like this:
Function NonBlank(Selection As Range, Index As Integer) As Variant
Dim Count As Integer
Count = 0
For Each cell In Selection
If Len(cell.Value) > 0 Then Count = Count + 1
If Index = Count Then
NonBlank = cell.Value
Exit For
End If
Next
End Function
Then on Sheet t, you can put =NonBlank(i!A:A,ROW(A1)) in the first cell where you want the first non-blank value of Sheet i, and then copy the formulas down.

Resources