简体   繁体   English

根据另一列的值动态自动填充Excel列中的编号

[英]Auto-fill numbering in a column of Excel based on the value of another column dynamically

Firstly, let me try to explain what it is I'm trying to accomplish (lest anyone has any better suggestions). 首先,让我尝试解释一下我要完成的工作(其他人没有更好的建议)。

I have a set of data that will come to me with any number of rows and values each month. 我有一组数据,每个月都会收到任意数量的行和值。 Without the ability to be specific, I'll come up with a general example: Let's say these are people looking for jobs, so the data will change every month. 如果没有具体的能力,我将举一个一般的例子:假设这些人正在寻找工作,因此数据每个月都会变化。 In this dataset, I have a flag that indicates whether or not they're "interested" in the specific job I'm hiring for (again, this is a fake example, so I'm sure there are tools that actually do this, just go with it). 在此数据集中,我有一个标志,用于指示他们是否对我正在招聘的特定工作“感兴趣”(再次,这是一个伪造的示例,因此我确定确实有工具可以做到这一点,放手去做)。 I am only allowed to take a certain percentage of the entire pool of applicants and I want to take that percentage from only the "interested" parties, but the count of "interested" parties are greater than that percentage. 我只允许在整个申请人池中占一定比例,我只想从“感兴趣的”各方那里获得该百分比,但是“感兴趣的”缔约方的数量要大于该百分比。 Let's say my total number of people is 100000 and the interested number is 7000, but I can only take 2% of the total population. 假设我的总人数是100000,感兴趣的人数是7000,但我只能吸收总人口的2%。

So, I created a cell that gives me what the 2% is. 因此,我创建了一个单元格,它给出了2%的值。 I figured that I would try to auto-fill a line number only on the rows where the "Interested" flag is "Yes" (all other rows would be 0 or N/A) and, when the auto-fill gets to be the same number as my fixed cell giving my 2% (2000), then it stops filling in the numbers (or makes them 0 or N/A - whatever). 我发现我将尝试仅在“ Interested”标志为“ Yes”(所有其他行均为0或N / A)的行上自动填充行号,并且当自动填充变为与固定单元格相同的数字给出2%(2000年),然后它将停止填写数字(或使其变为0或N / A-随便什么)。 Then I can filter by that column. 然后,我可以按该列进行过滤。

I can do VBA or formula or whatever you can think of, I just don't know how to accomplish this. 我可以执行VBA或公式,或者您可以想到的任何方法,我只是不知道该如何完成。

For reference, this article seemed to be very close, but didn't have the specified reference to a value from another column: VBA to Auto Fill Specific Number of Rows 作为参考,本文似乎非常接近,但没有对另一列中的值进行指定的引用: VBA到“自动填充特定的行数”

This is untested, but may help. 这未经测试,但可能会有所帮助。

Dim RwNum As Integer
Dim lCol As Integer

lCol = Worksheets("Sheet1").Cells(1, Columns.Count).End(xlToLeft)

RwNum = Range("Ref Cell") 'the cell that gives you the 2%

Sheets("Sheet1").Range("A1").AutoFilter Field:=x, Criteria1:="Yes", Operator:=xlFilterValues 'Field "x" is the "Yes" column

Sheets("Sheet1").Range(Cells(1, 1), Cells(RwNum, lCol)).SpecialCells(xlCellTypeVisible).Copy Destination:=Sheets("Sheetx").Range("A1") 'sheetx would be a blank worksheet; this will give you a listing of the 2%.

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

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