I want to be able to the following two actions; I know the Excel Formulas but I'm hoping there is a way to program this w/ VBA.
- I'm using the excel formula to perform a word count for each cell with the output in Column B:
=LEN(TRIM($A2))-LEN(SUBSTITUTE($A2," ",""))+1
Cells in Column A contain free-form text, ranging from 500-2,500 words and the deliminator is spaces. The number of cells with text varies from workbook to workbook.
So far I've tried entering the following code with no luck. I receive a type mismatch
Code:
Dim lastRow As Long
With Sheets("Sheet1")
lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
With .Range("B2:B" & lastRow).Formula = "=IF(LEN(TRIM(A2))=0,0,LEN(TRIM(A1))-LEN(SUBSTITUTE(A2,"" "",""""))+1)"
.Value = .Value
End With
End With
- The second step I'm trying to do is searching the column header (Columns C through BZ) and counting how many times that word appears in each cell.
=(LEN($A2)-LEN(SUBSTITUTE(UPPER($A2),UPPER(C$2),"")))/LEN(C$1)
Unlike the problem above I've not been able to start a script.
counting words in a string [excel]
. You'll find plenty of examples on stackoverflow. There will also be examples for counting cells in a string if they appear in a range. That should get you started. Then, if you have problems, post back by editing your question. Please read the HELP pages for information as to What topics can I ask about here?, How to Ask a Good Question, and How to create a Minimal, Complete, and Verifiable example – Ron Rosenfeld