首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Excel 2010 :基于标准的工作表2> sheet1单元格的复制块

Excel 2010 :基于标准的工作表2> sheet1单元格的复制块
EN

Stack Overflow用户
提问于 2013-07-30 09:57:39
回答 1查看 1.1K关注 0票数 0

早上好..。

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

搜索“测试a”或“测试b”等Sheet1,如果找到“测试a”,则搜索工作表2,以A搜索“测试a”的所有出现,并将每一行的复制表2 ->P从工作表1中找到的初始“测试a”开始,到第2页中的每一行向下递增。

表1:

A组:

  • 试验a
  • xxxxxx
  • xxxxx
  • xxxx
  • 试验b
  • xxxxxx
  • xxxxx
  • xxxx
  • 试验c
  • xxxxxx
  • xxxxx
  • xxxx

表2:

Col A,B,C,D,-> Col 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.

预期结果:

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

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

我所能做的就是编写一个高级代码:

代码语言:javascript
复制
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.

非常感谢关于如何完成这一点的一些指导!

非常感谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-30 19:27:38

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

代码语言:javascript
复制
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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17943895

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档