Repeating macro without overwriting

Foxvh

New Member
Joined
Jul 9, 2020
Messages
14
Office Version
  1. 2010
Platform
  1. Windows
Hi Guys,

i have a macro that inputs data from one sheet to another and have been trying to get it to repeat based on a cell value everything.
as i want this to create multiple lines every time it loops it just overwrites the first line was input.

VBA Code:
Dim lRow As Long
    Dim ws As Worksheet
    Set ws = Worksheets("Sheet1")
    lRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
    With ws
        .Cells(lRow, 6).Value = Worksheets("Input").Range("B5").Value
        .Cells(lRow, 1).Value = Worksheets("Input").Range("C5").Value
        .Cells(lRow, 5).Value = Worksheets("Input").Range("D5").Value
        .Cells(lRow, 4).Value = Worksheets("Input").Range("E9").Value
        .Cells(lRow, 3).Value = Worksheets("Input").Range("E11").Value
        .Cells(lRow, 11).Value = Worksheets("Input").Range("E13").Value
        .Cells(lRow, 8).Value = Worksheets("Input").Range("E15").Value
        .Cells(lRow, 12).Value = Worksheets("Input").Range("E17").Value
        .Cells(lRow, 14).Value = Worksheets("Input").Range("H8").Value
        .Cells(lRow, 7).Value = Worksheets("Input").Range("H10").Value
        .Cells(lRow, 13).Value = Worksheets("Input").Range("H12").Value
        .Cells(lRow, 9).Value = Worksheets("Input").Range("E19").Value
        .Cells(lRow, 10).Value = Worksheets("Input").Range("E20").Value

    End With

    End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Hi & welcome to MrExcel.
What cell shows the number of copies needed?
 
Upvote 0
Sorry i forgot to send the updated code
the below is the code with the one with the repeater

VBA Code:
Sub Insert()


Dim lRow As Long
    Dim ws As Worksheet
    Dim i As Long
    Set ws = Worksheets("Sheet1")
    lRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
    With ws
    For i = 1 To Range("B7").Value
        .Cel ls(lRow, 6).Value = Worksheets("Input").Range("B5").Value
        .Cells(lRow, 1).Value = Worksheets("Input").Range("C5").Value
        .Cells(lRow, 5).Value = Worksheets("Input").Range("D5").Value
        .Cells(lRow, 4).Value = Worksheets("Input").Range("E9").Value
        .Cells(lRow, 3).Value = Worksheets("Input").Range("E11").Value
        .Cells(lRow, 11).Value = Worksheets("Input").Range("E13").Value
        .Cells(lRow, 8).Value = Worksheets("Input").Range("E15").Value
        .Cells(lRow, 12).Value = Worksheets("Input").Range("E17").Value
        .Cells(lRow, 14).Value = Worksheets("Input").Range("H8").Value
        .Cells(lRow, 7).Value = Worksheets("Input").Range("H10").Value
        .Cells(lRow, 13).Value = Worksheets("Input").Range("H12").Value
        .Cells(lRow, 9).Value = Worksheets("Input").Range("E19").Value
        .Cells(lRow, 10).Value = Worksheets("Input").Range("E20").Value
        Next i
    End With

    End Sub
 
Upvote 0
Which sheet is the value on, Sheet1 or Input?
 
Upvote 0
the value is on input.
sheet 1 is where all the data will be copied to from the input sheet.
 
Upvote 0
Ok, thanks for that.
Rather than looping try
VBA Code:
Sub Insert()
   Dim lRow As Long, Rws As Long
   Dim ws As Worksheet
   
   Set ws = Worksheets("Sheet1")
   lRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).Row
   
   With Worksheets("Input")
      Rws = .Range("B7").Value
      ws.Cells(lRow, 6).Resize(Rws).Value = .Range("B5").Value
      ws.Cells(lRow, 1).Resize(Rws).Value = .Range("C5").Value
      ws.Cells(lRow, 5).Resize(Rws).Value = .Range("D5").Value
      ws.Cells(lRow, 4).Resize(Rws).Value = .Range("E9").Value
      ws.Cells(lRow, 3).Resize(Rws).Value = .Range("E11").Value
      ws.Cells(lRow, 11).Resize(Rws).Value = .Range("E13").Value
      ws.Cells(lRow, 8).Resize(Rws).Value = .Range("E15").Value
      ws.Cells(lRow, 12).Resize(Rws).Value = .Range("E17").Value
      ws.Cells(lRow, 14).Resize(Rws).Value = .Range("H8").Value
      ws.Cells(lRow, 7).Resize(Rws).Value = .Range("H10").Value
      ws.Cells(lRow, 13).Resize(Rws).Value = .Range("H12").Value
      ws.Cells(lRow, 9).Resize(Rws).Value = .Range("E19").Value
      ws.Cells(lRow, 10).Resize(Rws).Value = .Range("E20").Value
   End With
End Sub
 
Upvote 0
oh wow that works so much better thank you very much! :)

I've only just started using VBA but i'm really enjoying working with excel and VBA so trying to learn as much as i can.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,888
Messages
6,175,215
Members
452,618
Latest member
Tam84

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