Public Schedule Face-to-Face & Online Instructor-Led Training - View dates & book

vba

ResolvedVersion 2003

Steven has attended:
Excel VBA Intro Intermediate course

VBA

How can I write a macro that uses filters to pulls out all rows that begin with a couple of letters in a certain column and then copy it across to a new worksheet?

RE: VBA

Hi Steven

To copy data across to a new sheet based on the first couple of letters in a field you need to use the LEFT function. The code below helps to do this.

Sub TextTrim()


Dim intNumRows As Integer 'Holds the number of rows in the table
Dim intRowCount As Integer 'Indicated which row is being copied
Dim strMyText As String 'Holds the cutdown search string
Dim intTargetRow As Integer 'indicates the target row on the new worksheet

intTargetRow = 1 'Default the target row to 1

intNumRows = Sheets("Main Data").Range("A1").CurrentRegion.Rows.Count

For intRowCount = 1 To intNumRows

strMyText = Left(Sheets("Main Data").Cells(intRowCount, 1), 3)

If strMyText = "ASD" Then

Sheets("ASD Data").Cells(intTargetRow, 1).Value = Sheets("Main Data").Cells(intRowCount, 1).Value
'This only shows the copying of one cell. You need to use a counter to check the number of
'columns to be able to copy more cells (As in the course exercises

intTargetRow = intTargetRow + 1

End If

Next intRowCount

End Sub

I have also attached a workbook where I tested this code.

Hope this helps

Carlos

Attached files...

Filter Partial Text.xls

Mon 29 Jun 2009: Automatically marked as resolved.

 

Training courses

Training information:

Welcome. Please choose your application (eg. Excel) and then post your question.

Our Microsoft Qualified trainers will then respond within 24 hours (working days).

Frequently Asked Questions
What does 'Resolved' mean?

Any suggestions, questions or comments? Please post in the Improve the forum thread.

Excel tip:

Outlining - ungrouping rows or columns

Highlight want you want to ungroup and press ALT + SHIFT + right cursor arrow

View all Excel hints and tips

Connect with us:

0207 987 3777

Call for assistance

Request Callback

We will call you back

Server loaded in 0.13 secs.