0

I would like to write a script that looks like a loop to me. My data is under column A.

  1. I want to put script say in column B. eg B2 = A2
  2. Under B3 I put in "xxxx" and under B4 I put in "yyyy" and then repeat step 1 & 2 until end of data.

So far I have the below. How can I loop it as I have to like type it up another many hundred times.... Thanks.

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
1
  • You already know you need a loop. Do a loop. Commented Aug 30, 2013 at 15:56

2 Answers 2

2

Trying to convert your code into 'loop logic' you get something like this:

'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
Sign up to request clarification or add additional context in comments.

Comments

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

[EDIT]

For @KazJaw:

VBA Erase Statement

3 Comments

Erase??... not VBA!!
@KazJaw I have edited my answer to include the VBA Erase Statement from the Excel VBA Help. I'm not sure what you mean by "not VBA"
@tigeravatar, thanks for the info. the funny thing is that I have never used it and was not aware that erase statement exists. And I don't think I lost anything without knowing it, do I?.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.