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

标签 vba excel 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

结束子

最佳答案

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

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

关于VBA根据关键字将行从sheet1复制到sheet2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49764858/

相关文章:

excel - 从 Excel 文档在 Word 中自动创建表格

excel - 明天使用 VBA Application.ontime

vba - 如何根据Excel中的单元格值更改公式?

Excel公式: Lookup from multiple named ranges

excel - 将文本文件数据提取到工作表

excel - 在 Excel 中检测一列中的单元格变化

arrays - 使用未分配的变量作为条目创建 Excel VBA 数组

javascript - Excel like 列标题选项过滤算法

vba - 从 VBA 触发计算时,#GETTING_DATA 消息永远不会解析

excel - 再次动态范围 - 再次使用文本字符串