Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Inserting rows based on another cells value

I have a column of cells that could have the value of
either 1, 2 or 3.

I want 1 row added below the cell if the value is 2, 2
rows added below the cell if the value is 3 and no cells
added if the value is 1.

Anyone know if this is possible and if it is how is it
done.
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,885
Default Inserting rows based on another cells value

Hi Mike
try the following:

Sub insert_rows()
Dim lastrow As Long
Dim row_index As Long

lastrow = ActiveSheet.Cells(Rows.count, "A").End(xlUp).row
For row_index = lastrow - 1 To 1 Step -1
with Cells(row_index, "A")
If .Value =2 or .value = 3 then
Cells(row_index + 1, "A").resize(.value-1,1).EntireRow.Insert
(xlShiftDown)
End If
end with
Next
End Sub



--
Regards
Frank Kabel
Frankfurt, Germany


MikeT wrote:
I have a column of cells that could have the value of
either 1, 2 or 3.

I want 1 row added below the cell if the value is 2, 2
rows added below the cell if the value is 3 and no cells
added if the value is 1.

Anyone know if this is possible and if it is how is it
done.


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3
Default Inserting rows based on another cells value

Thanks Frank,

Here's more of my work you might want to do for me.

In the rows that are inserted, can I copy the data from
certain cells(not the whole row, but constant certain
cells) in the row above that caused the insert.

Thanks for your help

MikeT

P.S. KKKNIE, I couldn't get your macro to run.



-----Original Message-----
Hi Mike
try the following:

Sub insert_rows()
Dim lastrow As Long
Dim row_index As Long

lastrow = ActiveSheet.Cells(Rows.count, "A").End(xlUp).row
For row_index = lastrow - 1 To 1 Step -1
with Cells(row_index, "A")
If .Value =2 or .value = 3 then
Cells(row_index + 1, "A").resize(.value-

1,1).EntireRow.Insert
(xlShiftDown)
End If
end with
Next
End Sub



--
Regards
Frank Kabel
Frankfurt, Germany


MikeT wrote:
I have a column of cells that could have the value of
either 1, 2 or 3.

I want 1 row added below the cell if the value is 2, 2
rows added below the cell if the value is 3 and no cells
added if the value is 1.

Anyone know if this is possible and if it is how is it
done.


.

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Inserting rows based on another cells value

A quick macro:

Code
-------------------
Sub test()

Dim i As Long

For i = 2 To 1000
If Cells(i - 1, 1).Value = 2 Then Cells(i, 1).EntireRow.Insert shift:=xlDown
If Cells(i - 1, 1).Value = 3 Then
Cells(i, 1).EntireRow.Insert shift:=xlDown
Cells(i, 1).EntireRow.Insert shift:=xlDown
End If
Next

End Su
-------------------

Change the 1000 to the number of rows you have and change the one afte
the comma in cells(i-1, 1) and cells(i,1) to the column number.



--
Message posted from http://www.ExcelForum.com

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 102
Default Inserting rows based on another cells value

Hi Mike, to get the values repeated for the inserted row select the column after this macro. (not the whole column)


Sub MoreRows()
Dim Rng As Excel.Range, i As Long
Set Rng = Selection
Application.ScreenUpdating = False
ActiveCell.Select
With Rng
For i = .Cells.Count To 1 Step -1
If .Cells(i, 1).Value 1 And _
.Cells(i, 1).Value < 4 Then _
.Rows(i + 1).Resize(.Cells(i, 1).Value - _
1, 1).EntireRow.Insert shift:=xlDown
Application.StatusBar = "Inserting rows at row " & i
Next
End With
End Sub

Now press F5 Special Blanks OK = arrow up Ctrl + Enter


Regards Robert

"MikeT" wrote in message ...
I have a column of cells that could have the value of
either 1, 2 or 3.

I want 1 row added below the cell if the value is 2, 2
rows added below the cell if the value is 3 and no cells
added if the value is 1.

Anyone know if this is possible and if it is how is it
done.



---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.655 / Virus Database: 420 - Release Date: 08/04/2004




Reply
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
inserting rows keeping the merged cells Syd100 Excel Discussion (Misc queries) 1 November 29th 07 01:29 PM
need help please inserting multiple rows based on cell value then copying to data sheet [email protected] Excel Worksheet Functions 1 July 1st 07 08:44 PM
Highting rows based on content of cells Sarah Excel Worksheet Functions 1 January 24th 07 09:44 PM
inserting a named range into new cells based on a named cell Peter S. Excel Discussion (Misc queries) 1 June 4th 06 03:53 AM
List rows in an order based on a cells value Josh M Excel Discussion (Misc queries) 0 September 14th 05 04:51 PM


All times are GMT +1. The time now is 04:44 AM.

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"