2012-07-19 143 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 
相關問題