2013-08-30 141 views
0

我想写一个脚本,看起来像一个循环给我。我的数据是在A列Excel VBA - 简易循环

  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 
+0

你已经认识你了需要一个循环。做一个循环。 – ApplePie

回答

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 
+0

感谢您的帮助。 – Jax

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 Erase Statement

+0

'擦除'??不是VBA! –

+0

@KazJaw我编辑了我的答案,以包含Excel VBA帮助中的VBA清除语句。我不确定你的意思是“not VBA” – tigeravatar

+0

感谢您的帮助。 – Jax