VBA Excel:search表单中的部分string,然后将行和粘贴到另一个表

我是VBA新手,需要编写代码来执行特定function。

我需要能够search列“ B ”的部分string,并将行输出到另一个表。

例如,我需要将所有具有“ MOTOR ”字样的行作为“ B ”列中string的一部分,并将这些行粘贴到“ sheet 2 ”中。“ B ”列中的sting可以有多种string作为“ TEFCMOTOR286B2 ”或“ MOTORSLIDEBASE286B2 ”,我需要复制两行与以前的string,并将其列入“ 表2 ”列需要被复制的行数有所不同,有时不会是任何与部分string“ MOTOR ”。

让我知道如果你需要更多的信息。

这对macroslogging器来说是一个很好的工作

  1. 打开logging器
  2. 放置一个AutoFilter的原始数据
  3. 使用AutoFilterselect文本filter>包含…
  4. 复制可见的行并粘贴到另一个表
  5. closureslogging器

如果从Sheet1复制到Sheet2,则可以尝试此操作。

 Option Explicit Public Sub TransferMotor() Dim lngLastRow As Long Dim lngRow As Long Dim strValue As String Dim lngRowOutput As Long ' where does the data end in Sheet1 lngLastRow = Sheet1.UsedRange.Rows.Count If lngLastRow = 1 Then Exit Sub ' no data ' Clear down sheet2, assume we have column headings in row 1 we want to keep Sheet2.Range("2:1048576").Clear lngRowOutput = 2 ' where are we going to write the values to in Sheet2 when we find a "MOTOR" phrase For lngRow = 2 To lngLastRow strValue = Sheet1.Cells(lngRow, 2).Value ' get value from column B If InStr(1, strValue, "MOTOR", vbTextCompare) > 0 Then ' can we find "MOTOR" in the text Sheet1.Rows(lngRow).Copy Sheet2.Rows(lngRowOutput).PasteSpecial lngRowOutput = lngRowOutput + 1 End If Next lngRow End Sub