Solved

Using EXCEL VBA, I want to copy a row of cells of variable length and paste into a column at the first blank row in the column

Posted on 2015-01-27
7
362 Views
Last Modified: 2015-01-27
In trying to modify code from previous expert help, I am trying to use the following line to copy a row of length NrOpns from "Sheet 1" and paste into "Sheet 2" in the first non-blank cell.

See attached spreadsheet for illustration.

'Copy row of value of length NrOpns from "Sheet1" into "Sheet2"
Worksheets("Sheet2").Range("C" & firstRow, "C" & firstRow + NrOpns - 1).String = Worksheets("Sheet1").Range(Cells(2, 9), Cells(2, 9 + NrOpns)).String

As is, it causes VB to choke. If I change .string to .value, it only copies the first cell (2,9) into NrOpns number of rows in Sheet2.
Copy-and-Paste-Example.xlsx
0
Comment
Question by:EdLB
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 30

Expert Comment

by:gowflow
ID: 40573788
Sorry not clear what you want to copy.

Best way is you to fill Sheet2 with the results then will built the VBA accordingly.
gowflow
0
 

Author Comment

by:EdLB
ID: 40573838
goflow, did you see the attached spreadsheet for illustration? Just using a Macro recording to build the VB won't work because I am using a variable number for the number of columns in Sheet1.
0
 
LVL 30

Expert Comment

by:gowflow
ID: 40573875
I didn't say use a macro recording.

I said fill the sheet2 with the result to see an example of how you want the data transferred as your notes and explanation got me now where in understanding what you want.

gowflow
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:EdLB
ID: 40574127
See attached file for updated explanation.
Copy-and-Paste-Examplev2.xlsx
0
 
LVL 23

Expert Comment

by:Michael Fowler
ID: 40574181
I have updated the code from my last example to include this functionality
Sub CopyPart()
    Dim firstRow As Long, stepCount As Long, i 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
    
    For i = 0 To stepCount - 1
        Worksheets("Sheet2").Range("C" & firstRow + i).Value = Worksheets("Sheet1").Cells(2, i + 9)
    Next
    
End Sub

Open in new window

Copy-and-Paste-Example.xlsm
0
 
LVL 23

Accepted Solution

by:
Michael Fowler earned 500 total points
ID: 40574197
Assuming that you will want this function to run against all the part numbers in sheet 1 and not just the first here is the code edited to allow for this

Sub CopyAll()
    Dim lastrow As Long, i As Long
    
    lastrow = Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row
    For i = 3 To lastrow
        CopyPart Worksheets("Sheet1").Range("A" & i)
    Next
    
End Sub

Sub CopyPart(rng As Range)
    Dim firstRow As Long, stepCount As Long, i As Long
    
    firstRow = Worksheets("Sheet2").Range("A" & Rows.Count).End(xlUp).Row + 1
    stepCount = rng.Offset(0, 7).Value
    
    Worksheets("Sheet2").Range("A" & firstRow, "A" & firstRow + stepCount - 1).Value = rng.Value
    
    For i = 0 To stepCount - 1
        Worksheets("Sheet2").Range("C" & firstRow + i).Value = Worksheets("Sheet1").Cells(2, i + 9)
    Next
    
End Sub

Open in new window

Copy-and-Paste-Example.xlsm
0
 

Author Closing Comment

by:EdLB
ID: 40574446
Thanks again and I have one more...
0

Featured Post

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

691 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