+ Reply to Thread
Results 1 to 10 of 10

Count characters selected in code in the VBA editor

  1. #1
    Registered User
    Join Date
    06-06-2014
    Location
    Hong Kong
    MS-Off Ver
    2010
    Posts
    91

    Count characters selected in code in the VBA editor

    Hi there,

    Can't google this because all it gives is a formula to count characters in excel which is not what I'm looking for.
    I need to count the actual number of highlighted characters in a vba formula because the Evaluate function only takes an archaic 255 characters.
    Is there any option to turn this on in the code editor?

    Thanks.
    Last edited by 6StringJazzer; 07-07-2020 at 08:02 AM. Reason: clarified title

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    27,019

    Re: Count characters selected in code in the VBA editor

    I don't know of a way to do this directly in the VBA editor. The Standard toolbar has a box that shows the line number and column number of the cursor, so if your formula is on one line you can get the beginning and ending column numbers and subtract.

    A workaround is to do it in Word. You can copy the formula and paste into Word. Right click on the status bar and select Character Count. The count of the selected characters appears in the status bar.
    Jeff
    | | |·| |·| |·| |·| | |:| | |·| |·|
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Registered User
    Join Date
    06-06-2014
    Location
    Hong Kong
    MS-Off Ver
    2010
    Posts
    91

    Re: Count characters selected in code in the VBA editor

    A shame but thanks for responding. I was hoping I didn't have to go through the cut and paste into word :P
    I could of course hope for Microsoft to remove the 255 cap.....

    Maybe a better chance of buying a winning lottery ticket...

  4. #4
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Count characters selected in code in the VBA editor

    Hi,

    You can 'cut and paste' into the Immediate Window to get the length.

    For example try the following:
    a. Open the Immediate Window (Ctrl G)
    b. Select the text.
    c. Type the following in the Immediate Window: s = "
    d. Paste the selected text after the 'double quote". A final double quote will automatically be added.
    e. In the Immediate Window type:
    ?len(s) followed by the Enter key

    Lewis

  5. #5
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,021

    Re: Count characters selected in code in the VBA editor

    You can use code like this (assumes you've only selected text on one line):

    Please Login or Register  to view this content.
    Everyone who confuses correlation and causation ends up dead.

  6. #6
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Count characters selected in code in the VBA editor

    I believe there are 2 prerequisite' for rorya's code to run:

    a. In Excel Trust Center, CheckBox 'Trust access to the VBA project access model' MUST be selected.

    b. In VBA > Tools > References > Enable 'Microsoft Visual Basic For Applications Extensibility 5.3'

    Lewis

  7. #7
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,021

    Re: Count characters selected in code in the VBA editor

    a) is correct (I should have mentioned that, so thanks!) but there is no need for the VBA extensibility reference.

  8. #8
    Valued Forum Contributor
    Join Date
    02-14-2019
    Location
    Turkiye
    MS-Off Ver
    2010 - 64 Bit on Windows-11 (24 H2) 64 Bit
    Posts
    1,187

    Re: Count characters selected in code in the VBA editor

    Based on Rorya's code in message #5, maybe this;

    Note: As stated in message #6; in Excel Trust Center "Trust access to the VBA project access model" must be selected and the reference "Microsoft Visual Basic For Applications Extensibility 5.3" must be added to the project.

    Please Login or Register  to view this content.
    Last edited by Haluk; 07-15-2020 at 03:23 AM. Reason: improvement of code...

  9. #9
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    27,019

    Re: Count characters selected in code in the VBA editor

    Quote Originally Posted by rorya View Post
    You can use code like this (assumes you've only selected text on one line):
    From now on I am never going to say there is no way to do something no matter how certain I am or no matter how bizarre it sounds.

  10. #10
    Registered User
    Join Date
    06-06-2014
    Location
    Hong Kong
    MS-Off Ver
    2010
    Posts
    91

    Re: Count characters selected in code in the VBA editor

    Thanks for the ideas, I'll take a look at it thanks

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Userform, count selected characters
    By Un-Do Re-Do in forum Excel Programming / VBA / Macros
    Replies: 11
    Last Post: 09-30-2019, 08:57 PM
  2. [SOLVED] Weighted count of selected characters in a textbox
    By Un-Do Re-Do in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-23-2018, 01:50 AM
  3. [SOLVED] How to export code from a text editor in the main workbook to a text editor for many files
    By Feisty2 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 04-17-2018, 01:36 PM
  4. [SOLVED] Writing special characters in Visual Basic Editor
    By Un-Do Re-Do in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-27-2018, 09:45 AM
  5. code that will count the number of items selected in a slicer on a pivot table
    By asmi in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-11-2017, 12:57 AM
  6. [SOLVED] Vba code to count the number of characters from a cell and to do an action
    By IonutC in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 07-04-2016, 05:50 AM
  7. Replies: 2
    Last Post: 02-09-2016, 11:00 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1