Solved

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

Posted on 2015-01-26
4
85 Views
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.
0
Comment
Question by:EdLB
  • 2
  • 2
4 Comments
 
LVL 23

Expert Comment

by:Michael74
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
    Next
    
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
Example.xlsm
0
 

Author Comment

by:EdLB
ID: 40571728
Attached is a file with an example of the two sheets and the data.
Copy-and-Paste-Example.xlsx
0
 
LVL 23

Accepted Solution

by:
Michael74 earned 500 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

Copy-and-Paste-Example.xlsm
0
 

Author Closing Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Viewers will learn what comprises a theme in Excel 2013, as well as how to customize them.
Viewers will learn the basics of printing in Excel 2013 and how to adjust some common settings.

759 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now