ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   row insertion before/after (https://www.excelbanter.com/excel-programming/435463-row-insertion-before-after.html)

anisha via OfficeKB.com

row insertion before/after
 
Hi,

I want to insert a row if the value in col.a and col. b of any specific row
is different.
Please help me out without least posssible chang in my code since as a
beginner i won't be able to grasp the changes easily


Also please tellme how to insert a row previous to the current row. my code i
think is inserting row after the current row
Sub Testinsert()

Dim x, y As Integer
x = ActiveCell.Row
x = 1

Do While Cells(x, 1).Value < " "
y = x + 1
If Cells(x, 1).Value < Cells(x, 2).Value Then
Cell.EntireRow.Insert
Cells(y, 1) = "a" 'assuming new inserted rowindex to be y(ie x+1)
Cells(y, 2) = "b"
End If
x = x + 1
Loop

End Sub

Thanks,thanks, thanks a lot

--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.a...mming/200910/1


Per Jessen

row insertion before/after
 
Hi

I would use a For...Next loop, and start from last row stepping up to first
data row. When you insert a row, the rows below are pushed down.

I know you did not wanted to much change, this is a much better approach. Do
feel free to ask if you need any clarification on what the code do.

Sub TestInsertA()
Dim LastRow As Long, FirstDataRow As Long
Dim r as Long

FirstDataRow = 2 'Assume headings in row 1
LastRow = Range("A" & Rows.Count).End(xlUp).Row 'Last row with data
For r = LastRow To FirstDataRow Step -1
If Cells(r, 1).Value < Cells(r, 2).Value Then
Rows(r).EntireRow.Insert
Cells(r, 1) = "a"
Cells(r, 2) = "b"
End If
Next
End Sub

Hopes this helps.
....
Per

"anisha via OfficeKB.com" <u54234@uwe skrev i meddelelsen
news:9e36918489321@uwe...
Hi,

I want to insert a row if the value in col.a and col. b of any specific
row
is different.
Please help me out without least posssible chang in my code since as a
beginner i won't be able to grasp the changes easily


Also please tellme how to insert a row previous to the current row. my
code i
think is inserting row after the current row
Sub Testinsert()

Dim x, y As Integer
x = ActiveCell.Row
x = 1

Do While Cells(x, 1).Value < " "
y = x + 1
If Cells(x, 1).Value < Cells(x, 2).Value Then
Cell.EntireRow.Insert
Cells(y, 1) = "a" 'assuming new inserted rowindex to be y(ie x+1)
Cells(y, 2) = "b"
End If
x = x + 1
Loop

End Sub

Thanks,thanks, thanks a lot

--
Message posted via OfficeKB.com
http://www.officekb.com/Uwe/Forums.a...mming/200910/1




All times are GMT +1. The time now is 09:39 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com