MS VB从1个工作表复制到另一个带有空单元格的工作表

时间:2019-01-18 15:47:23

标签: excel vba

我的excel工作表有一个MS Visual Basic宏,我有一个子工作簿和一个父工作簿。我想将单元格从子工作表“帐户”复制到父工作表“帐户”。子工作表中的单元格有一些空白单元格,当前使用此代码,它停在空白单元格,我希望它错过空白单元格并转到带有值的下一个单元格,然后继续复制。

    Sub Button1_Click()
    'Field Name
    Windows("childsheet.xlsm").Activate
    Range("A3").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Windows("parentsheet.xlsm").Activate
    Range("A3").Select
    ActiveSheet.Paste

    'API Name
    Windows("childsheet.xlsm").Activate
    Range("B3").Select
    Range(Selection, Selection.End(xlDown)).Select
    Selection.Copy
    Windows("parentsheet.xlsm").Activate
    Range("B3").Select
    ActiveSheet.Paste

'Type
Windows("childsheet.xlsm").Activate
Range("C3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows("parentsheet.xlsm").Activate
Range("C3").Select
ActiveSheet.Paste

'Length
Windows("childsheet.xlsm").Activate
Range("D3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows("parentsheet.xlsm").Activate
Range("D3").Select
ActiveSheet.Paste

'Required
Windows("childsheet.xlsm").Activate
Range("E3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows("parentsheet.xlsm").Activate
Range("E3").Select
ActiveSheet.Paste

'Read Only?
Windows("childsheet.xlsm").Activate
Range("F3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows("parentsheet.xlsm").Activate
    Range("F3").Select
    ActiveSheet.Paste
    End Sub

有效。它复制我指定的每个列,但是当它到达具有空单元格的列时,它将复制任何信息。在该单元格中从上到下,但如果遇到空白,它将在此处停止,然后移至下一列。我要它复制所有信息。

1 个答案:

答案 0 :(得分:0)

代替

(gdb) pi open("output_data.log","w").write(gdb.execute("print myarray@100000",to_string=True))

使用类似

Windows("childsheet.xlsm").Activate
Range("A3").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Windows("parentsheet.xlsm").Activate
Range("A3").Select
ActiveSheet.Paste

进一步的改进是将With Workbooks("childsheet.xlsm").ActiveSheet .Range("A3", .Cells(.Rows.Count, "A").End(xlUp)).Copy Destination:=Workbooks("parentsheet.xlsm").ActiveSheet.Range("A3") .Range("B3", .Cells(.Rows.Count, "B").End(xlUp)).Copy Destination:=Workbooks("parentsheet.xlsm").ActiveSheet.Range("B3") '… and so on … End With 替换为工作表名称,例如.ActiveSheet,以便您的代码更加可靠。