编译错误:重复声明

时间:2018-02-25 04:10:02

标签: excel vba excel-vba

我这里有这个代码:

Dim c As Long, rG As Range, vR As Variant
c = wsC.Range("A" & Rows.Count).End(xlUp).Row + 1
Dim wsA As Worksheet, wsC As Worksheet
Set wsA = Sheets("Active")
Set wsC = Sheets("Closed")
wsA.Activate
For i = ActiveSheet.Range("X" & Rows.Count).End(xlUp).Row To 1 Step -1
Set rG = Range("X" & i)
vR = rG.Value
If InStr(vR, "/") > 0 Then
    rG.EntireRow.Copy wsC.Cells(c, 1)
    rG.EntireRow.Delete
    c = c + 1
End If
Next I

然而,当我运行代码时,它突出显示:

wsC As Worksheet

黄色,弹出一声说:

  

Microsoft Visual Basic

     

编译错误:

     

当前范围内的重复声明

但是,如果我把这段代码:

Dim c As Long, rG As Range, vR As Variant
c = 1000
Dim wsA As Worksheet, wsC As Worksheet
Set wsA = Sheets("Active")
Set wsC = Sheets("Closed")
wsA.Activate
For i = ActiveSheet.Range("X" & Rows.Count).End(xlUp).Row To 1 Step -1
Set rG = Range("X" & i)
vR = rG.Value
If InStr(vR, "/") > 0 Then
    rG.EntireRow.Copy wsC.Cells(c, 1)
    rG.EntireRow.Delete
    c = c + 1
End If
Next I

代码运行成功,但是具有此代码的唯一问题是,如果我有超过1000个单元格条目,那么它将在单元格A1000处开始替换。我想要完成的是将它粘贴在A列但是在第一个空闲单元格中。任何人对我如何实现这一点有任何想法?

非常感谢提前。

1 个答案:

答案 0 :(得分:0)

在@RADO和@Joey Grant的帮助下,我很高兴能解决这个问题!十分感谢大家!这就是我最终的结果。让我知道任何人都有更好的想法。

Dim c As Long, rG As Range, vR As Variant
Dim wsA As Worksheet, wsC As Worksheet

Set wsA = Sheets("Active")
Set wsC = Sheets("Closed")

c = wsC.Range("A" & Rows.Count).End(xlUp).Row + 1
wsA.Activate

For i = ActiveSheet.Range("X" & Rows.Count).End(xlUp).Row To 1 Step -1
    Set rG = Range("X" & i)
    vR = rG.Value
    If InStr(vR, "/") > 0 Then
        rG.EntireRow.Copy wsC.Cells(c, 1)
        rG.EntireRow.Delete
        c = c + 1
    End If
Next i

非常感谢Stackoverflow社区你们摇滚!