Macro to insert rows in a spreadsheet
Closed
Cobs
-
Mar 20, 2012 at 05:56 PM
rizvisa1 Posts 4478 Registration date Thursday January 28, 2010 Status Contributor Last seen May 5, 2022 - Mar 25, 2012 at 07:32 AM
rizvisa1 Posts 4478 Registration date Thursday January 28, 2010 Status Contributor Last seen May 5, 2022 - Mar 25, 2012 at 07:32 AM
Related:
- Macro to insert rows in a spreadsheet
- Insert check mark in word - Guide
- Insert key - Guide
- How to insert photo in word for resume - Guide
- Google spreadsheet right to left - Guide
- How to insert at the rate in laptop - Guide
1 response
rizvisa1
Posts
4478
Registration date
Thursday January 28, 2010
Status
Contributor
Last seen
May 5, 2022
766
Mar 25, 2012 at 07:32 AM
Mar 25, 2012 at 07:32 AM
try this. The main macro is "insertBlankRows"
Option Explicit
Public Sub insertBlankRows()
Dim lastRow As Long
Dim dataFirstRow As Long
Dim scanCol As Variant
Dim counter As Integer
Dim changedRow As Boolean
' the last used row in column A
lastRow = getItemLocation("*", Cells)
'location of first data row
dataFirstRow = 2
'columns to be scanned
scanCol = Array("B", "C", "D", "E")
Do While lastRow > dataFirstRow
changedRow = False
For counter = 0 To UBound(scanCol)
'is value different from row above
If (Cells(lastRow, scanCol(counter)) <> Cells(lastRow - 1, scanCol(counter))) _
Then
'it is different
changedRow = True
Exit For
End If
Next
If (changedRow) _
Then
Rows(lastRow).Insert
End If
lastRow = lastRow - 1
Loop
End Sub
Public Function getItemLocation(sLookFor As String, _
rngSearch As Range, _
Optional bFullString As Boolean = True, _
Optional bLastOccurance As Boolean = True, _
Optional bFindRow As Boolean = True) As Long
'find the first/last row/column within a range for a specific string
Dim Cell As Range
Dim iLookAt As Integer
Dim iSearchDir As Integer
Dim iSearchOdr As Integer
If (bFullString) _
Then
iLookAt = xlWhole
Else
iLookAt = xlPart
End If
If (bLastOccurance) _
Then
iSearchDir = xlPrevious
Else
iSearchDir = xlNext
End If
If Not (bFindRow) _
Then
iSearchOdr = xlByColumns
Else
iSearchOdr = xlByRows
End If
With rngSearch
If (bLastOccurance) _
Then
Set Cell = .Find(sLookFor, .Cells(1, 1), xlValues, iLookAt, iSearchOdr, iSearchDir)
Else
Set Cell = .Find(sLookFor, .Cells(.Rows.Count, .Columns.Count), xlValues, iLookAt, iSearchOdr, iSearchDir)
End If
End With
If Cell Is Nothing Then
getItemLocation = 0
ElseIf Not (bFindRow) _
Then
getItemLocation = Cell.Column
Else
getItemLocation = Cell.Row
End If
Set Cell = Nothing
End Function