LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,758
Default Compare cells and insert row

When you use Cells(X,Y) to refer to a range, the X represents the row and the Y
represents the column.

And rows are numbered (1, 2, ..., 65536) and columns are usually lettered (A, B,
...., IV) <but there's a setting that can show numbers (1, 2, ..., 256).

And this won't work:
Rows("j:j").Select

But if you could use:

rows(j & ":" & j).select
or even
rows(j).select

But it's not necessary to select most things to work with them.

I wasn't sure what column contained the key value, so I guessed column A.

Try this against a copy of the worksheet.

Option Explicit
Sub testme()

Dim wks As Worksheet
Dim FirstRow As Long
Dim LastRow As Long
Dim iRow As Long

Set wks = Worksheets("sheet1")
With wks
FirstRow = 2 'headers in 1
LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row

For iRow = LastRow To FirstRow + 1 Step -1
If .Cells(iRow, "A").Value = .Cells(iRow - 1, "A").Value Then
'do nothing
Else
.Rows(iRow).Resize(2).Insert
.Rows(1).Copy _
Destination:=.Cells(iRow + 1, "A")
End If
Next iRow
End With
End Sub

Tom Fortune wrote:

Have have rows of data for deviations. I sort by Close date and delete all
rows that have been closed. I then sort by department. What i want to do
next is insert a blank row and copy the first row in the first row so that it
is easy to cut and paste the data for each department and email to those
departments. I'm having trouble with an if statement that compares the
active cell with the next or previous (which ever is easiest), and if they
are different, insert the blank and header. The blank row is not abosilutely
necessary. It just helps to cut and paste into the email.

This is what I was trying:

Dim cRowsB As Integer
Dim j As Integer

cRowsB = Cells(Rows.Count, 2).End(xlUp).Row
Range("G3").Select

For j = 3 To cRowsB
If Cell(G, j).Value = Cell(G, j - 1).Value Then
Rows("j:j").Select
Selection.Insert Shift:=xlDown
Rows("1:1").Select
Selection.Copy
Rows("j:j").Select
Selection.Insert Shift:=xlDown
Selection.Paste
ActiveCell.Offset(1, 0).Select
End If
Next j

Thanks for any help. I'm a really not good at VB.

"Dan Gardner" wrote:



"Tom Fortune" wrote:

After sorting, how is the best way to compare two adjacent cells in a column,
and if they are different, insert a blank row and a header row between them.
--
Sincerely,
Tom Fortune


Tom,
Not completely sure about the question, but to insert rows and clolumns you
can use the following code

Rows("XX:XX").Select
Selection.Insert Shift:=xlDown
Columns("XX:XX").Select
Selection.Insert Shift:=xlToRight

as for reading cells
workbooks("XXXXX").worksheets("XXXXXX").cells(XX, XX).value
or if your not changeing workbooks and/or worksheets then just simple
cells(XX, XX).value
works

if this isn't mutch help you can always record a macro of you doing what you
want to do and then view and edit the code to fit your needs


--

Dave Peterson


 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
compare 2 column cells and return the adjacent columns cells data of the cell trebor57 Excel Worksheet Functions 1 February 1st 11 02:54 PM
Compare 1 cell to column of cells returning adjacent cells info? Mr. Fine Excel Worksheet Functions 1 April 15th 10 07:36 PM
Need a insert a empty cell if compare different function saici Excel Discussion (Misc queries) 2 June 9th 06 08:18 AM
How do I compare cells and if FALSE compare to next cell in EXCEL Cindie Excel Worksheet Functions 0 March 24th 06 05:29 PM
Compare 2 cells in 2 worksheets, rewrite one of the cells dbomb Excel Programming 1 September 28th 04 09:16 AM


All times are GMT +1. The time now is 08:20 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"