如何在Excel文件中获取某列中某行的最长文本?使用公式或VBA都可以。

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

How do I get the longest text of a row in some column in the middle of an Excel file? Formulas or VBA are okay

问题

I have an excel file in the column somewhere in the middle of which (yellow selected row for example) I need to find the longest text of that row, and do that for each row in that column.

I obviously get a circle reference if I simply select the whole row as the range, and what I tried with VBA making two ranges out of that gets over-complicated quickly, making me think there must be an easier way, potentially even using a formula.

I have to follow the following rules due to requirements for the file:

  • I can only use that column (yellow)
  • I don't know per se how long the row is going to go on, something I can easily check via VBA
  • the leftmost column has to be excluded

Is there an easy way? Any ideas?

I tried simply selecting the row, obviously leads to a circle reference.
I've tried defining arrays via formula to include the two ranges, that didn't quite work out.
I've begun writing VBA code, but trying to combine two Ranges of that row, excluding a cell in-between is where it falls flat.

英文:

如何在Excel文件中获取某列中某行的最长文本?使用公式或VBA都可以。

I have an excel file in the column somewhere in the middle of which (yellow selected row for example) I need to find the longest text of that row, and do that for each row in that column.

I obviously get a circle reference if I simply select the whole row as the range, and what I tried with VBA making two ranges out of that gets over-complicated quickly, making me think there must be an easier way, potentially even using a formula.

I have to follow the following rules due to requirements for the file:

  • I can only use that column (yellow)
  • I don't know per se how long the row is going to go on, something I can easily check via VBA
  • the leftmost column has to be excluded

Is there an easy way? Any ideas?

I tried simply selecting the row, obviously leads to a circle reference.
I've tried defining arrays via formula to include the two ranges, that didn't quite work out.
I've begun writing VBA code, but trying to combine two Ranges of that row, excluding a cell in-between is where it falls flat.

答案1

得分: 3

这是一个选项:

如何在Excel文件中获取某列中某行的最长文本?使用公式或VBA都可以。

单元格 E2 中的公式:

=BYROW(2:7,LAMBDA(x,LET(y,DROP(FILTER(x,COLUMN(x)<>COLUMN()),,1),@SORTBY(y,LEN(y),-1))))

英文:

Here is one option:

如何在Excel文件中获取某列中某行的最长文本?使用公式或VBA都可以。

Formula in E2:

=BYROW(2:7,LAMBDA(x,LET(y,DROP(FILTER(x,COLUMN(x)<>COLUMN()),,1),@SORTBY(y,LEN(y),-1))))

答案2

得分: 0

"The "brute force" technique is to access an additional dimension by inserting a new Sheet.<br> In that new Sheet2, in B2, have =LEN(Sheet1!B2) and drag across and down.<br> That has given you an easier point from which to find what it is that you want.<br> Even if the data covered every single cell in Sheet1, and so in Sheet2, you could add Sheet3, and in B1 put E.g. =MAX(Sheet2!B:B) and in B2 =IF(Sheet2!B2=Sheet3!$B$1,ROW(),&quot;&quot;)<br> Then on Sheet4, in B2, =OFFSET(Sheet1!B1,SUM(Sheet3!B2:B1048576)-1,0)<br> In practice you probably do have some spare rows/columns (can you add a row at the top of the source sheet?) so you can probably put the answer on Sheet3.<br> The key is to separate the different steps: finding the length; finding the largest length, finding in which cell that occurs, and accessing the value in that cell."

英文:

The "brute force" technique is to access an additional dimension by inserting a new Sheet.<br>
In that new Sheet2, in B2, have =LEN(Sheet1!B2) and drag across and down.<br>
That has given you an easier point from which to find what it is that you want.<br>
Even if the data covered every single cell in Sheet1, and so in Sheet2, you could add Sheet3, and in B1 put E.g. =MAX(Sheet2!B:B) and in B2 =IF(Sheet2!B2=Sheet3!$B$1,ROW(),&quot;&quot;)<br>
Then on Sheet4, in B2, =OFFSET(Sheet1!B1,SUM(Sheet3!B2:B1048576)-1,0)<br>

In practice you probably do have some spare rows/columns (can you add a row at the top of the source sheet?) so you can probably put the answer on Sheet3.<br>
The key is to separate the different steps: finding the length; finding the largest length, finding in which cell that occurs, and accessing the value in that cell.

huangapple
  • 本文由 发表于 2023年3月3日 21:37:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75627782.html
匿名

发表评论

匿名网友

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

确定