VBA Copy certain data to specific columns

Yugo101

New Member
Joined
Jun 12, 2017
Messages
27
Currently on my workbook, i have a VBA set up so i press a button opens up file explorer i click an excel file and copies all the data, but next step i want to do is only copy rows where one column has a certain text.
CodeTypeNameDate TimeRubbishCommentsRubbish
123314ABJohn01/01/0112134568asdasdasda a a a a aetc
411354CDJulie
01/01/01
12134568
asdasdasd a aa a etc
1464988ABJack
01/01/01
12134568
asdasdasdbbbbbetc

Above is the sort of lay out of the sheet i would select when doing file explorer. So Ideal is only copy rows where type AB is present and Copy Code (A) to A on new sheet, Type (B) to B, but Name(c) to E, Date (D) to F, Time (f) to G and Comments (G) to I. Best example since not at work. So basically need to copy rows where only AB is present then Copy the data required to Certain columns.
 

Excel Facts

Quick Sum
Select a range of cells. The total appears in bottom right of Excel screen. Right-click total to add Max, Min, Count, Average.
Hi
Try this as a sample VBA to copy for the data you showed form sheet1 to sheet2 as a start
VBA Code:
Sub test()
    Dim a
    Dim i&, k&
    a = Sheets("sheet1").Cells(1).CurrentRegion
    With Sheets("sheet2")
        For i = 2 To UBound(a)
            If a(i, 2) = "AB" Then
                .Cells(k + 1, 1).Resize(, 9) = Application.IfError(Application.Index(a, i, Array(1, 2, "", "", 3, 4, 5, "", 7)), "")
                k = k + 1
            End If
        Next
    End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,076
Messages
6,122,983
Members
449,092
Latest member
Mr Hughes

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top