简体   繁体   中英

excel VBA copy paste only column A of the row?

I have this formula to generate result at sheet4.

however, I only want matched criteria from sheet3's column A to be copy over to sheet4, not the entire row.

in this case, i only want 36238 and 63545 to show at sheet4 as output

Been trying couple modification and can't work, so just paste this original code here see if anyone can pin point the modification.

here's my full code:

Private Sub CommandButton1_Click()

    FilterCriteria1

End Sub

Sub FilterCriteria1()

Dim LSearchRow As Long
Dim shtSearch As Worksheet
Dim shtCopyTo As Worksheet
Dim rw As Range

    LSearchRow = 2 'Start search in row 2

    Set shtSearch = Sheets("Sheet3")
    Set shtCopyTo = Sheets("Sheet4")

On Error GoTo Err_Execute

    Do While Len(shtSearch.Cells(LSearchRow, 1).Value) > 0

        Set rw = shtSearch.Rows(LSearchRow)

        If rw.Cells(4).Value = 0 And rw.Cells(7).Value <= -0.4 Then

            MsgBox "bingo: " & rw.Cells(4).Value & "_" & rw.Cells(7).Value

            rw.Copy shtCopyTo.Cells(Rows.count, 1).End(xlUp).Offset(1, 0)

        End If
        LSearchRow = LSearchRow + 1
    Loop

Err_Execute:
    If Err.Number = 0 Then MsgBox "All have been copied!" Else _
    MsgBox Err.Description


End Sub

sheet3和sheet4输出

It looks to be your line:

rw.Copy shtCopyTo.Cells(Rows.count, 1).End(xlUp).Offset(1, 0)

I imagine you probably want:

Cells(rw, 1).copy shtCopyTo.Cells(Rows.count, 1).End(xlUp).Offset(1, 0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM