【发布时间】:2016-07-20 02:25:14
【问题描述】:
我是 Excel 宏和 VBA 的新手,面临以下问题:
(1) 我有一个大约 50,000 行和 11 列的数据集。
(2) 我需要根据某个关键字从工作表中提取行,该关键字与特定列中存在的字符串匹配。
(3) 我有来自另一个堆栈溢出问题的以下代码:
Sub testIt()
Dim r As Long, endRow as Long, pasteRowIndex As Long
endRow = 10 ' of course it's best to retrieve the last used row number via a function
pasteRowIndex = 1
For r = 1 To endRow 'Loop through sheet1 and search for your criteria
If Cells(r, Columns("B").Column).Value = "YourCriteria" Then 'Found
'Copy the current row
Rows(r).Select
Selection.Copy
'Switch to the sheet where you want to paste it & paste
Sheets("Sheet2").Select
Rows(pasteRowIndex).Select
ActiveSheet.Paste
'Next time you find a match, it will be pasted in a new row
pasteRowIndex = pasteRowIndex + 1
'Switch back to your table & continue to search for your criteria
Sheets("Sheet1").Select
End If
Next r
End Sub
(4) 当正在搜索的列的单元格将“YourCriteria”作为唯一条目时,这非常有效。
(5) 但是,在我的数据中,我的字符串中嵌入了“YourCriteria”
例如:“YourCriteria” = “ball”,特定列中的单元格包含“the dog play with the ball”、“the ball is bad”等。
如何提取包含“YourCriteria”的行?需要对代码进行哪些修改?
谢谢
【问题讨论】: