如果单元格包含值n,则如何创建由n个单元格组成的列,每个单元格包含值n(然后与另一个单元格重复)? [英] If cell contains value n, how can I create a column of n cells, each containing value n (and then repeat with another cell)?

查看:75
本文介绍了如果单元格包含值n,则如何创建由n个单元格组成的列,每个单元格包含值n(然后与另一个单元格重复)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为简单起见,假设我有一列5个数字,如下所示:-

For the sake of simplicity, let's say I have a column of 5 numbers as follows:-

8
15
8
3
26

我想做的是创建一个列,该列首先由包含值'8'的8个单元格组成,然后由包含值'15'的15个单元格组成,然后由包含值'8'的8个单元格组成,依此类推.因此,此示例将导致一列长度为60个单元格(但是,我的现实生活中的列具有200多个数字,并且将导致约2500个数字的列!).

What I want to do is create a column composed firstly of 8 cells containing the value '8', then 15 cells containing the value '15', then 8 cells containing the value '8', and so on. This example would therefore result in a column length of 60 cells (however, my real-life column has over 200 numbers and would result in a column of about 2500 numbers!).

这可能吗?需要公式还是宏? (恐怕我也不擅长于此,如果这很容易的话,我深表歉意.)任何建议都非常欢迎.

Is this possible? Would it need a formula or a macro? (I'm not very adept with either, I'm afraid, so apologies if this is a no-brainer.) Any suggestions very welcome.

推荐答案

尝试一下

Public Sub Test()

    PopulateColumn Range("A1"), Range("B1")

End Sub


Public Sub PopulateColumn(ByVal rngSourceStart As Range, ByVal rngTargetStart As Range)

    Dim rngSource As Range
    Dim rngTarget As Range

    Set rngSource = rngSourceStart
    Set rngTarget = rngTargetStart

    Dim iValue As Integer
    Dim i As Integer

    While rngSource.Value <> ""
        iValue = rngSource.Value
        For i = 1 To rngSource.Value
            rngTarget.Value = iValue
            Set rngTarget = rngTarget.Offset(1, 0)
        Next
        Set rngSource = rngSource.Offset(1, 0)
    Wend

End Sub

这篇关于如果单元格包含值n,则如何创建由n个单元格组成的列,每个单元格包含值n(然后与另一个单元格重复)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆