如何在循环~VBA中增加字符变量

时间:2017-01-28 07:46:38

标签: excel vba excel-vba

我需要创建一个循环来使用VBA在excel电子表格中打印类似结构的步骤。

我必须从单元格E1开始,直到用户在文本框中输入的数字。我需要在单元格E1,F2,G3 ......中打印一个字符串,依此类推。

我使用了以下内容,但它不起作用:

Dim j As Integer, N As Integer
Dim i As String

N = InputBox("Plese enter the number of Processes", "Enter data", "Enter number here")

i = "E"

For j = 1 To N
    Range(i & j).Value = "Random text"
    i = i + 1
Next j

感谢您的帮助:)

I need the output as in this screenshot.

2 个答案:

答案 0 :(得分:4)

使用" Cells(rowIndex,columnIndex)"接受可以递增的行索引和列索引的数字的属性

Dim j As Integer, N As Integer, i As Integer

N = InputBox("Plese enter the number of Processes", "Enter data", "Enter number here")

i = 5

For j = 1 To N
    Cells(j,i).Value = "Random text"
    i = i + 1
Next j

答案 1 :(得分:2)

使用Range.Offset,从“E1”开始并经过值N

尝试以下代码:

Dim N As Long
Dim i As Long

N = InputBox("Please enter the number of Processes", "Enter data", "Enter number here")

For i = 1 To N
    Range("E1").Offset(i, i).Value = "Random text"
Next i