How to use EXCEL VBA to paste a single value down a predetermined number of rows?

Posted on 2015-01-26
Medium Priority
Last Modified: 2015-01-26
I have a part number with N steps in its routing. I want to copy the part number from cell A1 in spreadsheet "Sheet1" and paste the part number in column A of another spreadsheet "Sheet 2" on N rows. I have N in a variable called NrOpns. Also, when I go to "Sheet 2" I want to find the first non-blank row in column A and start the paste operation there.
Question by:EdLB
  • 2
  • 2
LVL 23

Expert Comment

by:Michael Fowler
ID: 40571563
Could you post an example workbook so we have an example to work with.

Here is a VBA example based on my understanding of the explaination

Sub test()

    Dim lastUsedRow As Long, partCounter As Long, n As Long
    Dim partPrefix As String, firstPartNo As String
    lastUsedRow = Worksheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row
    If lastUsedRow = 1 Then lastUsedRow = 0
    firstPartNo = Worksheets("Sheet1").Range("A1")
    partPrefix = Left(firstPartNo, Len(firstPartNo) - 2)
    partCounter = Right(firstPartNo, 2)
    n = Worksheets("Sheet1").Range("B1")
    For i = lastUsedRow + 1 To n
       Worksheets("Sheet2").Range("A" & i).Value = partPrefix & partCounter
       partCounter = partCounter + 1
End Sub

Open in new window

It assumes that the last two digits are the counter and that NrOpns is in cell B1. I have also attached a workbook with this Macro

Author Comment

ID: 40571728
Attached is a file with an example of the two sheets and the data.
LVL 23

Accepted Solution

Michael Fowler earned 2000 total points
ID: 40571751
Here you go

Sub CopyPart()
    Dim firstRow As Long, stepCount As Long
    firstRow = Worksheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row + 1
    stepCount = Worksheets("Sheet1").Range("H3").Value
    Worksheets("Sheet2").Range("A" & firstRow, "A" & firstRow + stepCount - 1).Value = Worksheets("Sheet1").Range("A3").Value
End Sub

Open in new window


Author Closing Comment

ID: 40571910
Thanks Michael, that's a huge help. And thanks also for getting back to me so quickly.

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
Video by: Zack
Viewers will learn about using Excel in a browser with Excel Online.
Viewers will learn the basics of formula auditing in Excel 2013.

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question