2012-07-19 168 views
0

我有一个excel工作表,其中包含大量行和几列。第一列包含制造商名称,第二列包含所有产品的产品代码,第三列包含说明等。 我想要做的是复制与特定产品代码对应的行。例如:使用宏将工作表中的行从一个工作表复制到另一个工作表中使用

**Manufacturer  Product code  Description** 
abc     B010    blah blah 
dgh     A012     
hgy     X010     
eut     B013     
uru     B014     
eut     B015    
asd     G012    
sof     B016 
uet     B016 
etc 

有没有办法复制产品代码在B010 - B016之间的行?也可能有双重/匹配的产品代码,也可以复制它们。

有意义吗?

对不起,我还没有vba代码放在这里呢。

在此先感谢。

+0

将其复制到具有完全相同单元格引用的新工作表中?或者产品'abc',代码'B010'是第一行,产品'eut',代码'B013'是第二行等? – LittleBobbyTables 2012-07-19 19:47:41

+0

很确定你可以使用excel的[高级过滤器](http://office.microsoft.com/en-us/excel-help/filter-by-using-advanced-criteria-HP005200178.aspx)来实现这个*([ (http://msdn.microsoft.com/en-us/library/aa221800(v = office.11​​).aspx))* *([vba示例](http://social.msdn.microsoft。 com/Forums/zh-CN/exceldev/thread/1a53a4af-5877-44a3-8654-b32fb7e34e03 /))* – 2012-07-19 19:48:06

+0

@LittleBobbyTables - 是的,它可以在新工作表的新行上开始。这并不重要。谢谢。 – duper 2012-07-19 20:00:22

回答

0

这应该是诀窍;它将A:C范围单元格复制到B010和B016之间的任何B单元格值到Sheet2中的下一个可用行。

Private Sub CopyRows() 
    Dim lastrow As Long 
    Dim r1 As Long, r2 As Long 

    ' Get the last row in the worksheet 
    lastrow = ActiveSheet.Cells.SpecialCells(xlCellTypeLastCell).Row 

    r2 = 1 

    For r1 = 1 To lastrow 
     ' If the last three characters of the B cell are numeric... 
     If IsNumeric(Right(Sheet1.Range("$B$" & r1).Value, 3)) Then 
      ' If the first character of the B cell is "B", and the last three 
      ' characters are between 10 and 16 ... 
      If Left(Sheet1.Range("$B$" & r1).Value, 1) = "B" And _ 
       CLng(Right(Sheet1.Range("$B$" & r1).Value, 3)) >= 10 And _ 
       CLng(Right(Sheet1.Range("$B$" & r1).Value, 3)) <= 16 Then 

       ' ... copy the A-C range for the row to the next available row 
       ' in Sheet2 
       Sheet2.Range("$A$" & r2, "$C$" & r2).Value = _ 
        Sheet1.Range("$A$" & r1, "$C$" & r1).Value 

       r2 = r2 + 1 

      End If 
     End If 
    Next 
End Sub 
相关问题