简体   繁体   中英

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

  2. the cell i can select is in col 'A' (GrossSales) and any row in the Revenue table; the selected cell then identifies '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]

  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']

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; and currently, the xls has been 'static', so the column is simply identified as "H". in the userform then, i get my specific value to calc using 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. 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

so the reference to [#Headers],[Sales] is not returning the equivalent of the old "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]]. it clearly isn't equal to H. i tried putting the reference into a MsgBox to display, but that failed as well.

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]. 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
  2. Me.txtSales = Intersect(Activecell.Entirerow, Activesheet.Listobjects("Table_Revenue").Listcolumns("Sales").Range).Value with dim txtSales Variant

and #2 worked right, with the space between "Net Sales". 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.

one last thing: ListObjects worked because it was a 'table'? 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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