尝试偏移活动单元时出现错误1004

时间:2013-09-25 12:34:36

标签: excel-vba vba excel

我试图抵消活动单元格,直到满足某个条件。我写的是

Do While ActiveCell.Value <> Worksheets("Unit B").Range("D1").Value  
    ActiveCell.Offset(0, 1).Select
Loop

你能帮我纠正一下吗?


提供我的整个代码供参考:

Private Sub CommandButton1_Click()

Dim ddsdata As Range

Dim i As Long
i = 1

Worksheets("Unit B").Select
Set ddsdata = Worksheets("Unit B").Range("E3:E35")
Worksheets("Data Sheet").Select
Worksheets("Data Sheet").Range("E1").Select

Do While ActiveCell.Offset(0, i) <> Worksheets("Unit B").Range("D1").Value
i = i + 1

Loop

ActiveCell.Offset(1, i).Select
ActiveCell.Value = ddsdata

1 个答案:

答案 0 :(得分:0)

尝试以下代码。避免在代码中使用Select / Activate / ActiveCell。也始终通过sheetname然后单元格引用单元格。例如,Sheet1.Range("A1")在工作簿中以获得更好的结果。

Private Sub CommandButton1_Click()

    Dim ddsdata As Range

    Dim i As Long
    i = 1

    Thisworkoook.Activate
    Set ddsdata = Thisworkoook.Sheets("Unit B").Range("E3:E35")

    Do While Worksheets("Data Sheet").Range("E1").Offset(0, i) <> Worksheets("Unit B").Range("D1").Value
            i = i + 1
    Loop

    ActiveCell.Offset(1, i).Select
    ActiveCell.Value = ddsdata

End Sub