Copy to another sheet in For-loop with Criteria

forrestli

New Member
Joined
Feb 8, 2022
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hi. I am trying to copy cells to another columns based on the row number of selection.
For example, first I input row 16, then the Notation is "A". Second, above row 16, all the rows with "A" will be selected. Third, these rows' value will be copied to another sheet.
I prefer For-if -loop, because i want to past them one by one from the Row selected, from the bottom to the top.

1644601047467.png

VBA Code:
Sub CopySelected()

Dim A As Long: A = 4
Dim B As Long: B = 2
Dim RowNo, Notation

With ActiveSheet
    RowNo = Application.InputBox("Select row to be input", Type:=1)

    If VarType(RowNo) = vbBoolean Then
       Exit Sub
    Else
       Notation = .Cells(RowNo, 6).Value
    End If

    For A = RowNo To 4
        If .Cells(A, 1).Value = Notation Then
         .Cells(A, 2).Value.Copy Sheet2.Cells(B, 1)
         B = B + 1
        End If
    Next A
End With

End Sub
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Hi, a VBA demonstration for starters :​
VBA Code:
Sub Demo1()
    Dim L&, R&, T$
        Application.ScreenUpdating = False
    With Sheet2
            L = .UsedRange.Rows.Count
            R = ActiveCell.Row
            T = Cells(R, 1).Text
        For R = R To 2 Step -1
            If Cells(R, 1).Text = T Then
                L = L + 1
                Cells(R, 1).Copy .Cells(L, 1)
            End If
        Next
    End With
        Application.ScreenUpdating = True
End Sub
 
Upvote 0
Solution
Mr. Marc

You are my lifesaver! My visa is almost end and my boss is using this small task to challenge my probation. Thank you so much from Canada.

VBA Code:
Sub Demo1()
    Dim RowNo
    RowNo = Application.InputBox("Select row to be input", Type:=1)
    Dim A, RowMix, B
        Application.ScreenUpdating = False
    With ActiveSheet
            
            RowMix = .Cells(RowNo, 6).Text
            A = RowNo
            B = 2
        
        For A = RowNo To 2 Step -1
            If .Cells(A, 6).Text = RowMix Then
                .Cells(A, 19).Copy Sheets("DataEntry").Cells(B, 8)
                B = B + 1
            End If
        Next
    End With
        Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,429
Messages
6,119,433
Members
448,897
Latest member
ksjohnson1970

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