VBA code in 1 line

Formula11

Active Member
Joined
Mar 1, 2005
Messages
443
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
 
Rick, this is actually closer to the generic solution I was looking for. Code below works.
What is the function then not allowing the 1-line option? Is it For/Next or If/End If.
VBA Code:
Counter = 0: For i = 1 To Len(Item): Counter = Counter - IsNumeric(Mid(Item, i, 1)): Next
 
Upvote 0

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Yes... because If..Then has a single line version, there is no way to "end" it... if you try to use a colon, it just links the command after it to the statement attached to the Then keyword. Given that you only need to add 1 when the IsNumeric is True and 0 otherwise, my code line makes use of the fact that, in VBA, False is equivalent to zero and True is equivalent to minus one.
 
Upvote 0
What do you hope to gain from using a single line of code
it makes it difficult to read for future maintenance either for yourself or others
it doesn’t save much in storage
it doesn’t matter to vba whether it is single or multi line
 
Upvote 0
For me personally it makes it a lot easier to read.
More of the overall module can then be seen on the screen.
 
Upvote 0

Forum statistics

Threads
1,221,310
Messages
6,159,173
Members
451,543
Latest member
cesymcox

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