更新由Dynamic Source创建的多个命名范围使用溢出
在Excel 365中,目前,我根据动态标头行手动更新命名范围,每个标头下方具有动态值。我希望有人可以帮助我使用VBA代码根据当前数据/表更新名称和范围。
目的是为每个项目提供一个带有相关单位
值的下拉列表值。
源数据来自每周更新的外部工作簿。数据位于item
和单位
的列中,但是每个item
and 单位通常都有多个实例每周可能会有新项目或删除一些项目,与关联的单位
值相同。来源如下所示:
项目 | 单位 |
---|---|
AA 120 | 100 |
AA 120 | 100 |
AA 120 | 150 |
AA 60 | 350 |
BB 200 | 36 |
BB 200 30 BB 200 | 30 |
BB 200 | 30 |
SH 1001 | 55 |
SH 1001 | 55 |
拉动独特的标头,
=IFERROR(INDEX($B$4:$B$600, MATCH(0, COUNTIF($K$2:K2, $B$4:$B$600), 0)),"")
使用以下公式在以下公式中 标题下方的行,要拉出每个标头的唯一值,
=UNIQUE(FILTER($C$4:$C$600,$B$4:$B$600=L2))
结果表如下所示:
AA 120 | AA 60 | BB 200 | SH 1001 1001 |
---|---|---|---|
1001 | 1001 100 330 | 36 | 55 |
150 | 30 |
我目前有所有标题,然后选择下面的行,然后选择从选择
在定义的名称
下创建,然后选择top Row
。这会创建名为范围的范围,指的是每个标头下方的单元格。然后,我通过在末尾添加#
来编辑名称所指的每个范围,因此它指的是溢出的数据,因为它是一个动态范围。
例如,从aa_120
从= Sheet1!$ 3
= Sheet1!$ L $ 3#
我一一做这件事对于100多个命名范围。有任何技巧还是有助于提高这一效率?
哦,我用于数据验证源的公式为=间接(C7)
其中c7
是项目/命名范围。这一切都很好...
TIA
编辑: 我已经制定了一些VBA代码,以在引用范围的末尾添加#
。所有命名范围的范围都从第3行开始,因此,
Sub RangeRename()
Dim n As Name
For Each n In Names
If Right(n.RefersTo, 1) = "3" Then n.RefersTo = n.RefersTo & "#"
Next n
End Sub
如果某人可以改进我的VBA,以更新源数据更新何时更新范围,则可以一次更新所有相关范围。当前,我在name Manager
中删除所有命名范围,然后选择所有已更新的item
和单位
数据的2行。然后,从
定义的名称下从选择创建,然后选择top Row
。然后我运行宏。但是,如果宏可以在上面做所有事情,那就太好了。我很难看到项目
和对应单位
的数量是否变化,因为它是动态的。另外,有3个不同的床单上有3个数据源,所有行2& 3,但列从l,n&开始。 t对于3张。
我所解决的问题将对我有用,但是如果有人可以改进我的代码,我将非常感激!
In Excel 365, currently I manually update multiple named ranges based on a dynamic header row, with dynamic values under each header. I hoping someone can help me with using VBA code to update the names and ranges all together based on the current data/table.
The goal is to have a drop down list with the relevant Units
values, without blanks or duplicates, for each Item.
The source data comes from an external workbook that is updated weekly. The data is in columns of Item
and Units
, but there is often multiple instances of each Item
and Units
and each week there may be new Items or some removed, same with the associated Units
values. The source is as per below example:
ITEM | UNITS |
---|---|
AA 120 | 100 |
AA 120 | 100 |
AA 120 | 150 |
AA 60 | 350 |
BB 200 | 36 |
BB 200 | 30 |
BB 200 | 30 |
SH 1001 | 55 |
SH 1001 | 55 |
The unique headers are pulled using the formula
=IFERROR(INDEX($B$4:$B$600, MATCH(0, COUNTIF($K$2:K2, $B$4:$B$600), 0)),"")
The following formula is in the row below the headers, to pull the unique values for each header
=UNIQUE(FILTER($C$4:$C$600,$B$4:$B$600=L2))
The resulting table is as per below example:
AA 120 | AA 60 | BB 200 | SH 1001 |
---|---|---|---|
100 | 350 | 36 | 55 |
150 | 30 |
Currently I have highlight all the headers and the row below and select Create from Selection
under Defined Names
and select Top row
. This creates named ranges that refer to the cell below each header. I then edit each range that the name refers to, by adding #
at the end, so it refers to the spilled data, as it is a dynamic range.
e.g. Update the named range reference for AA_120
from =SHEET1!$L$3
to =SHEET1!$L$3#
I do this one by one for 100+ named ranges. Any tips or help to make this more efficient?
oh and the formula I'm using for the source of the Data Validation is =INDIRECT(C7)
where C7
is the Item/named range. This all works well...
TIA
EDIT:
I worked out some VBA code to add the #
at the end of the referred range. The range for all Named Ranges starts on row 3, so this worked to update all relevant ranges at once...
Sub RangeRename()
Dim n As Name
For Each n In Names
If Right(n.RefersTo, 1) = "3" Then n.RefersTo = n.RefersTo & "#"
Next n
End Sub
Would still really appreciate it if someone could improve on my VBA, to update all the ranges from when the source data is updated. Currently I delete all named ranges in the Name Manager
, then select the 2 rows with all the updated Item
and Units
data. Then Create from Selection
under Defined Names
and select Top row
. Then I run my macro. But if the Macro could do all above, that would be great. The difficulty I see if that the amount of Items
and corresponding Units
vary as it is dynamic. Plus there are 3 data sources on 3 different sheets, all rows 2&3 but columns start at L, N & T for the 3 sheets.
What I have worked out will work for me, but if anyone can improve on my code, I'd be extremely grateful!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许我完全不明白您要做什么...但是您无法使用简单的枢轴表获得想要的信息?我想您可以使用一些高级技巧来获取您想要的格式...
但是使用当前的Excel公式,我想我通过这样做可以得到您想要的东西:
我将您的源数据粘贴到工作表中( A1:b10)并将其纳入
表(控制+T),选择“我的表具有标头”选项。 (我认为最好使用桌子,但是如果您不愿意,可以在下面的公式进行调整。)
我在同一或另一个纸上选择了另一个单元格 - e2-和输入
= transpose(unique(table1 [item]))
。这给出了水平的独特物品溢出。在(IE,e3)下方的单元格中,我输入
= ifError(sort(sorter(unique(filter(table1 [[units]):[[units]]:[table1],table1 [[item]]:[item]] = d15)),“”)
。 (如果您想水平拖动事物,我们需要重复此类包裹的列名,我们需要。)如果您不需要/想对我们要创建的列中进行分类,则可以离开该单元公式的一部分。我从左侧水平拖动该单元格,现在超越了您现在需要的(如果您的列表中有任何新项目)。公式中的
ifError
将使表在当前没有数据显示的最正确的列上保持清洁。如果/当您在源数据表中添加更多行时,溢出的公式会进行相应调整,因此我认为您担心动态源会得到照顾。结果如下所示。也许有一种方法可以设计一个单一公式,当将E3放入E3时会溢出到2D阵列中,因此您甚至不需要拖动东西...但是我会把它留给其他人。
Maybe I don't understand exactly what you're trying to do...but can't you get the info you want with a simple pivot table? I imagine there are advanced tricks you can apply to that to get to the formatting you want...
But using current Excel formulas, I think I got what you want by doing this:
I pasted your source data into a worksheet (A1:B10) and made it into
a table (Control+T), selecting 'My table has headers' option. (I think it's better to use a table but it's certainly possible to tweak the formulas below if you don't want to.)
I picked another cell on the same or another sheet--let's say E2--and entered
=TRANSPOSE(UNIQUE(Table1[ITEM]))
. This gives a horizontal spill of unique items.In the cell immediately below that (ie, E3) I entered
=IFERROR(SORT(UNIQUE(FILTER(Table1[[UNITS]:[UNITS]],Table1[[ITEM]:[ITEM]]=D15))),"")
. (You need to repeat the bracketed column names like this if you want to drag things horizontally, which we do.) If you don't need/want to sort the Units in the columns we're going to create, you can leave that part of the formula out.I dragged that cell horizontally to the left, going way beyond you need it right now (in case any new Items appear in your list). The
IFERROR
in the formula will keep the table looking clean on the right-most columns where no data currently appear. If/when you add more rows to your source data table, the spilled formulas will adjust accordingly, so I think your worry about dynamic sources is taken care of.Results shown below. Maybe there's a way to devise a single formula that when put into E3 will spill into a 2-D array, so you don't even need to drag things...but I'll leave that to someone else.
以防万一任何人都想实现同一件事。
我使用过滤器创建项目列,然后
transpose(unique(filter(g:g,b:b = t7
,对于每个项目旁边的单位选项的行。对于VBA,我添加了然后
在数据验证中,我输入
= toxdav
在源字段中。Just in case anyone is wanting to achieve the same thing.
I used a filter to create the column of items, then
TRANSPOSE(UNIQUE(FILTER(G:G,B:B=T7
for the rows of unit options next to each item.For the VBA I added the following to a module
Then in the data validation I entered
=toXDAV
in the source field.