将数据复制到新的工作表中,最后四行除外

时间:2019-01-02 15:48:05

标签: excel vba

我正在尝试从工作表1(由区域D21:O21定义)中复制一个表,直到最后–(减去)最后4行。

我粘贴结果有问题。在工作表2中,我有一张表格,用于填充数据透视图。在删除以前填充的行(带有值)并粘贴新的行(新值)之后,表的扩展范围超出了预期。它将下游添加空白单元格,就好像复制的工作表1中有更多行带有值。

例如:假设我的表(在工作表1中)有600行带有值。如果我将其粘贴到表格(第2页)中,它会延伸超过600行(大约10000行)。而不是添加多个空行,我想要的是600,除了最后4行=从顶部到596行。

Sub Prime()

Dim Last_Row1 As Long, Last_Row2 As Long
Dim ws1 As Worksheet, ws2 As Worksheet
    Set ws1 = Sheets("Enter DATA here")
    Set ws2 = Sheets("DATA")
    Application.ScreenUpdating = False
    Last_Row1 = ws1.Range("C" & Rows.Count).End(xlUp).Row ' Determine the lastrow of the data to copy
    Last_Row2 = ws2.Range("A" & Rows.Count).End(xlUp).Row ' Determine the next empty row in order to paste the data
    ws1.Range("D21:O21" & Last_Row1).Copy ws2.Range("A" & Last_Row2)
Application.ScreenUpdating = True

End Sub

3 个答案:

答案 0 :(得分:0)

尝试一下:

Sub Prime()
    Dim Last_Row1 As Long, Last_Row2 As Long, table As Range
    Dim ws1 As Worksheet, ws2 As Worksheet

    Set ws1 = Sheets("Enter DATA here")
    Set ws2 = Sheets("DATA")

    Set table = ws1.Range("D21:O28")   // I arbitrarily set this to 8 rows

    Application.ScreenUpdating = False

        Last_Row1 = ws1.Range("C" & Rows.Count).End(xlUp).Row ' Determine the lastrow of the data to copy
        Last_Row2 = ws2.Range("A" & Rows.Count).End(xlUp).Row ' Determine the next empty row in order to paste the data
        table.Resize(table.Rows.Count - 4, table.Columns.Count).Copy ws2.Range("A" & Last_Row2)

    Application.ScreenUpdating = True
End Sub

答案 1 :(得分:0)

<html>
<head>
<title>HTML table</title>
</head>
<body>
 <table border="5" align="center" bgcolor="pink" width="30%">
            <Thead>
                <Tr bgcolor="yellow">
                    <Th>ID</Th>
                    <Th>Name</Th>
                    <Th>Branch</Th>
                    <Th colspan="2">Skillset</Th>
                    
                </Tr>
            </Thead>
        </body>
            <Tbody>
                <Tr>
                    <Td>201</Td>
                    <Td>Sam</Td>
                    <Td rowspan="2">CSE</Td>
                    <Td>CSS</Td>
                    <Td>html</Td>
                </Tr>
    
                    <Tr>
                        <Td>202</Td>
                        <Td>Jack</Td>
                        <Td>Java</Td>
                        <Td>J2EE</Td>
                    </Tr>
                
                
                        <Tr>
                            <Td>203</Td>
                            <Td>Harsh</Td>
                            <Td>Mech</Td>
                            <Td>TDrilling</Td>
                            <Td>Java</Td>
                        </Tr>
                    
                    
                            <Tr>
                                <Td>204</Td>
                                <Td>Ryan</Td>
                                <Td>ECE</Td>
                                <Td>Java</Td>
                                <Td>Python</Td>
                            </Tr>
                        </Tbody>
        </table>
        </html>
    

答案 2 :(得分:0)

您可以使用“偏移”功能排除最后4行。

Last_Row1 = ws1.Range("C" & Rows.Count).End(xlUp).offset(-4,0).Row 

复制范围时,应删除列“ O”后面的行指示符“ 21”,否则,直到最后一行才选择代码。如下所示:

ws1.Range("D21:O" & Last_Row1).Copy ws2.Range("A" & Last_Row2)