Ask Question Forum:
Model Library:2025-02-08 Updated:A.I. model is online for auto reply question page
C
O
M
P
U
T
E
R
2
8
Show
#
ASK
RECENT
←
- Underline
- Bold
- Italic
- Indent
- Step
- Bullet
- Quote
- Cut
- Copy
- Paste
- Table
- Spelling
- Find & Replace
- Undo
- Redo
- Link
- Attach
- Clear
- Code
Below area will not be traslated by Google,you can input code or other languages
Hint:If find spelling error, You need to correct it,1 by 1 or ignore it (code area won't be checked).
X-position of the mouse cursor
Y-position of the mouse cursor
Y-position of the mouse cursor
Testcursor
caretPos
Attachment:===
Asked by rberke
at 2024-07-12 01:58:24
Point:500 Replies:12 POST_ID:828960USER_ID:11861
Topic:
Microsoft Excel Spreadsheet Software;;
I thought this would work, but it doesn't. =countif(a1:a5,"*#*")
To demonstrate, put 999999 into a1 through a5 then make the column 1 character wide.
I have a workaround that uses vba, but it is very slow. It uses .cells.find (what:="#") and loops through the selected range.
I would much prefer an solution that uses an excel worksheet function.
To demonstrate, put 999999 into a1 through a5 then make the column 1 character wide.
I have a workaround that uses vba, but it is very slow. It uses .cells.find (what:="#") and loops through the selected range.
I would much prefer an solution that uses an excel worksheet function.
Author: rberke replied at 2024-07-19 02:16:11
Martin's vba code was simple and worked, so I gave him points. My own code is more complicated by much faster so I gave myself the best answer.
Duncan's attempt avoided VBA. so it would have been ideal. Unfortunately, it did not work if any of the numbers had decimals, commas or other formatting.
Duncan's attempt avoided VBA. so it would have been ideal. Unfortunately, it did not work if any of the numbers had decimals, commas or other formatting.
Author: rberke replied at 2024-07-15 09:01:54
I simplified my code, then did a little more testing. It turns out that Martin's code is faster than my code when more than 20% of the cells have ###, For instance, when I had 20000 cells which ALL had ###, My code took 5 times longer than Martin's. For my purposes, this rarely happens, so I will continue to use my own code. Nonetheless, Martin's might be "better" for other people.
Sub CountPoundSigns()
Dim time As Double
time = Now() ' AccurateNow() ' < my personal.xls has an AccurateNow function which is far more accurate than Now().
Dim cell As Object, cellA As Range, cnt As Long
With ActiveSheet.UsedRange.Columns(1)
Set cellA = .find(what:="#", LookIn:=xlValues, LookAt:=xlPart, _
matchcase:=False, SearchOrder:=xlByRows)
If Not cellA Is Nothing Then
Set cell = cellA
Do
cnt = cnt + 1
Set cell = .FindNext(cell)
Loop While cell.Address <> cellA.Address
End If
End With
MsgBox cnt & " " & (Now()- time) * 24 * 60 * 60
End Sub
Sub CountPoundSigns()
Dim time As Double
time = Now() ' AccurateNow() ' < my personal.xls has an AccurateNow function which is far more accurate than Now().
Dim cell As Object, cellA As Range, cnt As Long
With ActiveSheet.UsedRange.Columns(1)
Set cellA = .find(what:="#", LookIn:=xlValues, LookAt:=xlPart, _
matchcase:=False, SearchOrder:=xlByRows)
If Not cellA Is Nothing Then
Set cell = cellA
Do
cnt = cnt + 1
Set cell = .FindNext(cell)
Loop While cell.Address <> cellA.Address
End If
End With
MsgBox cnt & " " & (Now()- time) * 24 * 60 * 60
End Sub
Assisted Solution
Author: rberke replied at 2024-07-14 13:55:19
On my machine your code took 6 seconds for 20000 rows.
my code took less than a second.
When I originally posted this question, I had not actually implemented my workaround, so I did not realize how fast it would be. It turns out it is blazing fast, so I have actually accomplished what I wanted.
I will close the question.
my code took less than a second.
When I originally posted this question, I had not actually implemented my workaround, so I did not realize how fast it would be. It turns out it is blazing fast, so I have actually accomplished what I wanted.
I will close the question.
Expert: duncanb7 replied at 2024-07-14 10:36:12
Doing it on Excel cell that is not easy one for your case even I tried it to closer one. I suggest you
to do it on VBA to pick easier way
Have a nice day , talk your tomorrow
Duncan
to do it on VBA to pick easier way
Have a nice day , talk your tomorrow
Duncan
Expert: duncanb7 replied at 2024-07-14 10:28:44
I tried that works at my side no any issue at all
Just open a new sheet to test those suggestion from the posts
Duncan
Just open a new sheet to test those suggestion from the posts
Duncan
Expert: MartinLiss replied at 2024-07-14 10:28:10
Martin, the goal was something faster than my workaround. Your solution is simpler but a LOT slower.
Mine took 1.5 seconds for 100,000 records. Out of curiosity how long did your's take? BTW if your interested I have an article on timing code if you want an accurate number. Author: rberke replied at 2024-07-14 10:25:49
It cannot work reliably. Demonstrate yourself. Put 123456 into cell a1 and a2. Format a1 as $#,##0.00 and a2 as #0
You will see a1 displays ### and a2 displays 123456. But then both have len = 6
You will see a1 displays ### and a2 displays 123456. But then both have len = 6
Expert: duncanb7 replied at 2024-07-14 10:08:11
so len() is not related to the cell formated or not, just
make simple example on a new sheet and test my code for Excel cell before
concluding it
Duncan
make simple example on a new sheet and test my code for Excel cell before
concluding it
Duncan
Expert: duncanb7 replied at 2024-07-14 10:04:52
Please let us know what you want to let it work on Excel cell formula or VBA ?
it seems VBA is better, and the speed is not such slow
it seems VBA is better, and the speed is not such slow
Accepted Solution
Author: rberke replied at 2024-07-14 10:03:00
Duncan: I should have mentioned the cells are formatted, so Len function will not work at all.
Martin, the goal was something faster than my workaround. Your solution is simpler but a LOT slower.
I suspect that there is no solution that is better than mine, but I will leave the problem open for a while longer in hopes of another approach.
rberke
Sub CountPoundSigns()
Dim rng As Range
Dim cell As Object, firstaddress As String, fnd As Range
With ActiveSheet.UsedRange.Columns(1)
Set cell = .Find(what:="#", LookIn:=xlValues, LookAt:=xlPart, _
MatchCase:=False, SearchOrder:=xlByRows)
If Not cell Is Nothing Then
firstaddress = cell.Address
Do
If fnd Is Nothing Then
Set fnd = cell
Else
Set fnd = Union(fnd, cell)
End If
Set cell = .FindNext(cell)
Loop While cell.Address <> firstaddress
End If
End With
MsgBox fnd.Cells.Count
Exit Sub
End Sub
Martin, the goal was something faster than my workaround. Your solution is simpler but a LOT slower.
I suspect that there is no solution that is better than mine, but I will leave the problem open for a while longer in hopes of another approach.
rberke
Sub CountPoundSigns()
Dim rng As Range
Dim cell As Object, firstaddress As String, fnd As Range
With ActiveSheet.UsedRange.Columns(1)
Set cell = .Find(what:="#", LookIn:=xlValues, LookAt:=xlPart, _
MatchCase:=False, SearchOrder:=xlByRows)
If Not cell Is Nothing Then
firstaddress = cell.Address
Do
If fnd Is Nothing Then
Set fnd = cell
Else
Set fnd = Union(fnd, cell)
End If
Set cell = .FindNext(cell)
Loop While cell.Address <> firstaddress
End If
End With
MsgBox fnd.Cells.Count
Exit Sub
End Sub
Assisted Solution
Expert: MartinLiss replied at 2024-07-13 09:17:56
500 points EXCELLENT
You can't use worksheet functions because they all look at the stored value of the cells and not at their appearance.
With 100,000 rows this takes only 1.5 seconds to run.
With 100,000 rows this takes only 1.5 seconds to run.
Sub CountPoundSigns()Dim lngCount As LongDim lngRow As LongFor lngRow = 1 To ActiveSheet.UsedRange.Rows.Count If InStr(1, Cells(lngRow, 1).Text, "#") Then lngCount = lngCount + 1 End IfNextMsgBox lngCountEnd Sub 1:2:3:4:5:6:7:8:9:10:11:
Expert: duncanb7 replied at 2024-07-12 03:59:58
It may be better doing on VBA
For Excel cell,I try to compare the length of cell and cell width to solve the issue that is what I can do but that is not good one if narrow width is equal to one character, If length of cell is greater than cell width, it means the cell has pound sign(#).
please try it as follows for at least narrow width=2.For narrow width=1, it will work sometimes and
sometimes not. So you need to work around for this
=SUMPRODUCT(--(LEN(A1:A5)>CELL("width",A1)))
Be reminded need to do automatic update cell formula for refresh cell if the columnwidth is changed by Excel: tools->options.caluation->calcuation->automatic
Hope understand your question completely.If not, please point it out
Duncan
For Excel cell,I try to compare the length of cell and cell width to solve the issue that is what I can do but that is not good one if narrow width is equal to one character, If length of cell is greater than cell width, it means the cell has pound sign(#).
please try it as follows for at least narrow width=2.For narrow width=1, it will work sometimes and
sometimes not. So you need to work around for this
=SUMPRODUCT(--(LEN(A1:A5)>CELL("width",A1)))
Be reminded need to do automatic update cell formula for refresh cell if the columnwidth is changed by Excel: tools->options.caluation->calcuation->automatic
Hope understand your question completely.If not, please point it out
Duncan