简体   繁体   English

Excel VBA中的结构化引用:在范围中使用“ #Headers [col-name]”

[英]structured reference in excel vba: using '#Headers [col-name]' in Range

folks, not quite sure how to present this properly. 伙计们,不太确定如何正确呈现。 so bear with with me: 所以和我一起忍受:

  1. i have an excel vba userform, and would like to do a calc inside the userform, retrieving data from a cell in an xls worksheet 我有一个excel vba用户窗体,并且想在用户窗体内进行计算,从xls工作表中的单元格检索数据

  2. the cell i can select is in col 'A' (GrossSales) and any row in the Revenue table; 我可以选择的单元格位于“ A”列(GrossSales)和“收入”表中的任何行; the selected cell then identifies 'active cell.row'. 然后,所选单元格将标识“ active cell.row”。 NOW, the column i want for specific data is defined elsewhere in a different column - the column could be found by #Headers[Sales] 现在,我要用于特定数据的列在其他列的其他位置定义-该列可以通过#Headers [Sales]找到

  3. since the worksheet is in development, i keep moving columns around; 由于工作表正在开发中,因此我会不断移动专栏文章; so i would like to use a 'dynamic' reference to the name of the column, rather than a 'static' name using column letter [like col 'H'] 所以我想使用“动态”引用列名,而不是使用“静态”列名[像col'H']

so, how it works, i click on a cell in col 'A' in a worksheet, and the userform gets me the active row: ActiveCell.Row; 因此,它是如何工作的,我单击了工作表中“ A”栏的一个单元格,用户窗体为我提供了活动行:ActiveCell.Row; and currently, the xls has been 'static', so the column is simply identified as "H". 并且目前,xl​​s处于“静态”状态,因此该列仅被标识为“ H”。 in the userform then, i get my specific value to calc using Me.Sales.Value = Range("H" & ActiveCell.Row).Value 然后在用户窗体中,我使用Me.Sales.Value = Range(“ H”&ActiveCell.Row).Value获取要计算的特定值

what i need to is to replace "H" with a header row 'name' so if i move columns this link still works. 我需要用标题行“ name”替换“ H”,所以如果我移动列,此链接仍然有效。 i tried this: Me.txtSales.Value = Range("Table_Revenue[[#Headers],[Sales]]" & ActiveCell.Row).Value and i get this error: ..run time error 1004.. ..Method 'Range' of object '_Global' failed 我尝试了这个:Me.txtSales.Value = Range(“ Table_Revenue [[#Headers],[Sales]]”&ActiveCell.Row).Value,我得到了这个错误:..运行时错误1004 .. ..Method'对象“ _Global”的范围“失败”

so the reference to [#Headers],[Sales] is not returning the equivalent of the old "H" 因此,对[#Headers],[Sales]的引用未返回旧的“ H”

can anyone tell me what i am missing here? 谁能告诉我我在这里想念的吗? probably my lack of understanding of what is 'inside' the reference Table_Revenue[[#Headers],[Sales]]. 可能是我对参考Table_Revenue [[#Headers],[Sales]]的“内部”缺乏理解。 it clearly isn't equal to H. i tried putting the reference into a MsgBox to display, but that failed as well. 它显然不等于H。我尝试将引用放入MsgBox中进行显示,但同样失败。

thanks, ron 谢谢,罗恩

我建议您改用ListObject

Me.txtSales.Value = Intersect(Activecell.Entirerow, Activesheet.Listobjects("Table_Revenue").Listcolumns("Sales").Range).Value 

rory, u are correct. 罗里,你是正确的。 i was experimenting to understand range & value [i am still very wet behind the ears w/excel & vba]. 我正在尝试了解范围和价值[我仍然很潮湿,耳朵里有excel和vba]。 i tried these things: 我尝试了这些事情:

  1. Me.txtSales.Value = Intersect(Activecell.Entirerow, Activesheet.Listobjects("Table_Revenue").Listcolumns("Sales").Range) with dim txtSales Variant and then with dim txtSales Range Me.txtSales.Value = Intersect(Activecell.Entirerow,Activesheet.Listobjects(“ Table_Revenue”)。Listcolumns(“ Sales”)。Range)使用txtSales变体变暗,然后使用txtSales范围变暗
  2. Me.txtSales = Intersect(Activecell.Entirerow, Activesheet.Listobjects("Table_Revenue").Listcolumns("Sales").Range).Value with dim txtSales Variant Me.txtSales = Intersect(Activecell.Entirerow,Activesheet.Listobjects(“ Table_Revenue”)。Listcolumns(“ Sales”)。Range)。带有暗淡txtSales变量的值

and #2 worked right, with the space between "Net Sales". 和#2运作正常,在“净销售额”之间留有间隔。 so, as you said - it should work and it did. 因此,正如您所说-它应该起作用并且确实起作用。 what i think i found as well was that this version: Range("Table_Revenue[[#Headers],[Sales]]" & ActiveCell.Row).Value was commented as only working in excel, and not in vba. 我认为我也发现该版本:Range(“ Table_Revenue [[#Headers],[Sales]]”&ActiveCell.Row).Value被评论为仅在excel中工作,而在vba中不工作。

one last thing: ListObjects worked because it was a 'table'? 最后一件事:ListObjects之所以起作用是因为它是一个“表”? if it wasn't a table, but defined as a 'range', would it have worked? 如果它不是表,而是定义为“范围”,它会起作用吗?

anyway, i learned a lot - so thankyou very much for the solution, but thanks more for the learning experience. 无论如何,我学到了很多东西-非常感谢您的解决方案,但也感谢您的学习经验。

ron 罗恩

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

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