简体   繁体   English

从数据项和乘数构建excel数组

[英]build excel array from data items and a multiplier

first question on this site. 这个网站上的第一个问题。

Been coming here to bask in the warm glow of the knowledge on offer for years! 多年来来到这里沐浴着所提供知识的热情! Please be gentle with me. 请对我好一点。 ;) ;)

I'm not a programmer but can muddle my way around excel but I have a problem in excel that I'm struggling to find a solution to. 我不是程序员,但是可以绕过excel,但是我在excel中有一个问题,我一直在努力寻找解决方案。

I need to take a set of data and turn it into an array (or list) of all the occurrences of that data. 我需要获取一组数据并将其转换为该数据所有出现的数组(或列表)。 For example a set of data (A,B,C) and an instances value for each item (2,1,3). 例如,一组数据(A,B,C)和每个项目的实例值(2,1,3)。

What I need to do is take those two items and create an array of all occurrences. 我需要做的是将这两项合并为所有出现的数组。

Like this:- 像这样:-

Data,Instances

A,2
B,1
C,3
Total 6

Result
1,A
2,B
3,C
4,A
5,C
6,C

(I hope that's clear - my rating isn't high enough to post a screenshot) (我希望这很清楚-我的评分还不够高,无法发布屏幕截图)

So, in this example I have 2 As, 1 B and 3 Cs giving a total of 6 items. 因此,在此示例中,我有2个As,1个B和3个C,总共提供6个项目。 To create the result I've run through the list 6 times listing each data item if it still has an occurrence (but an array/list that was AABCCC would be just as valid). 为了创建结果,我遍历了列表6次,列出了每个数据项(如果仍然存在)(但是AABCCC的数组/列表同样有效)。 For the full data set there could be as many as 12 different data items with any number of occurrences each from 1 to 12. 对于完整的数据集,可以有多达12个不同的数据项,且出现次数从1到12。

Somehow I think I'm overcomplicating a simple process but for the life of me I can't get my head around achieving the result I need. 我以某种方式认为我使一个简单的过程变得过于复杂,但是对于我自己的一生,我无法全力以赴地实现所需的结果。

Say we put your data in column A : 假设我们将您的数据放在A列中:

在此处输入图片说明

and run this short macro: 并运行以下简短宏:

Sub croupier()
    Dim N As Long, K As Long, i As Long, ary(), bry()
    Dim v As String
    N = Cells(Rows.Count, "A").End(xlUp).Row
    ReDim ary(1 To N)
    ReDim bry(1 To N)

    For i = 1 To N
        v = Cells(i, "A").Value
        cry = Split(v, ",")
        ary(i) = cry(0)
        bry(i) = CLng(cry(1))
    Next i

    K = 1
    While Application.WorksheetFunction.Sum(bry) > 0
        For i = 1 To N
            If bry(i) <> 0 Then
                Cells(K, "B").Value = ary(i)
                bry(i) = bry(i) - 1
                K = K + 1
            End If
        Next i
    Wend
End Sub

Our result is this: 我们的结果是这样的:

在此处输入图片说明

We repeatedly run down column A placing the values in column B until the count of an item reaches zero. 我们反复运行A将值放在B列中,直到某项的计数达到零为止。

When the overall count of items is zero, we stop. 当项目总数为零时,我们停止。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM