[英]Loop through Column A in Excel VBA, get a random word from Column B and output the row in Column C
I'm very new to VBA Macros and Excel, and I have a small problem that I need to solve. 我是VBA Macros和Excel的新手,我有一个小问题需要解决。
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. 假设我有一个A列 ,其中有20行的单个单词/字母,有一个B列 ,其中有5行的单个单词/字母。
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 ? 如何从A1开始遍历A列并在B列中随机选择一行以加在一起形成一个新字符串,该字符串将在C列中输出?
Meaning I want the output to look like this in Column C : 意思是我希望输出在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? 任何人都知道如何实现这一目标吗?
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: 标记Excel 2007后,您可能需要改用以下代码:
iRandom = Round((5 - 1) * Rnd + 1, 0)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.