vba

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

VBA

resolvedResolved · Low Priority · Version 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:

Quickly insert a function

In Excel 97 and 2000 it was known as the Paste Function dialog box, these days it's known as the Insert Function dialog box. Regardless, one has to choose Insert|Function. or the fx button to open it up. There is, however, a non-mousey way to get hold of the Insert Function dialog box: press Shift+F3 in a blank cell to open the Insert Function dialog.

Press Shift+F3 after a function name and open bracket to open the Function Arguments dialog. For example, type =VLOOKUP( into a cell and press Shift+F3 to obtain a detailed description of VLOOKUP's arguments.

View all Excel hints and tips


Server loaded in 0.05 secs.