0

早上好...

我正在尝试在 excel 2010 中执行以下操作:

在 Sheet1, col A 中搜索“test a”或“test b”等。如果找到“test a”,则在 Sheet 2, col A 中搜索所有出现的“test a”并复制 sheet 2 col B-> P 为工作表 1 Col B 中的每一行,从工作表 1 中的初始“测试 a”开始,对于工作表 2 中的每一行向下递增。

表 1:

可乐 :

  • 测试一个
  • xxxxxx
  • xxxxx
  • xxxx

  • 测试b

  • xxxxxx
  • xxxxx
  • xxxx

  • 测试c

  • xxxxxx
  • xxxxx
  • xxxx

表 2:

A、B、C、D 栏 -> P 栏

  • 测试 a , 1, 2, 3, 4 ....
  • 测试 a , 5, 6, 7, 8 ....
  • 测试 a , a, b, c, d ....
  • 测试 a , e, f, g, h ....

  • 测试 b , 1, 2, 3, 4 ....

  • 测试 b , 5, 6, 7, 8 ....
  • 测试 b , a, b, c, d ....
  • 测试 b , e, f, g, h ....

ETC

期望的结果:

  • 测试 a , 1, 2, 3, 4 ...
  • xxxxxx , 5, 6, 7, 8 ....
  • xxxxx , a, b, c, d ....
  • xxxx , e, f, g, h ....

从来没有做过任何 excel/vb 编码,我什至很难开始!

我能做的最好的就是高级代码:

For search criteria 'test a|test b ..'
 if sheet 1, col A equal to 'criteria' (save row where found)
   if sheet2, col A equal to 'criteria'
     copy sheet2, col b->col p, row (where 'criteria' found) > sheet1, Col B, row (where criteria found in sheet1), incrementing row downwards as we go.

非常感谢有关如何完成此操作的一些指导!

非常感谢

4

1 回答 1

0

这是一个粗略的草图,可以帮助您入门。使用嵌套循环结构,因此效率不是很高,但提供了基本功能,应该允许您自定义以更好地适应您的数据。

Sub SearchCriteria()

Dim ws1 As Worksheet
Set ws1 = Sheet1

Dim ws2 As Worksheet
Set ws2 = Sheet2

Dim ws1RowCounter As Integer
ws1RowCounter = 2

Dim ws2RowCounter As Integer
ws2RowCounter = 2

Dim innerCounter As Integer
innerCounter = 0

Dim ws1Select As String
Dim ws2Select As String

Dim copyRange As Range

Do While ws1.Cells(ws1RowCounter, 1) <> ""

    'look for this value in ws2
    ws1Select = ws1.Cells(ws1RowCounter, 1)

    'loop through ws2 range, look for matches
    Do While ws2.Cells(ws2RowCounter, 1) <> ""
        ws2Select = ws2.Cells(ws2RowCounter, 1)

        If InStr(ws1Select, ws2Select) > 0 Then
            'copy range if match found
            Set copyRange = Range(ws2.Cells(ws2RowCounter, 2), ws2.Cells(ws2RowCounter, 16))
            copyRange.copy
            Set copyRange = Range(ws1.Cells(ws1RowCounter + innerCounter, 2), _
                ws1.Cells(ws1RowCounter + innerCounter, 16))
            copyRange.PasteSpecial xlPasteAll
            innerCounter = innerCounter + 1
        End If

        ws2RowCounter = ws2RowCounter + 1

    Loop

    ws1RowCounter = ws1RowCounter + 1
    ws2RowCounter = 1
    innerCounter = 0
Loop

End Sub
于 2013-07-30T19:27:38.827 回答