[英]How do I remove words with less than 3 characters from a cell?
I'm trying to create a word cloud in Tableau but I need to prep the data in Excel first.我正在尝试在 Tableau 中创建一个词云,但我需要先准备 Excel 中的数据。 I already removed all characters that weren't alphanumeric.
我已经删除了所有不是字母数字的字符。 But now I want to remove the words like not, it, or is.
但现在我想删除诸如 not、it 或 is 之类的词。
I honestly just want to remove all the words with less than 3 characters.老实说,我只想删除所有少于 3 个字符的单词。 I tried the following two pieces of code using VBA.
我使用 VBA 尝试了以下两段代码。
Sub removeSmallWords()
ScreenUpdating = False
Dim rng As Range
Set rng = Range("A1:A10")
Dim oReg As Object
Set oReg = CreateObject("vbscript.regexp")
For Each cell In rng
With oReg
.Pattern = "(\s|^)(\w{1,2})(\s|$)"
.Global = True
cell.Value = .Replace(cell.Value, " ")
End With
cell.Value = Trim(cell.Value)
Next cell
Set oReg = Nothing
ScreenUpdating = True
End Sub
But this created an error that said Active X Can't create the component.但这产生了一个错误,指出 Active X 无法创建组件。
The second thing I tried was this:我尝试的第二件事是:
Sub removeSmallWords()
ScreenUpdating = False
Dim rng As Range
Set rng = Range("A1:A10")
Dim stringArray() As String
Dim newString As String
For Each cell In rng
newString = ""
stringArray = Split(cell.Text)
For i = 0 To UBound(stringArray)
If Len(stringArray(i)) > 3 Then
newString = newString & " " & stringArray(i)
End If
Next i
cell.Value = Trim(newString)
Next cell
ScreenUpdating = True
End Sub
This just doesn't do anything.这只是没有做任何事情。 I select a cell with only plain text.
我 select 一个只有纯文本的单元格。 I go to "Run Macro" and I select it and hit run.
我 go 到“运行宏”和我 select 它并点击运行。 Nothing happens.
什么都没发生。
What am I doing wrong?我究竟做错了什么?
Thanks!谢谢!
The second macro works on the range A1 through A10 , not a cell you have Selected
.第二个宏适用于A1到A10范围,而不是您
Selected
的单元格。
Also replace:同时替换:
If Len(stringArray(i)) > 3 Then
with:和:
If Len(stringArray(i)) > 2 Then
To retain words with 3 characters.保留3个字符的单词。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.