Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default Inserting Lines And Copying Them

Hi fellow Excel users,

I got the following problem:
I would like to insert the amount of lines specified in colomn "m".
This works fine. But i would also like to copy the lines . So when the
For loop detects a number like 3 , he has to insert 3 new lines and
copy the values from the line which had the value 3 somewhere in it's
row.

Now my code only inserts the amount of lines specified in "m".

My code:
Sub voegin()
Dim lastrow As Long
Dim row_index As Long



lastrow = ActiveSheet.Cells(Rows.Count, "m").End(xlUp).Row
For row_index = lastrow - 1 To 1 Step -1
If LCase(Cells(row_index + 1, "m").Value) = "1" Then
Cells(row_index + 1, "m").EntireRow.Insert _
(xlShiftUp)
ElseIf LCase(Cells(row_index + 1, "m").Value) = "2" Then
Cells(row_index + 1, "m").Resize(2, 1).EntireRow.Insert _
(xlShiftUp)
ElseIf LCase(Cells(row_index + 1, "m").Value) = "3" Then
Cells(row_index + 1, "m").Resize(3, 1).EntireRow.Insert _
(xlShiftUp)
ElseIf LCase(Cells(row_index + 1, "m").Value) = "4" Then
Cells(row_index + 1, "m").Resize(4, 1).EntireRow.Insert _
(xlShiftUp)
ElseIf LCase(Cells(row_index + 1, "m").Value) = "5" Then
Cells(row_index + 1, "m").Resize(5, 1).EntireRow.Insert _
(xlShiftUp)
ElseIf LCase(Cells(row_index + 1, "m").Value) = "6" Then
Cells(row_index + 1, "m").Resize(6, 1).EntireRow.Insert _
(xlShiftUp)
ElseIf LCase(Cells(row_index + 1, "m").Value) = "7" Then
Cells(row_index + 1, "m").Resize(7, 1).EntireRow.Insert _
(xlShiftUp)
ElseIf LCase(Cells(row_index + 1, "m").Value) = "8" Then
Cells(row_index + 1, "m").Resize(8, 1).EntireRow.Insert _
(xlShiftUp)
ElseIf LCase(Cells(row_index + 1, "m").Value) = "9" Then
Cells(row_index + 1, "m").Resize(9, 1).EntireRow.Insert _
(xlShiftUp)
End If
Next
End Sub

Does anybody know how i get the code to copy the lines as well?
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,441
Default Inserting Lines And Copying Them

Troy,

Simply do the copy prior to the insert. Also, you can use the value in the
cell as a variable, so no need for the select case structu

Sub voegin2()
Dim lastrow As Long
Dim row_index As Long
Dim myCnt As Integer

lastrow = ActiveSheet.Cells(Rows.Count, "m").End(xlUp).Row
For row_index = lastrow - 1 To 1 Step -1
myCnt = CInt(Cells(row_index + 1, "m").Value)
Cells(row_index + 1, "m").EntireRow.Copy
Cells(row_index + 1, "m").Resize(myCnt, 1).EntireRow.Insert
Application.CutCopyMode = False
Next
End Sub

HTH,
Bernie
MS Excel MVP

"Troyk" wrote in message
m...
Hi fellow Excel users,

I got the following problem:
I would like to insert the amount of lines specified in colomn "m".
This works fine. But i would also like to copy the lines . So when the
For loop detects a number like 3 , he has to insert 3 new lines and
copy the values from the line which had the value 3 somewhere in it's
row.

Now my code only inserts the amount of lines specified in "m".

My code:
Sub voegin()
Dim lastrow As Long
Dim row_index As Long



lastrow = ActiveSheet.Cells(Rows.Count, "m").End(xlUp).Row
For row_index = lastrow - 1 To 1 Step -1
If LCase(Cells(row_index + 1, "m").Value) = "1" Then
Cells(row_index + 1, "m").EntireRow.Insert _
(xlShiftUp)
ElseIf LCase(Cells(row_index + 1, "m").Value) = "2" Then
Cells(row_index + 1, "m").Resize(2, 1).EntireRow.Insert _
(xlShiftUp)
ElseIf LCase(Cells(row_index + 1, "m").Value) = "3" Then
Cells(row_index + 1, "m").Resize(3, 1).EntireRow.Insert _
(xlShiftUp)
ElseIf LCase(Cells(row_index + 1, "m").Value) = "4" Then
Cells(row_index + 1, "m").Resize(4, 1).EntireRow.Insert _
(xlShiftUp)
ElseIf LCase(Cells(row_index + 1, "m").Value) = "5" Then
Cells(row_index + 1, "m").Resize(5, 1).EntireRow.Insert _
(xlShiftUp)
ElseIf LCase(Cells(row_index + 1, "m").Value) = "6" Then
Cells(row_index + 1, "m").Resize(6, 1).EntireRow.Insert _
(xlShiftUp)
ElseIf LCase(Cells(row_index + 1, "m").Value) = "7" Then
Cells(row_index + 1, "m").Resize(7, 1).EntireRow.Insert _
(xlShiftUp)
ElseIf LCase(Cells(row_index + 1, "m").Value) = "8" Then
Cells(row_index + 1, "m").Resize(8, 1).EntireRow.Insert _
(xlShiftUp)
ElseIf LCase(Cells(row_index + 1, "m").Value) = "9" Then
Cells(row_index + 1, "m").Resize(9, 1).EntireRow.Insert _
(xlShiftUp)
End If
Next
End Sub

Does anybody know how i get the code to copy the lines as well?



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
Copying and inserting lines teo410 Excel Worksheet Functions 6 August 13th 09 05:31 PM
inserting new lines with number tripflex Excel Discussion (Misc queries) 2 March 6th 09 10:10 PM
Inserting # of lines from one spreadsheet to another klafert Excel Discussion (Misc queries) 1 August 30th 06 01:18 PM
Inserting Lines or Copying lines with formulas but without data wnfisba Excel Discussion (Misc queries) 2 August 18th 06 04:41 PM
Inserting Lines C[_2_] Excel Programming 3 October 3rd 03 10:44 PM


All times are GMT +1. The time now is 12:40 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"