VBA Do While Loop每次都将数据放入不同的单元格中

时间:2017-02-08 09:23:21

标签: excel vba excel-vba while-loop do-while

我最近给了一个Do While循环来吐出一些数字,你可以看到我问的另一个问题,我现在的问题是我想将数据放在我从中获取数据的单元格中,所以我知道我从中获取数据的行的行号,但因为有3个不同的可能答案可能会出来。我需要在每个单元格中放置一个值,例如:A3,A4,A5

Dim SNs As String

SNs = "Q" & xout


Const SNLength As Integer = 5

Dim SerialNumber As String
Dim index As Integer

If Left(SNs, 1) = "Q" Then SNs = Mid(SNs, 2)

index = 1



Do While index < Len(SNs)
    SerialNumber = Mid(SNs, index, SNLength)


    'Code from user


Worksheets("Calendar").Range("H" & Rng.Row).Activate

ActiveCell.Offset(0, 0).Value = "Q" + SerialNumber
ActiveCell.Offset(0, 1).Value = "Q" + SerialNumber
ActiveCell.Offset(0, 2).Value = "Q" + SerialNumber

    index = index + SNLength



  ' I am using the string SerialNumber and it always will have a value of 3 strings in each loop, so I need to insert them each into cells on the right, your code does what in the sense of placing them in the right cells but not the right data, the data ends up like this: 
当msgbox中的数字完全不同时,Q52587 Q52587 Q52587所有相同的数字,这是最后一个msgbox值。

Loop

请有人帮助我,这让我感到困惑:)

1 个答案:

答案 0 :(得分:0)

dim i as integer
Do While index < Len(SNs)

    SerialNumber = Mid(SNs, index, SNLength)
    MsgBox "Q" + SerialNumber
    index = index + SNLength

    ActiveCell.Offset(0, i).Value = "Q" + SerialNumber

    index = index + SNLength

    i=i+1
Loop