0

我想写一个对我来说看起来像循环的脚本。我的数据在 A 列下。

  1. 我想把脚本说在 B 列中。例如 B2 = A2
  2. 在 B3 下输入“xxxx”,在 B4 下输入“yyyy”,然后重复步骤 1 和 2,直到数据结束。

到目前为止,我有以下内容。我怎样才能循环它,因为我必须再输入数百次....谢谢。

Sub DATA()

Dim DL As Long
Dim Script1 As String
Dim Script2 As String

DL = Range("A" & Rows.Count).End(xlUp).Row
Script1 = "KEY END"
Script2 = "KEY WAIT"

Range("B2").Value = Range("A2")
Range("B3").Value = Script1
Range("B4").Value = Script2

Range("B5").Value = Range("A3")
Range("B6").Value = Script1
Range("B7").Value = Script2

Range("B8").Value = Range("A4")
Range("B9").Value = Script1
Range("B10").Value = Script2   

End Sub
4

2 回答 2

2

尝试将您的代码转换为“循环逻辑”,您会得到如下结果:

'beginning of your code here
Dim i As Long
for i=2 to DL

    Range("B" & i*3-4).Value = Range("A" & i)
    Range("B" & i*3-3).Value = Script1
    Range("B" & i*3-2).Value = Script2

next i
'the end of your sub here
于 2013-08-30T16:02:33.957 回答
0
Sub tgr()

    Dim arrData() As Variant
    Dim varAcell As Variant
    Dim strScript1 As String
    Dim strScript2 As String
    Dim DataIndex As Long
    Dim i As Long

    strScript1 = "KEY END"
    strScript2 = "KEY WAIT"

    With Range("A2", Cells(Rows.Count, "A").End(xlUp))
        If .Row < 2 Then Exit Sub   'No data
        ReDim arrData(1 To .Rows.Count * 3)
        For Each varAcell In .Value
            For i = 1 To 3
                DataIndex = DataIndex + 1
                arrData(DataIndex) = Choose(i, varAcell, strScript1, strScript2)
            Next i
        Next varAcell
    End With

    Range("B2").Resize(UBound(arrData)).Value = Application.Transpose(arrData)

    Erase arrData

End Sub

[编辑]

对于@KazJaw:

VBA 擦除语句

于 2013-08-30T16:13:28.447 回答