Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I'm very new to VBA Macros and Excel, and I have a small problem that I need to solve.

Say I have a Column A that has 20 rows of a single word/letter, and a Column B that has 5 rows of a single word/letter.

How do I loop through Column A, starting from A1 and randomly select a row in Column B to add together to form a new string which will be output in Column C?

Meaning I want the output to look like this in Column C:

A1 (for each A) + B2 (random)
A2 (for each A) + B4 (random)
A3 (for each A) + B1 (random)
A4 (for each A) + B3 (random)
A5 (for each A) + B4 (random)
A6 (for each A) + B2 (random)
...

and so on and so forth.

Anyone has any idea how to achieve this?

share|improve this question
    
possible duplicate of Generating a header and a set of given sentences with Excel / VBA – brettdj Jul 25 '13 at 9:40
up vote 3 down vote accepted

Try:

Sub HTH()
    Dim rCell As Range
    Dim iRandom As Integer

    For Each rCell In Range("A1:A20")
        iRandom = Application.WorksheetFunction.RandBetween(1, 5)
        rCell.Offset(, 2).Value = CStr(rCell.Value & Cells(iRandom, "B").Value)
    Next rCell

End Sub

As you have tagged Excel 2007 you may need to use this instead:

iRandom = Round((5 - 1) * Rnd + 1, 0)
share|improve this answer
    
awesome. I can't thank you enough! – Kyle Yeo Jul 25 '13 at 11:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.