Solved

Adding a second worksheet that has same 'Copy partial row' functionality as Worksheet 1

Posted on 2014-02-28
7
394 Views
Last Modified: 2014-03-01
I am working in Excel 2010. I have attached a sample file that currently copies a partial row from Worksheet 1 to Worksheet 2 based on the criteria outlined below. I need to add Worksheet 3 to this workbook, and want it to have exactly the same functionality as Worksheet 1 — if it meets the same criteria as outlined below, then a partial row is copied into Worksheet 2. So basically Worksheet 2 can be populated by both Worksheet 1 and Worksheet 3.

Criteria and desired result as per sample file attached:
If, in worksheet called "Worksheet 1":
A cell in any row in Column V: is not blank
A cell in the same row as above in Column X: = "Yes"
Then the following cells from the above row are copied and pasted, as values, into the next available blank row in "Worksheet 2", mapped as follows:
Column V value to Column A
Column W value to Column B
Column A value to Column C
Column B value to Column D
Column L value to Column E
Column M value to Column F

Further details:
If there are blank values in any of the cells being copied from Worksheet 1 or Worksheet 3, then the cell in Worksheet 2 would also show as blank.
The values are to be copied over just once into Worksheet 2.
The process would happen automatically anytime the above two conditions are met in Worksheet 1 or Worksheet 3.

Thanks,
Andrea
CopyPartialRow.xlsm
0
Comment
Question by:Andreamary
  • 4
  • 3
7 Comments
 
LVL 33

Accepted Solution

by:
Norie earned 500 total points
Comment Utility
Remove the code from Sheet1 and paste this code in the ThisWorkbook module.
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

    If Sh Is Sheet2 Then Exit Sub
    
    If Target.EntireRow.Cells(R, 22) <> "" And Target.EntireRow.Cells(R, 24) = "Yes" Then
        CopyRow Target
    End If
    
End Sub

Open in new window


Also, adjust CopyRow and CopyRow2 to take a range as an argument.
Sub CopyRow2(ByVal R As Range, ByVal R2 As Integer)

    Sheet2.Cells(R2, 1) = R.EntireRow.Cells(1, 22)
    Sheet2.Cells(R2, 2) = R.EntireRow.Cells(1, 23)
    Sheet2.Cells(R2, 3) = R.EntireRow.Cells(1, 1)
    Sheet2.Cells(R2, 4) = R.EntireRow.Cells(1, 2)
    Sheet2.Cells(R2, 5) = R.EntireRow.Cells(1, 12)
    Sheet2.Cells(R2, 6) = R.EntireRow.Cells(1, 13)

End Sub

Sub CopyRow(ByVal R As Range)

    If WorksheetFunction.CountA(Sheet2.Cells) = 0 Then
        R2 = 1
    Else
        R2 = Sheet2.Range("A" & Sheet2.Rows.Count).End(xlUp).Row + 1
    End If
    
    CopyRow2 R, R2
End Sub

Open in new window

0
 

Author Closing Comment

by:Andreamary
Comment Utility
Perfect! Thanks very much for the quick response...
0
 

Author Comment

by:Andreamary
Comment Utility
The initial test of this solution worked, but runtime errors are surfacing after more than a copy of rows are copied over. Should I start a new thread, or can this be addressed in this thread? I've attached the file to illustrate what is happening.
CopyPartialRow-TwoSheetTest.xlsm
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 33

Expert Comment

by:Norie
Comment Utility
What do you mean 'more than a copy of the rows are copied over'?
0
 

Author Comment

by:Andreamary
Comment Utility
I'm sorry but I really jumped the gun on closing this question when I did. :-( Please advise how to best rectify this. I'm providing a description of the errors I'm encountering in the file I attached in my previous post. I'm not an expert, so don't hesitate to ask for clarification if my explanations aren't clear. And if you feel that this should be handled as a new thread on my part, just let me know:

1. When entering "Yes" in X1 on either Worksheet 1 or Worksheet 3, run-time error 1004 triggered and line not copied to Worksheet 2 despite meeting criteria

2. Removing "Yes" from Worksheet 1 or Worksheet 3 triggers the line to be copied into Worksheet 2. We need the ability to remove "Yes" without it triggering any action.

3. Reapplying "Yes" to a line that has already been copied over to Worksheet 2 does not trigger the line to be copied over again. (We need the ability to remove "Yes" and reapply "Yes" to lines that have already been copied into Worksheet 2. Worksheet 1 and 3 are lists of chapters in a book, and the same chapter could be revised several times within one cycle, and each time it's going to be revised we copy it again into Worksheet 2.)

4. Removing "Yes" and/or reinserting "Yes" from X1 in either Worksheet 1 or Worksheet 3 triggers run-time error 1004 in Private Sub Workbook_SheetChange: the following line is highlighted in yellow:
 If Target.EntireRow.Cells(R, 22) <> "" And Target.EntireRow.Cells(R, 24) = "Yes" Then

Thanks for your patience,

Andrea
0
 
LVL 33

Expert Comment

by:Norie
Comment Utility
Oops, the problem is something from the old code that I should have fixed.

Try this for the code in the workbook module.
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)

    If Sh Is Sheet2 Then Exit Sub
    
    If Target.EntireRow.Cells(1, 22) <> "" And Target.EntireRow.Cells(1, 24) = "Yes" Then
        Application.EnableEvents = False
        CopyRow Target
        Application.EnableEvents = True
    End If
    
End Sub

Open in new window


I think that fixes all the points you raised but it's probably best if you check again.
0
 

Author Comment

by:Andreamary
Comment Utility
All good now! I made sure to test it in several ways. Thanks so much...
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
XMind Plus helps organize all details/aspects of any project from large to small in an orderly and concise manner. If you are working on a complex project, use this micro tutorial to show you how to make a basic flow chart. The software is free when…

743 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

11 Experts available now in Live!

Get 1:1 Help Now