You are here:

Excel/Using marked cells to select and copy rows

Advertisement


Question
I have a spreadsheet with many rows of data.  I have created a column A that a user puts a mark in (I have it as the letter X now) on rows that they would like to copy.  I am trying to create a macro that, when executed, will take all of the marked rows (with the X values in column A), copy them, then paste them in a new sheet that contains the same heading rows (rows 1-5).  Any help on how to execute this, or know of an easier method of completing this same action?

Answer
Dan,

If I wanted to copy rows that have a text value in column A I would do


Sub ABC()
Dim r as Range, r1 as Range
' get the next available row in sheet2 using column A
set r1 = worksheets("Sheet2").Cells(rows.count,"A").end(xlup).row + 1
' pick up the rows in sheet1 that have text in column A
set r = worksheets("Sheet1").columns(1).specialcells(xlconstants,xlTextValue)
' do the copy
r.copy r1
End sub

this works if the layout in each sheet is identical.  If it isn't (and that isn't totally clear from your description), then you would have to loop through the identified data and copy to the appropriate column - doing a row at a time and within each row, by column.

The exception is if you use the advanced filter.  the advanced filter can do this as a manual command (no macro required), but the headers would need to be the same in terms of the content of the header name (no requirement to be in the same location).  However, the advanced filter only looks at a one row header (you mentioned something about the headers being 5 rows).  The five row header would be no problem if the last row of the header can be used as a unique identifier - then the first 4 rows are ignored for purposes of the advance filter.  This is the easiest in my opinion if your data fits the "model"

--
Regards,
Tom Ogilvy  
About Excel
This topic answers questions related to Microsoft Excel spreadsheet (or workbook) stand-alone or Mircrosoft Office Excel including Excel 2003, Excel 2007, Office 2000, and Office XP. You can get Excel help on Excel formulas(or functions), Excell macros, charting in Excel, advanced features, and the general use of Excel. This does not provide a general Excel tutorial nor the basics of using a spreadsheet. It provides specific answers to using Microsoft Excel only. If you do not see your Excel question answered in this area then please ask an Excel question here

Excel

All Answers


Answers by Expert:


Ask Experts

Volunteer


Tom Ogilvy

Expertise

Selected as an Excel MVP by Microsoft since 1999. Answering Excel questions in Allexperts since its inception in 2001. Able to answer questions on almost all aspects of Excel's internal capabilities. If seeking a VBA solution, please specify that in your question itself so I give you the answer you want. [Excel has weak protection - if you are distributing an application, I don't answer questions on how to protect your project from your users.]

Experience

Extensive experience.

Education/Credentials
Master of Science (MS) degree Operations Research (ORSA)

Awards and Honors
Microsoft MVP in Excel.

©2012 About.com, a part of The New York Times Company. All rights reserved.