Solved

Excel VBA code question

Posted on 2011-02-22
5
163 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

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 code takes an Excel list of URL’s and adds a header titled “URL List”. It then searches through all URL’s in column “A”, looking for duplicates. When a duplicate is found, it is moved to the top of the list. The duplicate URL’s are then highlig…
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
This Micro Tutorial will demonstrate how to use a scrolling table in Microsoft Excel using the INDEX function.

773 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