选择具有列名称的表范围

huangapple go评论55阅读模式
英文:

Select a table range with the name of the columns

问题

I need to select a range (multiple columns) inside a table, and to do so, I use the name of the column. The table may change in size, so I thought this was an easy way to do it.

Dim first_col_index As Integer
first_col_index = table_roses.ListColumns("Total").Index
ws_roses.Range(table_roses & "[[" & table_roses.ListColumns(first_col_index + 1) & "]:[" & table_roses.ListColumns(table_roses.ListColumns.Count) & "]]").Select
Application.Selection.Clear

This way might not be the best or the most elegant, but it works.

My problem is that sometimes table_roses.ListColumns(first_col_index + 1) and/or table_roses.ListColumns(table_roses.ListColumns.Count) contain the characters "[" and "]". For example, right now my last column is equal to "Collins, Sacha [2]".

Normally I would just add a backtick to solve the issue like so: =SOUS.TOTAL(9;table_roses[Caron, Luc '[2']]) but now it's inside a variable...

I understand my issue, but I don't know how to solve it without changing the logic.

I could add a column before selecting the range, clear it, and then remove that column, but I wanted to know if there is a more elegant way to do it!

(Keep in mind that I'm still new to VBA)

Thank you in advance 选择具有列名称的表范围

英文:

I need to select a range (multiple column) inside a table and to do so, I use the name of the column. The table may change in size so I though this was an easy way to do it.

Dim first_col_index As Integer

first_col_index = table_roses.ListColumns("Total").Index

ws_roses.Range(table_roses & "[[" & table_roses.ListColumns(first_col_index + 1) & "]:[" & table_roses.ListColumns(table_roses.ListColumns.Count) & "]]").Select

Application.Selection.Clear

This way might not be the best or the most elegant but it works.

My problem is that sometimes table_roses.ListColumns(first_col_index + 1) and/or table_roses.ListColumns(table_roses.ListColumns.Count) contain the caracter [ and ]. For exemple, right now my last column is equal to "Collins, Sacha [2]".

Normaly I would just add a backtick to solve the issue like so: =SOUS.TOTAL(9;table_roses[Caron, Luc '[2']]) but now it's inside a variable...

I understand my issue but I dont know how to solve it without changing the logic.

I could add a column before selecting the range, clear it and the remove that column, but I wanted to know if there is a more elegant way to do it!

(Keep in mind that I'm still new to VBA)

Thank you in advance 选择具有列名称的表范围

答案1

得分: 2

A bit cleaner:

Dim table_roses As ListObject

Set table_roses = ActiveSheet.ListObjects(1)

'从“Total”选择到表格末尾
With table_roses.ListColumns("Total")
   .Range.Resize(, 1 + table_roses.ListColumns.Count - .Index).Select
End With
英文:

A bit cleaner:

Dim table_roses As ListObject

Set table_roses = ActiveSheet.ListObjects(1)
    
'select from "Total" to the end of the table
With table_roses.ListColumns("Total")
   .Range.Resize(, 1 + table_roses.ListColumns.Count - .Index).Select
End With

huangapple
  • 本文由 发表于 2023年4月20日 00:07:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76056670.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定