VBA code in 1 line

Formula11

Active Member
Joined
Mar 1, 2005
Messages
468
Office Version
  1. 365
Platform
  1. Windows
How can you place the following VBA code in 1 line.
Tried using semi-colon ":" but does not work.

VBA Code:
Counter = 0
For i = 1 To Len(Item)
   If IsNumeric(Mid(Item, i, 1)) = True Then
    Counter = Counter + 1
   End If
Next i

VBA Code:
Counter = 0: For i = 1 To Len(Item): If IsNumeric(Mid(Item, i, 1)) = True Then: Counter = Counter + 1: End If: Next i
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
remove the colon after THEN and try it
 
Upvote 0
I think the use of "For ... Next" is a problem here, and this approach won't work. However ... if you turn your multi-line version into a function, like so:
VBA Code:
Function NumCount(ByVal strToCheck As String) As Integer
    Dim intNdx As Integer
    NumCount = 0
    For intNdx = 1 To Len(strToCheck)
        If IsNumeric(Mid(strToCheck, intNdx, 1)) Then
            NumCount = NumCount + 1
        End If
    Next intNdx
End Function
then you use a single-line piece of code like so:
VBA Code:
counter = NumCount(item)
 
Upvote 0
CephasOz, thanks for confirming.
I'm sure the function would work but I wanted to apply 1-line code in general everywhere.
 
Upvote 0
If you really want a one-liner, how about this one...
VBA Code:
Counter = Evaluate("SUM(0+ISNUMBER(-MID(""" & Item & """,ROW(1:" & Len(Item) & "),1)))")
Note: This single line of code would replace all of the code you posted as there is no need to initialize the Counter variable to zero beforehand.
 
Upvote 0
Rick, thanks this seems to work. It looks like it's for a cell formula though. Surprising.
Would have been good if there was a generic way to use in all instances with For/Next statements.
 
Upvote 0
It is VBA code, but it is using the Evaluate function which, among other things, evaluates Excel formulas. So my code constructs a text string argument for the Evaluate function that is an Excel formula that counts digits in a text string and then executes it and assigns the answer to the Counter variable. No actual formulas are placed on the Excel worksheet though... everything happens in the computers memory.
 
Upvote 0
Would have been good if there was a generic way to use in all instances with For/Next statements.
How about 2 lines :
VBA Code:
For i = 1 To Len(Item): If IsNumeric(Mid(Item, i, 1)) = True Then counter = counter + 1
Next i
 
Upvote 0
@footoo,

You can still do the For..Next in one line of code by eliminating the If..Then statement...
VBA Code:
For i = 1 To Len(Item): Counter = Counter - IsNumeric(Mid(Item, i, 1)): Next
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top