简体   繁体   English

使用宏在Excel中将行从一个工作表复制到另一个工作表

[英]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.

相关问题 使用宏从Excel工作表复制数据到Excel工作表 - Copying data from/to excel worksheet using a macro Excel宏:根据条件将行值从一个工作表复制到另一个工作表中的特定位置 - Excel Macro: Copying row values from one worksheet to a specific place in another worksheet, based on criteria 将整个工作表从一个Excel复制到另一个 - Copying Entire Worksheet from one Excel to another 将特定范围的 Excel 单元格从一个工作表复制到另一个工作表 - copying of specific range of excel cells from one worksheet to another worksheet Excel VBA-将特定列从一个工作表复制到另一工作表 - Excel VBA - Copying specific columns from one worksheet to another worksheet Excel宏可将数据从一个工作表复制并粘贴到另一工作表 - Excel macro to copy and paste data from one worksheet to another worksheet 使用macro / vba将多个行从一个工作表复制到另一工作表 - Copy multiple rows from one worksheet to another worksheet using macro/vba 使用VBA将一个Excel工作表的格式复制到另一个Excel工作表 - Copying Format of one excel sheet to another excel worksheet using VBA 使用Excel VBA将行从一个列表对象复制到另一个 - Copying rows from one listobject to another using Excel VBA 将非空白行从一个工作表复制到另一个工作表 - Copying non blank rows from a range from one worksheet to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM