Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 17
Default insert rows based on a cell's value (macro)

Hello,

I'm trying to write a macro that will insert a number of rows based on a
cell's value.

For example, if the value of a cell in column "U", beginning w/ "U2", is 2,
insert one row under it. Then, go to the next integer in column "U" (in this
case, "U4". I would like for the macro to repeat until there is no longer a
value in column "U".

Note: If the value in "U" is 1, then no rows would need to be inserted.

The code that I have so far is as follows:

Sub AddMultitipleRows()

If Range("U2") 1 Then
'Insert that number of rows
Rows.Insert Shift:=xlDown
End If

Do Until ActiveCell.Value = 0
ActiveCell.Offset(1, 0).Select
Loop

End Sub

I appreciate any help you can provide.

Thank you.

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 35,218
Default insert rows based on a cell's value (macro)

It's lots easier if you start at the bottom and work your way up:

Option Explicit
Sub AddMultitipleRows2()

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

Set wks = ActiveSheet

With wks
FirstRow = 2
LastRow = .Cells(.Rows.Count, "U").End(xlUp).Row

For iRow = LastRow To FirstRow Step -1
With .Cells(iRow, "U")
If IsNumeric(.Value) Then
If .Value 1 Then
.Offset(1, 0).Resize(.Value).EntireRow.Insert
End If
End If
End With
Next iRow
End With

End Sub

Shoney wrote:

Hello,

I'm trying to write a macro that will insert a number of rows based on a
cell's value.

For example, if the value of a cell in column "U", beginning w/ "U2", is 2,
insert one row under it. Then, go to the next integer in column "U" (in this
case, "U4". I would like for the macro to repeat until there is no longer a
value in column "U".

Note: If the value in "U" is 1, then no rows would need to be inserted.

The code that I have so far is as follows:

Sub AddMultitipleRows()

If Range("U2") 1 Then
'Insert that number of rows
Rows.Insert Shift:=xlDown
End If

Do Until ActiveCell.Value = 0
ActiveCell.Offset(1, 0).Select
Loop

End Sub

I appreciate any help you can provide.

Thank you.


--

Dave Peterson
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 9,101
Default insert rows based on a cell's value (macro)

Sub AddMultitipleRows()
LastRow = Range("U" & Rows.Count).End(xlup).Row
RowCount = LastRow
Do while RowCount = 1
If Range("U2") 1 Then
Rows((RowCount + 1) & ":" & (RowCount + Range("U2")) ).Insert
End If
RowCount = RowCount - 1
Loop
End Sub


"Shoney" wrote:

Hello,

I'm trying to write a macro that will insert a number of rows based on a
cell's value.

For example, if the value of a cell in column "U", beginning w/ "U2", is 2,
insert one row under it. Then, go to the next integer in column "U" (in this
case, "U4". I would like for the macro to repeat until there is no longer a
value in column "U".

Note: If the value in "U" is 1, then no rows would need to be inserted.

The code that I have so far is as follows:

Sub AddMultitipleRows()

If Range("U2") 1 Then
'Insert that number of rows
Rows.Insert Shift:=xlDown
End If

Do Until ActiveCell.Value = 0
ActiveCell.Offset(1, 0).Select
Loop

End Sub

I appreciate any help you can provide.

Thank you.

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
Need Macro- Insert rows based on dates and copy info from that row Katerinia Excel Discussion (Misc queries) 1 April 6th 10 08:02 PM
Insert text based on another cell's colour or font in Excel 2003 Philip Hinton Excel Discussion (Misc queries) 3 December 5th 07 01:48 AM
Insert text based on another cell's colour or font in Excel 2003 Philip Hinton Excel Worksheet Functions 3 December 5th 07 01:48 AM
Conditional Formatting for Rows based on one cell's outcome adnamadawn Excel Discussion (Misc queries) 3 January 12th 07 06:18 PM
Varying a macro based on a cell's value Neal Zimm Excel Programming 4 December 27th 04 07:37 AM


All times are GMT +1. The time now is 03:57 PM.

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

About Us

"It's about Microsoft Excel"