如何在每次重复循环时向右移动一列?

时间:2015-03-10 07:58:32

标签: excel-vba vba excel

我试图将数据输入到列b,转到列c依次

我怎么能这样做是我的代码请帮助我...

Sub () 

Dim Continue As String
Continue = "Go"

Do While Continue = "Go"

Dim Message As String
Dim Answer As String

Message = "enter name"
Answer =  inputbox(Message) 
ActiveSheet.Range("B1").Value = (Answer)


If ActiveSheet.Range("B2") = "yes" then
activesheet.range("B10").value="good"
Else
ActiveSheet.Range("B10").Value = "no good"
End If

Continue = InputBox("Do you want to continue?")
Loop

End Sub

每次循环时,它都会继续在列c中插入数据,我希望在列c中

你可以编辑我的剧本..

提前致谢

1 个答案:

答案 0 :(得分:0)

当你写:ActiveSheet.Range("B1").Value = (Answer)时,“B1”是字面意思;它永远不会改变。

Range的参数是一个String,您可以在运行时构建该字符串:

Dim ColumnIdCrnt as String
Dim RowCrnt as Long

ColumnIdCrnt = "B"
RowCrnt = 1
ActiveSheet.Range(ColumnIdCrnt & RowCrnt).Value = (Answer)

这不是最简单的方法,但它是可能的。

而不是Range使用Cells。语法为Cells(Row, Column),其中Row必须是行号,Column可以是列标识符或列号。以下都是等效的:

ActiveSheet.Range("B1").Value
ActiveSheet.Cells(1, "B").Value
ActiveSheet.Cells(1, 2).Value

列号和列标识符之间的关系是:1 =“A”,2 =“B”,3 =“C”,26 =“Z”,27 =“AA”等等。

所以你可以这样写:

ColNumCrnt as Long

ColNumCrnt = 1

Do While Continue = "Go"
   ‘ Process input using column ColNumCrnt 

  ActiveSheet.Range(ColNumCrnt & "1").Value = (Answer)

  ColNumCrnt = ColNumCrnt + 1   ‘ Move one column to the right ready for next loop
Loop