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