VBA根据关键字从sheet1复制到sheet2

时间:2018-04-11 00:57:48

标签: excel vba excel-vba excel-formula

我的代码完成了我想要的操作,但它将其复制到工作表2中的A列。如果可能的话,我希望将数据放在B列的开头。

Sub EFP()
Dim keyword As String: keyword = Sheets("Results").Range("B3").Value
Dim countRows1 As Long, countRows2 As Long
countRows1 = 3 'the first row of my dataset in the Data tab
endRows1 = 500 'the last row of my dataset in the Data tab
countRows2 = 6 'the first row where I want to start writing the found rows
For j = countRows1 To endRows1
    If Sheets("Data").Range("B" & j).Value = keyword Then
        Sheets("Results").Rows(countRows2).Value = Sheets("Data").Rows(j).Value
        countRows2 = countRows2 + 1
    End If
Next j

End Sub

1 个答案:

答案 0 :(得分:2)

如果您复制并粘贴整行,它们将始终从A列开始。如果您希望结果从B列开始,则需要采用不同的方法,例如

Sheets("Results").Range("B" & countRows2 & ":Z" & countRows2).Value = Sheets("Data").Range("A" & j & ":Y" & j).Value