[英]copying rows from one worksheet to another in excel using macro
I have an excel worksheet with whole bunch of rows and several columns in it. 我有一个Excel工作表,其中有一整行和几列。 The 1st column contains the manufacturer's name, the 2nd column contains the product codes for all the products, the 3rd column contains the description and etc. What I want to do is to copy the rows that corresponds to certain product codes. 第一列包含制造商的名称,第二列包含所有产品的产品代码,第三列包含描述等。我要复制对应于某些产品代码的行。 For example: 例如:
**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
Is there a way to copy the rows that has the product codes in between B010 - B016? 有没有办法复制产品代码介于B010-B016之间的行? There might be double/matching product codes too, and it is totally fine to copy them too. 可能还会有双重/匹配的产品代码,也可以复制它们。
Makes sense? 说得通?
Sorry, i have no vba code to put in here yet. 抱歉,我还没有VBA代码可放入这里。
Thanks in advance. 提前致谢。
This should do the trick; 这应该可以解决问题; it copies the A:C range cells for any B cell values that are between B010 and B016 to the next available row in Sheet2. 它将B010和B016之间的任何B单元格值的A:C范围单元格复制到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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.