Solved

Excel VBA code question

Posted on 2011-02-22
5
162 Views
Last Modified: 2012-05-11
I have code that in certain rows, selects cells E:J and pastes them on another sheet. Now, I also need to copy the contents of N (skipping data in other cells), and paste it farther away on the other sheet.

In summary: For certain rows, copy E:J and paste on other sheet at G:O, and also copy N and paste on other sheet at P.

Is there a painless way to do this without running through the code twice? Thanks!
Set rng = wsEWork.Range("E7:E" & lrow)
    For Each item In rng
        'was it selected with an X in column E?
        If UCase(item.Value) = "R" Then      'row was selected
            Range("E" & item.row & ":J" & item.row).Copy
            'transfer to Install Checklist
            wsInstall.Range("E" & nextECopy).PasteSpecial Paste:=xlPasteValues, _
                Operation:=xlNone, SkipBlanks:=False, Transpose:=False
            Application.CutCopyMode = False
            nextECopy = nextECopy + 1
        End If
        wsEWork.Activate
    Next item

Open in new window

0
Comment
Question by:nbozzy
  • 3
  • 2
5 Comments
 

Author Comment

by:nbozzy
ID: 34951544
Sorry -- code is correct, my description was wrong. I am pasting E:J into other sheet's E:J, but then first sheet's col N goes into other sheet's col P.
0
 
LVL 41

Expert Comment

by:dlmille
ID: 34957454
You merely need to add the  following in your loop:

            wsInstall.Range("N" & nextECopy).value = Range("P" & item.row)

Dave
See below



Set rng = wsEWork.Range("E7:E" & lrow)
    For Each item In rng
        'was it selected with an X in column E?
        If UCase(item.Value) = "R" Then      'row was selected
            Range("E" & item.row & ":J" & item.row).Copy
            'transfer to Install Checklist
            wsInstall.Range("E" & nextECopy).PasteSpecial Paste:=xlPasteValues, _
                Operation:=xlNone, SkipBlanks:=False, Transpose:=False
            Application.CutCopyMode = False
            wsInstall.Range("N" & nextECopy).value = Range("P" & item.row)
            nextECopy = nextECopy + 1
        End If
        wsEWork.Activate
    Next item

Open in new window

0
 
LVL 41

Expert Comment

by:dlmille
ID: 34957462
To optimize - no need for copy/paste - just direct value assignments in this case (runs much faster):
Set rng = wsEWork.Range("E7:E" & lrow)
    For Each item In rng
        'was it selected with an X in column E?
        If UCase(item.Value) = "R" Then      'row was selected
            'transfer to Install Checklist
            wsInstall.Range("E" & nextECopy & ":J" & item.row).value = Range("E" & item.row & ":J" & item.row)
            wsInstall.Range("N" & nextECopy).value = Range("P" & item.row)
            nextECopy = nextECopy + 1
        End If
        wsEWork.Activate
    Next item

Open in new window

Enjoy!

Dave
0
 
LVL 41

Accepted Solution

by:
dlmille earned 500 total points
ID: 34957476
Apologies - I just tested - need the .value on the right side (of the equals) for the source range assigment.

Dave
Set rng = wsEWork.Range("E7:E" & lrow)
    For Each item In rng
        'was it selected with an X in column E?
        If UCase(item.Value) = "R" Then      'row was selected
            'transfer to Install Checklist
            wsInstall.Range("E" & nextECopy & ":J" & item.row).value = Range("E" & item.row & ":J" & item.row).Value
            wsInstall.Range("N" & nextECopy).value = Range("P" & item.row).Value
            nextECopy = nextECopy + 1
        End If
        wsEWork.Activate
    Next item

Open in new window

0
 

Author Comment

by:nbozzy
ID: 34962421
Dave, a million thank-you's for solving this and also for showing me a slick new trick!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

895 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

16 Experts available now in Live!

Get 1:1 Help Now