简体   繁体   English

从一个工作表复制到另一个工作表而无需使用vba打开excel

[英]Copy from one worksheet to another without opening excel using vba

I'm trying to copy cell values from one excel to another using VBA. 我正在尝试使用VBA将单元格值从一个Excel复制到另一个。 What I'm trying to do is that by clicking on the script, excel (without getting opened) values should automatically get copied from one worksheet to another. 我想做的是,通过单击脚本,excel(无需打开)值应该自动从一个工作表复制到另一个工作表。 While executing my script I get an error which says Type Mismatch: 'Sheets'. 在执行脚本时,出现错误,提示类型不匹配:“ Sheets”。 I've read various posts but could not find an answer to this problem. 我读过各种文章,但找不到该问题的答案。 My script looks like this: 我的脚本如下所示:

ImportData_Click

Sub ImportData_Click()
Dim objExcel2
Set objExcel2 = CreateObject("Excel.Application")

' open the source workbook and select the source sheet
objExcel2.Workbooks.Open "<path>\test1_vbs.xlsx"


' copy the source range
Sheets("Sheet1").Range("A1:B2").Copy

' select current workbook and paste the values
ThisWorkbook.Activate

Sheets("Sheet2").Range("A1:B2").PasteSpecial xlPasteValues

' close the source workbook
Windows("<path>\test1_vbs.xlsx").Activate
ActiveWorkbook.Save
ActiveWorkbook.Close

End Sub

If you are going to tranfer values only between application instances, skip the clipboard and use an array. 如果仅在应用程序实例之间转移值,请跳过剪贴板并使用数组。

Option Explicit

Sub ImportData_Click()
    Dim objExcel2 As Object, arr As Variant

    Set objExcel2 = CreateObject("Excel.Application")
    ' open the source workbook and select the source sheet
    With objExcel2.Workbooks.Open("c:\test\test2_vbs.xlsx")
        ' copy the source range to variant array
        arr = .Worksheets("Sheet1").Range("A1:B2").Value
        ' close the source workbook
        .Close savechanges:=False
    End With

    ' select current workbook and paste the values
    ThisWorkbook.Worksheets("Sheet2").Range("A1").Resize(UBound(arr, 1), UBound(arr, 2)) = arr

    ' close the source application instance
    objExcel2.Quit
    Set objExcel2 = Nothing

    ActiveWorkbook.Save
    ActiveWorkbook.Close

End Sub

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

相关问题 Excel VBA:从一个工作表复制到另一个 - Excel VBA: Copy from one worksheet to another 使用Excel VBA将长度可变的粘贴范围从一个工作表复制到另一工作表 - Copy Pasting Range of varying length from one worksheet to another worksheet using Excel VBA 如何使用VBA在excel上将列从一个工作表复制到另一个工作表? - How to copy columns from one worksheet to another on excel with VBA? Excel VBA-通过循环将数据从一个工作表复制到另一个工作表 - Excel VBA - Copy data from one worksheet to another via loop Excel使用VBA将数据从一张纸复制到工作表上的另一张 - Excel copy data from one sheet to another on worksheet refresh with vba Excel VBA通​​过循环将匹配信息从一个工作表复制到另一个工作表 - Excel VBA Copy matching information from one worksheet to another with a loop 使用 VBA 代码将 VBA 代码从一个工作表复制到另一个工作表 - Copy VBA code from one Worksheet to another using VBA code 如何使用VBA将特定数据从一个工作表复制到另一个工作表 - How to copy specific data from one worksheet to another using VBA 使用 VBA 将一系列单元格从一个工作表复制到另一个工作表 - Copy a range of cells from one worksheet to another using VBA Excel-VBA将数据从一个工作表复制到另一工作表并粘贴到新行 - Excel-VBA Copy data from one worksheet to another worksheet and paste in new row
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM