不知何故,它不是复制

时间:2016-10-06 13:35:56

标签: vba excel-vba excel

Sub GetTW_Data()

ThisWorkbook.Activate 'start in THIS workbook
Sheets(1).Select 'switch to data import sheet

'Opens source file (this filename never changes)
Workbooks.Open Filename:="C:\Test 1\1 eBay BrandProgress.xls", ReadOnly:=True

Workbooks("1 eBay BrandProgress.xls").Activate 'switch to source workbook
Sheets(1).Select.UsedRange.Copy 'switch to source data sheet

ThisWorkbook.Activate 'Return to THIS workbook

[B5].PasteSpecial Paste:=xlPasteValues 'paste data to import start cell
[a1].Select 'cancels highlighted paste region

Workbooks("1 eBay BrandProgress.xls").Close 'source data workbook

End Sub

2 个答案:

答案 0 :(得分:1)

您需要避免使用“选择并激活”。

将工作簿,工作表和范围设置为变量并使用它们。

另外,粘贴时只需要剪贴板以避免速度。

Sub GetTW_Data()
Dim tWb As Workbook
Dim ebayWb As Workbook
Dim tWs As Worksheet
Dim ebayWs As Worksheet
Dim rng As Range

Set tWb = ThisWorkbook
Set tWs = tWb.Sheets(1)

Set ebayWb = Workbooks.Open(Filename:="C:\Test 1\1 eBay BrandProgress.xls", ReadOnly:=True)
Set ebayWs = ebayWb.Sheets(1)

Set rng = ebayWs.UsedRange

tWs.Range("B5").Resize(rng.Rows.Count, rng.Columns.Count).Value = rng.Value

ebayWb.Close

End Sub

答案 1 :(得分:0)

您似乎正在尝试将整个

UserRange
工作表的内容粘贴到
Sheet(1)
,但您已将其粘贴到单元格B5中。您将更多的行和列粘贴到目标中,因为您缺少目标中的第一行和前四列。

更改复制范围以解决该差异,或粘贴到目标

上的单元格A1中