+ Reply to Thread
Results 1 to 5 of 5

VBA function: Insert character every X special character

  1. #1
    Registered User
    Join Date
    05-26-2020
    Location
    Odense, Denmark
    MS-Off Ver
    2020 - 365
    Posts
    31

    VBA function: Insert character every X special character

    Hi Forum,

    I've searched the web for help but without success. I'm searching for a VBA function that inserts a "space + a period ( .)" every 3rd period (.).

    The string can vary in lenght and therefore also the occurrence of periods. So the solution has to fit a dynamic string.

    As an example the text could be as follows:
    1. .xxx.xxx.xx.xx.xxxx.xxxx.xx.xxx.xxx.xxxx.
    2. .xxxx.xx.xxxx.xxxx.xxx.xxx.xx.


    Where i want the result as:
    1. .xxx.xxx. .xx.xx. .xxxx.xxxx. .xx.xxx. .xxx.xxxx.
    2. .xxxx.xx. .xxxx.xxxx. .xxx.xxx. .xx.


    How can I achieve that?
    Last edited by Ralleberg; 02-04-2022 at 08:48 AM.

  2. #2
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,460

    Re: VBA function: Insert character every X special character

    If the strings are always the same length, then this formula would do it (I know it's not VBA!):

    =SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(F2,".",". .",3),".",". .",6),".",". .",9),".",". .",12)

    You may need to replace commas with semi-colons for your locale.

    AliGW on MS365 Insider (Windows) 64 bit

    F
    2
    .xxx.xxx.xx.xx.xxxx.xxxx.xx.xxx.xxx.xxxx.
    3
    .xxx.xxx. .xx.xx. .xxxx.xxxx. .xx.xxx. .xxx.xxxx.
    Sheet: CSVUserList
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    Forum Rules (updated August 2023): please read them here.

  3. #3
    Registered User
    Join Date
    05-26-2020
    Location
    Odense, Denmark
    MS-Off Ver
    2020 - 365
    Posts
    31

    Re: VBA function: Insert character every X special character

    Hi Ali,


    Sorry I was a little fuzzy in the first post. The strings can vary in length and the occurrence of periods can therefore also vary.

    I've previous tried with the SUBSTITUTE function as you presented and another solution with Excel function, but none is doing the job if the string varies in lenth.

    I'll add the info to my initial post.
    Last edited by AliGW; 02-04-2022 at 08:30 AM. Reason: PLEASE don't quote unnecessarily!

  4. #4
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2404 (Windows 11 22H2 64-bit)
    Posts
    80,460

    Re: VBA function: Insert character every X special character

    I suggest you provide a sample workbook with at least 10-15 worked examples shpowing the variety you have just disclosed.

  5. #5
    Forum Expert XLent's Avatar
    Join Date
    10-13-2010
    Location
    Northumberland, UK
    MS-Off Ver
    various
    Posts
    2,704

    Re: VBA function: Insert character every X special character

    based on samples, the below would replicate your expected results (where you seem to be including previously inserted periods as part of the rolling 3-count)
    Please Login or Register  to view this content.
    so:
    Formula: copy to clipboard
    Please Login or Register  to view this content.

+ 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] vba to to insert special symbol-character in excel text sentence?
    By lilsnoop in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-02-2021, 02:37 PM
  2. replace function for special character in macro
    By sameeramavat in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-31-2020, 03:34 AM
  3. insert special character in an empty cell
    By xam99 in forum Excel General
    Replies: 2
    Last Post: 02-12-2020, 03:28 PM
  4. Insert command button and look for specific special character
    By Justin27 in forum Excel Formulas & Functions
    Replies: 0
    Last Post: 09-02-2013, 10:04 AM
  5. Need to highlight special character and the cells contains special character
    By dhilipan in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-26-2013, 01:36 AM
  6. Replies: 3
    Last Post: 11-08-2012, 05:01 PM
  7. Insert a special character before and after a word
    By Venkatesh V in forum Excel General
    Replies: 1
    Last Post: 02-21-2005, 11:06 AM

Tags for this Thread

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