VBA Attempting to Insert New Rows but Only needing Column A copied

horizonblue

New Member
Joined
Dec 7, 2023
Messages
16
Office Version
  1. 2010
Platform
  1. Windows
Hi,

Im using this VBA code at the moment.

Sub Macro1()
Dim i As Long, n As Variant
n = InputBox("How many rows:", "INSERT ROWS")
If n = "" Or Not IsNumeric(n) Or n < 1 Then Exit Sub
If Int(n) < Val(n) Then Exit Sub
i = 10
Do While Cells(i, "B") <> ""
i = i + 1
Loop
Rows(i & ":" & i + n - 1).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
Rows(i - 1 & ":" & i - 1).Copy
Rows(i & ":" & i + n - 1).PasteSpecial Paste:=xlPasteFormulas
Rows(i & ":" & i + n - 1).PasteSpecial Paste:=xlPasteFormats
Application.CutCopyMode = False
End Sub


The problem is, that it is copying everything in the previous rows. I am only wanting it to copy column A. Does anyone know how to fix this?

I'm also needing help to ensure that every time a new row is added that the reference number ticks over to the next numerical value.
 

Attachments

  • Capture.PNG
    Capture.PNG
    13.2 KB · Views: 21

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Try this. You don't need to use a loop to find the last row.

VBA Code:
Sub Macro1()
    Dim i As Long, n As Variant, lastRow As Long
    lastRow = Range("A" & Rows.Count).End(xlUp).Row
    n = InputBox("How many rows:", "INSERT ROWS")
    If n = "" Or Not IsNumeric(n) Or n < 1 Then Exit Sub
    If Int(n) < Val(n) Then Exit Sub
    Range("A" & lastRow + 1 & ":A" & lastRow + n).Formula = Range("A" & lastRow).Formula
    Range("A" & lastRow + 1 & ":A" & lastRow + n).NumberFormat = Range("A" & lastRow).NumberFormat
End Sub
 
Upvote 0
Hi, thank you.

The code adds a new line, but doesn't push out the bottom row that needs to remain. In the image attached the black square needs to remain underneath every new row, like the line highlighted in yellow.

It also does not add the next numerical number in the reference number.

Thanks for your help.
 

Attachments

  • Capture.PNG
    Capture.PNG
    14.7 KB · Views: 15
Upvote 0
What are the "Thumbs Down" representing? Is that part of your code or did it slip in there by accident?

I also can't find any question of what you want to achieve. You're showing code that does not work but no request.
It looks like you want to copy and paste. From where to where?
Anyway, let people know in a concise manner what you need to happen.
 
Last edited:
Upvote 0
Try this then
VBA Code:
Sub Macro1()
    Dim i As Long, n As Variant, lastRow As Long
    Dim refstr As String, refnum As Long, dashpos As Integer
    lastRow = Range("A" & Rows.Count).End(xlUp).Row
    n = InputBox("How many rows:", "INSERT ROWS")
    If n = "" Or Not IsNumeric(n) Or n < 1 Then Exit Sub
    If Int(n) < Val(n) Then Exit Sub
    Rows(lastRow + 1 & ":" & lastRow + n).Insert Shift:=xlDown
    refstr = Range("A" & lastRow).Value
    dashpos = InStr(1, refstr, "-")
    dashpos = InStr(dashpos + 1, refstr, "-")
    refnum = CLng(Mid(refstr, dashpos + 1))
    refstr = Left(refstr, dashpos)
    For i = 1 To n
        Range("A" & lastRow + i) = refstr & refnum
        refnum = refnum + 1
    Next i
End Sub

@jolivanes the thumbs are caused by the site interpreting (n) as a thumbs down emoji.
 
Upvote 1
The pictures have different increase of values. One in the middle (VBA-028/24-25 to VBA-029/24-25) and the other at the end (VBA-027/24-25 to VBA-027/24-26)
Which is it?

Re: "the thumbs are caused by the site interpreting (n) as a thumbs down emoji." New to me. Never seen that before. Thanks for the explanation.
 
Upvote 0
This is the way I understand the request. If not, let us know.
To increase value in the middle
Code:
Sub Maybe_1()
Dim lr As Long, rws As Long, i As Long
lr = Cells(Rows.Count, 1).End(xlUp).Offset(1).Row
rws = Application.InputBox("How many rows to insert?", "Number required.", , , , , , 1)
Cells(lr, 1).Resize(rws).EntireRow.Insert Shift:=xlDown
    For i = lr To lr + rws - 1
        Cells(i, 1).Value = Left(Cells(i - 1, 1), 4) & Format(Mid(Cells(i - 1, 1), 5, 3) * 1 + 1, "000") & Mid(Cells(i - 1, 1), 8)
    Next i
End Sub

To increase value at the end
Code:
Sub Maybe_2()
Dim lr As Long, rws As Long, i As Long
lr = Cells(Rows.Count, 1).End(xlUp).Offset(1).Row
rws = Application.InputBox("How many rows to insert?", "Number required.", , , , , , 1)
Cells(lr, 1).Resize(rws).EntireRow.Insert Shift:=xlDown
    For i = lr To lr + rws - 1
        Cells(i, 1).Value = Left(Cells(i - 1, 1), 11) & Mid(Cells(i - 1, 1), 12) * 1 + 1
    Next i
End Sub
 
Upvote 0
Try this then
VBA Code:
Sub Macro1()
    Dim i As Long, n As Variant, lastRow As Long
    Dim refstr As String, refnum As Long, dashpos As Integer
    lastRow = Range("A" & Rows.Count).End(xlUp).Row
    n = InputBox("How many rows:", "INSERT ROWS")
    If n = "" Or Not IsNumeric(n) Or n < 1 Then Exit Sub
    If Int(n) < Val(n) Then Exit Sub
    Rows(lastRow + 1 & ":" & lastRow + n).Insert Shift:=xlDown
    refstr = Range("A" & lastRow).Value
    dashpos = InStr(1, refstr, "-")
    dashpos = InStr(dashpos + 1, refstr, "-")
    refnum = CLng(Mid(refstr, dashpos + 1))
    refstr = Left(refstr, dashpos)
    For i = 1 To n
        Range("A" & lastRow + i) = refstr & refnum
        refnum = refnum + 1
    Next i
End Sub

@jolivanes the thumbs are caused by the site interpreting (n) as a thumbs down emoji.
Thank you for your assistance. This code has worked great! Unfortunately the rows added looses the boarder around it.
 
Upvote 0
That's fixable. Do you want borders like in your last picture?
 
Upvote 0

Forum statistics

Threads
1,223,885
Messages
6,175,181
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top