在第一个空行和第三个空行之间选择单元格,然后粘贴到另一个工作表中

时间:2015-10-12 15:18:37

标签: excel vba select resize row

我正在尝试创建一个子程序,选择第一个空白行和第三个空白行之间的行,并将其移动(同时将其从原始工作表中删除)到标记为“保留或取消”的工作表中

我有一个空行功能,它基本上会查找E列中的第一个空白单元格,然后选择该行。我试过用Selection.Resize玩箭头,但似乎无法让它工作。

Dim i1 As Long
Dim i2 As Long
Dim i3 As Long
i1 = ActiveSheet.Range("E1").End(xlDown).Offset(1).EntireRow.Select
i2 = .Range("E" & Rows.Count).End(xlUp).Row + 3
i3 = i2 - i1

ActiveSheet.Range("E1").End(xlDown).Offset(1).EntireRow.Select
Selection.Resize (i3)

我不知道如何将选择移到另一张纸上。

1 个答案:

答案 0 :(得分:0)

以下是两种在表格之间移动行的方法

假设您要在E列中移动带有空单元格的行,来自" Sheet1"到" Sheet2"

MoveRows.png

当时移动一行

import pandas as pd
from collections import OrderedDict

entity_dict = OrderedDict()
entity_dict['bam'] = 1.0
entity_dict['ham'] = 1.0
entity_dict['jam'] = 0.82390874094431876
entity_dict['kam'] = 1.0
entity_dict['lam'] = 1.0
entity_dict['mam'] = 0.82390874094431876
entity_dict['pam'] = 1.0
entity_dict['ram'] = 1.0
entity_dict['sam'] = 0.82390874094431876
entity_dict['tam'] = 1.0

entity_df = pd.DataFrame.from_dict(entity_dict, orient='index').T 

print(entity_df)

一次所有行

Public Sub moveRows1()
    Dim ws1 As Worksheet, ws2 As Worksheet, xRow1 As Long, xRow2 As Long

    Set ws1 = Worksheets("Sheet1")
    Set ws2 = Worksheets("Sheet2")

    xRow1 = ws1.Range("E1").End(xlDown).Offset(1).Row       'find 1st blank cell on Sheet1
    xRow2 = ws2.UsedRange.Row + ws2.UsedRange.Rows.Count    'find 1st blank cell on Sheet2

    ws1.Rows(xRow1).Cut                     'cut entire row from Sheet1
    ws2.Rows(xRow2).Insert Shift:=xlDown    'paste it on Sheet2
    ws1.Rows(xRow1).Delete Shift:=xlUp      'remove empty row from Sheet1
    'ws1.Rows(xRow1).EntireRow.Delete       'or delete entire row
End Sub