Help need VBA. copy columns to another sheet based on criteria in another column

bessieunl

New Member
Joined
May 19, 2015
Messages
11
I am new to VBA so all help is much appreciated.
in sheet1,
column AW contains values I wanna to copy, ColumnBN contains criteria like type 1234..
I wanna copy rows in column A that corresponding rows in BN meet criteria such as B =1,
in fact, I wanna to be able to copy cells in AW 4 times based on 4 different criteria type in BN. and Paste into column ABCD in sheet2.
I have developed a VBA but it doesn't seem to be working?
Code:
Sub LastRowInOneColumn()
   Dim LastRow As Long
   Dim i As Long, j As Long
   Dim h
   h = 1

   'Find the last used row in a Column: column A in this example
   With Worksheets("1")
      LastRow = .Cells(.Rows.Count, "AW").End(xlUp).Row
   End With
   MsgBox (LastRow)
   
   With Worksheets("Detail")
      LastRow2 = .Cells(.Rows.Count, "BN").End(xlUp).Row
   End With
   
   'first row number where you need to paste values in Sheet1'
   With Worksheets("2")
      j = .Cells(.Rows.Count, "A").End(xlUp).Row + 1
   End With
   
   Do While h < LastRow
    For i = 1 To LastRow2
       With Worksheets("1")
           If .Cells(i, 1).Value = "criteria1" Then
               .Rows(h).Copy Destination:=Worksheets("2").Range("A" & j)
               j = j + 1
           End If
       End With
     Next i
    Loop
End Sub

I am not sure if my logic is right. Help is much appreciated
 

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