如何复制一列数据,并用偏移量粘贴

时间:2022-09-17 11:37:59

I'm working on a Excel 2010 Sheet that has some doctors names and their adresses, but frequently there are 2 names that are identical but have diferent adresses. On this cases I would like to copy the adress info to the same row as the first name but wit h an offset of 4 collumns. Heres the code I came up with

我正在写一份Excel 2010表格,上面有一些医生的名字和他们的副词,但通常有两个名字是相同的,但都有不同的副词。在这种情况下,我想将adress info复制到与第一个名字相同的行中,但也就是4个集合的偏移量。这是我想出的密码

Sub OraganizadorEndereços()

    ActiveCell.Select
    If ActiveCell.Value = ActiveCell.Offset(1, 0).Value _
    Then ActiveCell.Offset(1, 0).Activate: _
    Range(ActiveCell.Offset(0, 1), ActiveCell.Offset(0, 4)).Copy: _
    ActiveCell.Offset(-1, 0).Select: _
    ActiveCell.Offset(0, 5).Paste _
    Else ActiveCell.Offset(1, 0).Select

End Sub

But I get an error on the

但是我得到了一个错误

ActiveCell.Offset(0, 5).Paste _
Else ActiveCell.Offset(1, 0).Select

Part of the code, saying that the obeject does not accept this property/method

代码的一部分,说obeject不接受这个属性/方法

And remember, I started programing in VBA today, so if you can answer with an explanation, I would appreciate.

记住,我今天开始在VBA编程,如果你能给我一个解释,我会很感激。

2 个解决方案

#1


6  

Try to rely less on activating and selecting cells - you can assign cells to a range variable to make things much easier. Also, you don't need to copy the cells (unless you also want to copy the formatting e.g. colours), use their .Value instead:

尽量减少对激活和选择单元格的依赖——您可以为范围变量分配单元格,使事情更容易。此外,您不需要复制单元格(除非您也想复制格式,例如颜色),使用它们的.Value代替:

Sub OraganizadorEndereços()

Dim rngTest as Range 'Define rngTest variable as Range
Set rngTest = Activecell 'Set rngTest to be the ActiveCell
If rngTest.Value = rngTest.Offset(1, 0).Value Then 
    'Replace the .Value of the columns to right with the .Value of the row below
    Range(rngTest.Offset(0,5), rngTest.Offset(0,8).value = Range(rngTest.Offset(1, 1), rngTest.Offset(1, 4)).Value
Else 
    Set rngTest = rngTest.Offset(1,0) 'Set rngTest to be the next line down
End If

End Sub

#2


2  

Try below code :

试试下面的代码:

Sub OraganizadorEndereços()

    Dim rng As Range
    Dim offsetRng As Range

    Set rng = ActiveCell
    rng.Select

    Set offsetRng = rng.Offset(1, 0)

    If rng = offsetRng Then
        offsetRng.Offset(0, 1).Resize(, 4).Copy offsetRng.Offset(0, 5)
        rng.Offset(1, 0).Activate
    Else
        rng.Offset(1, 0).Select
    End If

End Sub

#1


6  

Try to rely less on activating and selecting cells - you can assign cells to a range variable to make things much easier. Also, you don't need to copy the cells (unless you also want to copy the formatting e.g. colours), use their .Value instead:

尽量减少对激活和选择单元格的依赖——您可以为范围变量分配单元格,使事情更容易。此外,您不需要复制单元格(除非您也想复制格式,例如颜色),使用它们的.Value代替:

Sub OraganizadorEndereços()

Dim rngTest as Range 'Define rngTest variable as Range
Set rngTest = Activecell 'Set rngTest to be the ActiveCell
If rngTest.Value = rngTest.Offset(1, 0).Value Then 
    'Replace the .Value of the columns to right with the .Value of the row below
    Range(rngTest.Offset(0,5), rngTest.Offset(0,8).value = Range(rngTest.Offset(1, 1), rngTest.Offset(1, 4)).Value
Else 
    Set rngTest = rngTest.Offset(1,0) 'Set rngTest to be the next line down
End If

End Sub

#2


2  

Try below code :

试试下面的代码:

Sub OraganizadorEndereços()

    Dim rng As Range
    Dim offsetRng As Range

    Set rng = ActiveCell
    rng.Select

    Set offsetRng = rng.Offset(1, 0)

    If rng = offsetRng Then
        offsetRng.Offset(0, 1).Resize(, 4).Copy offsetRng.Offset(0, 5)
        rng.Offset(1, 0).Activate
    Else
        rng.Offset(1, 0).Select
    End If

End Sub