简体   繁体   English

如何使用VBA将列从一个Excel文件复制到另一个

[英]how to copy columns from one excel file to another using vba

I am very new to Excel and vba. 我对Excel和vba非常陌生。

I have an excel file that contains a button that does the following hen clicked: 我有一个excel文件,其中包含执行以下操作的按钮:

it opens a stored file, copy columns A data and then create a new excel file and paste the copied data on it 它打开一个存储的文件,复制A列的数据,然后创建一个新的Excel文件并将复制的数据粘贴到该文件上

I have tried the following code but I have a problem in selection.paste line 我尝试了以下代码,但在selection.paste行中遇到了问题

Dim oExcel As Excel.Application
Dim oWB As Workbook
Set oExcel = New Excel.Application
'Set oWB = oExcel.Workbooks.Open("C:\Users\omran.alhammadi\Desktop\power ana\4EC73A75.WD0.xls")
Workbooks.Open Filename:="C:\Users\omran.alhammadi\Desktop\power ana\4EC73A75.WD0.xls"



Rows("1:9").Select
    Selection.Delete Shift:=xlUp
    Columns("A:A").Select
    Selection.NumberFormat = "hh:mm:ss;@"

    Columns("A:A").Select
    Selection.Copy
'Selection.NumberFormat = "dd/mm/yyyy hh:mm:ss;@"



Set NewBook = Workbooks.Add
    With NewBook
        .Title = "temp"
        .Subject = "tempsub"
        .SaveAs Filename:="Temp.xls"
    End With

    Workbooks("temp.xls").Activate

ActiveSheet.Columns("A:A").Select
 Range("A1").Paste
Application.CutCopyMode = False



    '    NewBook("4EC73A75.WD0.xls").Activate
    '    Sheets("Sheet1").Activate
    '*** Select the destination cell
   ' Range("A1").Select
   ' ActiveSheet.Range("A1").Paste

    Workbooks("4EC73A75.WD0.xls").Close

保存文件时清除剪贴板,因此Paste方法失败。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM