vba 范围(单元格(),单元格())复制过去到其他工作表而不激活其他工作表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17149562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 15:53:14  来源:igfitidea点击:

vba range (cells(), cells()) copy past to other worksheet with out activating the other worksheet

excel-vbavbaexcel

提问by user2457968

I have a cell in a worksheet named Aand I have to copy it then paste it to Range(Cells(23, 60), cells(23, 78))of worksheet called B.

我在一个名为的工作表中有一个单元格,A我必须复制它然后将其粘贴到Range(Cells(23, 60), cells(23, 78))名为B.

How should I do ?

我应该怎么做 ?

I thought about using dynamic cell references like : copyrange = Range(Cells(696, 60), Cells(696, 60)).

我想过使用动态单元格引用,如:copyrange = Range(Cells(696, 60), Cells(696, 60))

回答by Siddharth Rout

Is this what you are trying?

这是你正在尝试的吗?

Sub Sample()
    Dim wsFrom As Worksheet, wsTo As Worksheet
    Dim CopyFrom As Range, CopyTo As Range
    Dim r1 As Long, r2 As Long, r3 As Long, r4 As Long
    Dim c1 As Long, c2 As Long, c3 As Long, c4 As Long

    Set wsFrom = ThisWorkbook.Sheets("A")
    Set wsTo = ThisWorkbook.Sheets("B")

    r1 = 696: c1 = 60
    r2 = 696: c2 = 60
    r3 = 23: c3 = 60
    r4 = 23: c4 = 78

    With wsFrom
        Set CopyFrom = .Range(.Cells(r1, c1), .Cells(r2, c2))
    End With

    With wsTo
        Set CopyTo = .Range(.Cells(r3, c3), .Cells(r4, c4))
    End With

    CopyFrom.Copy CopyTo
End Sub

回答by Gary's Student

Consider:

考虑:

Sub dural()
Dim r As Range
With Sheets("Sheet2")
    Sheets("Sheet1").Cells(696, 60).Copy Sheets("Sheet2").Range(.Cells(23, 60), .Cells(23, 78))
End With
End Sub

回答by Rafael

Don't forget the worksheet's name before cells():

之前不要忘记工作表的名称cells()

sub CopyA_PasteB()

dim wb as Workbook
dim wsA as worksheet
dim wsB as worksheet

set wb = Workbooks("WORKBOOK NAME")
set shA = wb.Worksheets("A")
set shB = wb.Worksheets("B")

shA.Range(shA.Cells(696, 60), shA.Cells(696, 60)).Copy
shB.Range("A1").Paste     'replace with your destination

End sub

回答by sous2817

You didn't really give us a lot to go on here, so I'll post one way and see if it gets you going:

你并没有真正给我们带来很多东西,所以我会发布一种方式,看看它是否能让你前进:

Sub test()
Dim columnReferenceSource     As Long
Dim rowReferenceSource        As Long
Dim columnReferenceDest       As Long
Dim rowReferenceDest          As Long
Dim r                         As Range

rowReferenceSource = 696
columnReferenceSource = 60

rowReferenceDest = 23
columnReferenceDest = 60

Set r = Range(Sheets("A").Cells(rowReferenceSource, columnReferenceSource), Sheets("A").Cells(rowReferenceSource, columnReferenceSource + 18))

r.Copy Destination:=Sheets("B").Cells(rowReferenceDest, columnReferenceDest)

End Sub

Obviously you'll have to set the row and column numbers for your situation. If you can provide more details as to what you're trying to do, I'm sure we can give a more complete answer. For instance, how do you go about determining the source and destination range?

显然,您必须根据自己的情况设置行号和列号。如果您可以提供有关您要执行的操作的更多详细信息,我相信我们可以提供更完整的答案。例如,您如何确定源和目标范围?