gpt4 book ai didi

excel - 范围内的 VBA 字数

转载 作者:行者123 更新时间:2023-12-04 22:30:31 26 4
gpt4 key购买 nike

嗨,我是excel的初学者,所以请原谅我的无知。
最近我发现自己需要一个函数来计算一系列单元格中的单词数(当然,将空单元格计为 0)。
在网上冲浪我发现了这个简单的 VBA 代码:

Function intWordCount(rng As Range) As Integer
intWordCount = UBound(Split(Application.WorksheetFunction.Trim(rng.Value), " "), 1) + 1
End Function

我发现这非常有用,因为我可以使用
=intwordcount(A2)+intwordcount(B2)+intwordcount(C2)

在 excel 功能栏中,将 3 个单元格中包含的单词数相加,而不计算空的单词数。

问题是我现在需要在一列中的大量单元格上执行此操作,并且只需使用
=intwordcount(A2:A18)

不起作用。
我认为错误在于它尝试将函数应用于范围,将其视为单个大单元格,而我希望它将函数应用于范围内的每个单元格并对每个输出求和以获得总数。
如果有人可以帮助我,我将非常感激,因为我无法找到解决方案。
我正在使用 Excel 2016。
非常感谢你的协助。

最佳答案

循环遍历单元格并应用相同的逻辑。

该功能现在适用于单细胞范围和多细胞范围

Function intWordCount(rng As Range) As Integer

Dim MyCell as Range

For Each MyCell in rng
intWordCount = intWordCount + UBound(Split(Application.WorksheetFunction.Trim(MyCell.Value), " "), 1) + 1
Next MyCell

End Function

关于excel - 范围内的 VBA 字数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53210595/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com