vba
The UK's Number 1 for Microsoft Office Training Add this page to your favourites/bookmarksBookmark page
 
View printable version of pagePrintable version
Plus One Google
Customer: Sign in
Delegate: Sign in
Trainer: Log in

Forum home » Delegate support and help forum » Microsoft Excel VBA Training and help » VBA

VBA

resolvedResolved · Low Priority · Version 2003

replyReply Thu 18 Jun 2009, 16:52Delegate Steven said...

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?

For upcoming training course dates see: Pricing & availability

replyReply Mon 22 Jun 2009, 11:03Trainer Carlos said...

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.

 

Please browse our web site to find out more about
excel classes and other Microsoft training courses.

Excel tip:

Paste with Live Preview in Excel 2010

Did you know you can preview what you are about to paste? Here's how to do it.

Copy what you are about to paste
Position the cursor where you want to paste
Right click to display the shortcut menu
Place the cursor over the Paste Options and this will give you a preview of the what you are about to paste.

View all Excel hints and tips


Microsoft Certified Partner Accredited Training Provider: Institute of IT Training Institute of Leadership and Management - Certified Courses Security Seal verified by visa, mastercard securecard