[英]Excel VBA code to select cells that contain a certain text string in one workbook, and then copy and paste these cells into a new workbook
[英]Excel VBA to select cells that contain a text string, and then copy and paste these cells into a new workbook
我是一个狂热的 excel 用户,但不擅长 vba。 任何帮助表示赞赏。 以下是我正在尝试执行的步骤。
在 B 列中查找文本字符串。 - 使用偏移量收集位于此字符串周围的两个值。 offset(4,0) 和 offset(3,10) - 总共执行四次,得到 8 个值。 - 将 8 个值粘贴到另一个工作簿中最后一行的 8 个连续单元格中
Set wkb = Excel.Workbooks.Open("c:\users\jojames\desktop\skillset performance with talktime.xls")
Set wks = wkb.Worksheets("Sheet1"): wks.Activate
find1Allscripts = "Allscripts - 10055"
Set Match1 = wks.Cells.Find(find1Allscripts)
findoffset1a = Match1.Offset(4, 0).Value
findoffset1b = Match1.Offset(3, 10).Value
find2Tier1 = "Tier1_ServiceDesk - 10052"
Set Match2 = wks.Cells.Find(find2Tier1)
findoffset2a = Match2.Offset(4, 0).Value
findoffset2b = Match2.Offset(3, 10).Value
find3Tier2 = "Tier2_ServiceDesk - 10053"
Set Match3 = wks.Cells.Find(find3Tier2)
findoffset3a = Match3.Offset(4, 0).Value
findoffset3b = Match3.Offset(3, 10).Value
find4Office = "Allscripts - 10055"
Set Match4 = wks.Cells.Find(find4Office)
findoffset4a = Match4.Offset(4, 0).Value
findoffset4b = Match4.Offset(3, 10).Value
'Paste the values'
Set wkb2 = ThisWorkbook
Set wks2 = wkb2.Sheets("ACD Data")
wks2.Activate
LastRow = wks2.Range("Y" & wks2.Rows.Count).End(xlUp).Row + 1
ActiveSheet.Range("Y" & LastRow).PasteSpecial xlPasteFormulas
Set wks = Nothing: Set wkb = Nothing
Set wks2 = Nothing: Set wkb2 = Nothing
我认为这是一个不错的开始。 我会将我正在搜索的内容存储在一个数组中并循环遍历它
myArray = Array("Allscripts -1005", "Tier1_ServiceDesk - 10052", ...)
for i = lbound(myArray) to ubound(myArray)
Set Match1 = wks.Cells.Find(myArray(i))
if not Match1 is Nothing then
LastRow = wks2.Range("Y" & wks2.Rows.Count).End(xlUp).Row + 1
wks2.Range("Y" & LastRow) = Match1.Offset(4,0).Value
wks2.Range("Z" & LastRow) = Match1.Offset(3,10).Value
end if
next i
顺便说一下,不需要选择或激活任何东西。 就像我一样把它称为一个对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.