根据变量复制并粘贴x次

时间:2014-07-22 13:15:52

标签: vba count copy

我有一个锦标赛竞争者名单。如果竞争者名单超过12,那么我需要在2个锦标赛戒指中尽可能平等地分割名单(即,如果在“1号环”中为13则为7,在“2号环”中为6)。决定竞争对手如何分裂的变量包含在单元格D35& D37。因此,我需要一段代码来查看单元格D35中的值,然后执行复制粘贴序列(以填充Ring 1范围)多次。然后它需要对D37中显示的值执行相同的操作。有人能帮忙吗。我已经看过这里和NET,我仍然在苦苦挣扎。

另外,如何发布我的工作簿样本以显示我正在尝试处理上述问题?

亲切的问候 安迪

1 个答案:

答案 0 :(得分:0)

只需将您的任务分成一些简单的任务:

- 检查是否超过12个竞争对手

- 如果超过12,则分为2个环;如果不只是使用1环

- 检查竞争对手,将它们粘贴到指定的环中

(我真的不明白你对变量D35和D37的意思,但我还不允许对你的帖子发表评论。如果你发表评论,我会改变这个答案)

我假设您的竞争对手名单在Cell A1中开始

Sub process()
Dim countCompetitors As Integer
Dim amountRing1 As Integer
Dim amountRing2 As Integer

Dim rng1 As Range
Dim rng2 As Range

countCompetitors = 1

'count the amount of competitors
While Not IsEmpty(ActiveSheet.Cells(countCompetitors, 1))
    countCompetitors = countCompetitors + 1
Wend

countCompetitors = countCompetitors - 1

If countCompetitors Mod 2 = 0 Then
    'amount of competitors is even
    amountRing1 = countCompetitors / 2
    amountRing2 = countCompetitors / 2
Else
    'amount of competitors is odd
    amountRing1 = countCompetitors / 2
    amountRing2 = countCompetitors / 2 - 1
End If

'rng1 and rng2 now contain the competitors
Set rng1 = ActiveSheet.Range(Cells(1, 1), Cells(amountRing1, 1))
Set rng2 = ActiveSheet.Range(Cells(amountRing1 + 1, 1), Cells(amountRing1 + amountRing2, 1))

'copy ring1 in column B and ring2 in column C
rng1.Copy ActiveSheet.Range("B2")
rng2.Copy ActiveSheet.Range("C2")

End Sub

我希望我可以帮助你一点,如前所述,请解释你对单元格D35和D37中的变量的意思,我会相应地改变我的代码;)