适用于Excel 2007及更高版本的VBA

时间:2014-01-22 18:46:19

标签: vba select excel-2007 excel-2010

我编写了一些查看列的VBA代码,查找该列中包含数据的下一个单元格,并将两者之间的单元格设置为范围。这最初是为Excel 2003工作簿编写的。当然,相同的命令对2007及以上的工作簿不起作用。任何人都可以帮助翻译2010 Excel VBA。

这是原始代码:

Dim first As Integer
Dim Last As Integer
Dim i As Integer
Dim n As Integer

n = Worksheets("Sheet1").Range("A:A").Cells.SpecialCells(xlCellTypeConstants).Count - 2
Range("A3").Select

For i = 1 To n
    first = (ActiveCell.Row + 1)
    Selection.End(xlDown).Select
    Last = (ActiveCell.Row - 1)
    Range("J" & first & ":J" & Last).Select
    Selection.Value = "=J$" & (first - 1)
    Range("A" & Last + 1).Select
Next i

当我在Excel 2010中运行它时。它不是在列A中找到包含数据的下一个单元格,而是选择整个列。

先谢谢。

1 个答案:

答案 0 :(得分:0)

Sub GoDownList()
Dim first As Integer
Dim Last As Integer
Dim i As Integer
Dim n As Integer

first = Range("A3").Row
Last = Range("A3").End(xlDown).Row
Range("J" & first & ":J" & Last).Value = "=J$" & (first - 1)

Do
    first = Range("A" & Last).End(xlDown).Row
    Last = Range("A" & first).End(xlDown).Row
    Range("J" & first & ":J" & Last).Value = "=J$" & (first - 1)
    If Range("A" & Last).End(xlDown).Row > 1000000 Then
        Exit Do
    End If
Loop
End Sub

我认为您的代码无效,因为您使用Selection.Value代替Selection.Formula