Solved

EXcel VBA

Posted on 2012-03-13
7
344 Views
Last Modified: 2012-03-13
I have an Excel workbook with two sheets (Master & Subset). I need to copy rows from the Master sheet to the Subset sheet which have a date in a particular field (Column O). The data starts in row 9 and extends to row 1980.

I have tried several methods and have not met with success. Finally I implemented the code below with the intent to copy all the rows from Master to Subset and then filter in Subset for the column above. The effect would be that only the rows with dates in column O would be displayed in the Subset sheet.

My Problem is the filter is excluding the first row in Subset. So i am getting an extra row in my result which most times should have been filtered out.


Sub CopyRange()

Sheets("Master").Select
    Range("A9:P1980").Select
    Selection.Copy

    Sheets("Subset").Select
    Range("A9").Select
    ActiveSheet.Paste
    Sheets("subset").Range("A9:P1980").AutoFilter Field:=15, Criteria1:="<>"

End Sub

How can I make this work? Is there another way to achieve my aim?
0
Comment
Question by:Markwedd
  • 4
  • 3
7 Comments
 
LVL 10

Expert Comment

by:SANTABABY
Comment Utility
Not sure why you don't want apply the filter on the entire column(in subset sheet)? any partucular reason?
Would you prefer the code to copy only the qualified rows to subset sheet?

Is it possible to atach a sample, hopefully that's answer my questions.

Thanx,
SB
0
 

Author Comment

by:Markwedd
Comment Utility
I only applied the filter on the partial column to avoid getting portions of the header in the mix.

I am open to any solution which allows me to achieve my aim.

I could only attach a workbook without data.
0
 
LVL 10

Expert Comment

by:SANTABABY
Comment Utility
Do you need row 1-8 in the subset?
I realize that you have sensitive data in your spreadsheet. Could you please attach a workbook with made up data(just enough to describe the problem)?
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Author Comment

by:Markwedd
Comment Utility
ROw 1-8 is the header. Please see attached workbook.
TTLC-Master.xls
0
 
LVL 10

Accepted Solution

by:
SANTABABY earned 500 total points
Comment Utility
Please try the following CopyRange routine(replace you current code), which will keep your header rows (1-8) intact and filter the subsequent rows. Please let me know if this not what you wanted.

Sub CopyRange()

Sheets("Master").Select
    Range("A9:P1980").Select
    Selection.Copy

    Sheets("Subset").Select
    Sheets("Subset").AutoFilterMode = False
    Range("A9").Select
    ActiveSheet.Paste
    'Sheets("subset").Range("A1:P1980").AutoFilter Field:=15, Criteria1:="<>"
    Sheets("subset").Range("A8:P1980").AutoFilter Field:=15, Criteria1:="<>"

End Sub
0
 

Author Closing Comment

by:Markwedd
Comment Utility
Thank you SANTABABY! That worked.

Could you tell me what I would have to do to delete the fields with blank instead of just removing them from view.
0
 
LVL 10

Expert Comment

by:SANTABABY
Comment Utility
Try the following please:

Sub CopyRange()

Sheets("Master").Select
    Range("A9:P1980").Select
    Selection.Copy

    Sheets("Subset").Select
    Sheets("Subset").AutoFilterMode = False
    Range("A10").Select
    ActiveSheet.Paste
    'Sheets("subset").Range("A1:P1980").AutoFilter Field:=15, Criteria1:="<>"
    Range("A9").Clear
    Sheets("subset").Range("A9:P1980").Select
    Selection.AutoFilter Field:=15, Criteria1:="="
    Selection.SpecialCells(xlCellTypeVisible).Select
    Selection.Copy
    Selection.EntireRow.Delete
    Sheets("Subset").AutoFilterMode = False


End Sub
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

A little background as to how I came to I design this code: Around 5 years ago I designed an add-in that formatted Excel files to a corporate standard, applying different cell colours and font type depending on whether the cells contained inputs,…
This collection of functions covers all the normal rounding methods of just about any numeric value.
Graphs within dashboards are meant to be dynamic, representing data from a period of time that will change each time the dashboard is updated with new data. Rather than update each graph to point to a different set within a static set of data, t…
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

762 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

12 Experts available now in Live!

Get 1:1 Help Now