简体   繁体   English

VBA-使用具有范围的单元格进行复制和粘贴

[英]VBA - Using Cells with Range to copy and paste

I am beginner in VBA and am finding it hard to understand what is wrong with my below statement. 我是VBA的初学者,发现很难理解我的以下声明出了什么问题。 To give a context of what I am trying to do, I have a source sheet (oldViewsWbk) whose cells I want to copy into my final workbook (finalViewsWbk) and then do some logic over these copied cells. 为了给出我要做什么的上下文,我有一个源工作表(oldViewsWbk),我要将其单元格复制到最终工作簿(finalViewsWbk)中,然后对这些复制的单元格进行一些逻辑处理。

ActiveSheet.Range(Cells(2, 2), Cells(246, 2)).Value = oldViewsWbk.Sheets(1).Range(Cells(2, 2), Cells(246, 2)).Value

My ActiveSheet is my final workbook. 我的ActiveSheet是我的最终工作簿。 For now I have hard-coded the row and column numbers, but they will be replaced by variables once I can get this working. 目前,我已经对行号和列号进行了硬编码,但是一旦可以使用,它们将被变量替换。

I know I can use Copy and PasteSpecial to get this done, but I am wondering why this particular statement keeps throwing the error '1004': Object defined or Application defined error . 我知道我可以使用Copy和PasteSpecial完成此操作,但是我想知道为什么这个特定的语句不断抛出错误'1004': Object defined or Application defined error Maybe I cant do this way at all, but I would like to know whats the reason behind it. 也许我根本不能这样做,但是我想知道背后的原因是什么。

Many Thanks! 非常感谢!

Because you don't reference the sheet when calling Cells, the activesheet is implied. 因为在调用Cells时没有引用工作表,所以隐含了活动工作表。 It works on the left side of the equation, because both the range and the cells are on the same sheet (ActiveSheet) 它在等式的左侧起作用,因为范围和单元格都在同一张纸上(ActiveSheet)

ActiveSheet.Range(Cells(2, 2), Cells(246, 2)).Value

but not on the right side, because the sheet is oldViewsWbk.Sheets(1) while the cells are on the activeSheet 但不在右侧,因为单元格位于activeSheet上时工作表为oldViewsWbk.Sheets(1)

oldViewsWbk.Sheets(1).Range(Cells(2, 2), Cells(246, 2)).Value

To address that, use 为了解决这个问题,请使用

ActiveSheet.Range(ActiveSheet.Cells(2, 2), ActiveSheet.Cells(246, 2)).Value = oldViewsWbk.Sheets(1).Range(oldViewsWbk.Sheets(1).Cells(2, 2), oldViewsWbk.Sheets(1).Cells(246, 2)).Value

Cells refers to the active sheet. Cells是指活动表。 .Cells refers to the sheet you've referenced. .Cells是指您引用的工作表。

dim rng as Range
With oldViewswbk.Sheets(1)
    Set rng = .Range(.Cells(2,2),.Cells(246,2))
Wend

ActiveSheet.Range(Cells(2,2),Cells(246,2)).Value = rng.Value

为避免此类问题,请使用:

ActiveSheet.Range("B2:B246").Value = oldViewsWbk.Sheets(1).Range("B2:B246").Value

its all on how you start the code off, for example, 有关如何启动代码的所有信息,例如,

Sub Button1_Click()
    Dim wb As Workbook
    Dim bk As Workbook
    Dim ws As Worksheet
    Dim sh As Worksheet
    Dim WSrng As Range
    Dim shRng As Range

    Set wb = Workbooks("Book3 1.xlsx")
    Set bk = Workbooks("Book3 2.xlsx")

    Set ws = wb.Sheets("Sheet1")
    Set sh = bk.Sheets("Sheet1")

    Set WSrng = ws.Range("A1:A10")
    Set shRng = sh.Range("A1:A10")

    WSrng.Value = shRng.Value


End Sub

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

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