使用VBA宏打印序列

时间:2017-02-09 08:52:53

标签: excel vba excel-vba

enter image description here我想在与此公式对应的单元格中打印字母“A”:

1 + 3 * 1 = 4

4 + 3 * 2 = 10

10 + 3 * 3 = 19

我想在单元格A4,A10,A19中输入“A”。我想为25个细胞做到这一点。

如何做到

1 个答案:

答案 0 :(得分:0)

你必须测试你是否不会走得太远:

Sub test_matangraj()
Dim i As Integer
Dim k As Integer
Dim Col As Long
Col = 1

For i = 1 To 25
    Col = Col + i * 3
    If Col < Columns.Count Then
        Cells(1, Col) = "A"
        For k = 1 To i - 1
            If Col + k < Columns.Count Then Cells(1, Col + k) = "A"
        Next k
    Else
        MsgBox "Number of columns excedeed!" & vbcrlfr & _
                "Col: " & Col, vbCritical + vbOKOnly
        Exit For
    End If
Next i
End Sub