如何在多行/单元格上书写

时间:2019-02-07 14:49:41

标签: excel vba

我正在尝试编写一个循环宏以在活动单元格的多个连续行(例如1000)上写文本。我在下面粘贴了我已经尝试过的内容...我想可能很接近,但是我显然缺少一些重要的细节。有人可以帮忙吗?

Sub firstloop()
    Dim Total As Long
    Dim Count As Long
    Total = 0
    For Count = 1 To 1000
        Count = Total + Count
    ActiveCell = "Test"
    ActiveCell.Offset(1, 0) = ActiveCell
    Next Count
End Sub

3 个答案:

答案 0 :(得分:1)

摆脱两个ActiveCell并使用:

Sub firstloop()

    Dim rng as range
    Set rng = selection

    Dim Count As Long
    For Count = 1 To 1000
        rng.offset(count - 1,0).Value = "Test"
    Next Count
End Sub

答案 1 :(得分:1)

值1000

Cells(1)用于避免意外结果,如果选择的单元格范围大于1。

排除ActiveCell

Sub Values1000()

    Const strText As String = "Text"
    Const cNumber As Long = 1000

    Selection.Cells(1).Offset(1).Resize(cNumber) = strText

End Sub

包含ActiveCell

Sub Values1000()

    Const strText As String = "Text"
    Const cNumber As Long = 1000

    Selection.Cells(1).Resize(cNumber) = strText

End Sub

或者只需将常量替换为其值,即可获得单行代码。

答案 2 :(得分:0)

Sub firstloop()

    Dim Count As Long
    Dim rng As Range
    Set rng = ActiveCell

    For Count = 1 To 1000
        rng.Offset(Count - 1, 0) = "Test"
    Next Count
End Sub