I am not very familiar with VBA in Excel. I need the VBA Function text value to return regardless of character limit (if a limit is required, 560 should suffice). I have read a lot of forum posts regarding the 255 character limit and wrapping text or using different strings etc, but I don't really understand how to implement that into the code I am using below.
The situation: I have a bunch of documents (paper copies) each day which each have a 7-digit numerical ID. This is always 7 numerical digits long. I type the values without spaces or commas into cell B4, and then in cell B5 I am calling the function via =InsertChar(B4) which will separate each document ID (every 7 digits) with a comma. The below code I have copied from online does the job perfectly, where every 7 digits it will insert a comma, until cell B5 exceeds 255 characters in which case it will return "#VALUE!".
I tried formatting the cells and adding some wraptext code (which did wrap the text), but as soon as cell B5 exceeded 255 characters, it still returned "#VALUE!" instead of the numbers & commas as text.
I would be very grateful if somebody could assist. Seems like a simple fix but I am a complete newbie to VBA and don't even understand half of the code I have used. :)
NB: Office Excel version 2016 and 365.
Cell B4 (dummy data typed in manually without commas):
1187226118437011865811187335118756231187659911875423119155451191554511915546119155471191568611915687118722611843701186581118733511875623118765991187542311915545119155451191554611915547119156861191568711915686119156871
VBA Code:
Option Explicit
Function InsertChar(STR As String, Optional sInsertCharacter As String = ",", Optional lSpacing As Long = 7) As String
Dim sCharString As String
Dim sFormatString As String
Dim sTemp As String
Dim I As Long
For I = 1 To lSpacing
sCharString = sCharString & "&"
Next I
sCharString = sCharString & sInsertCharacter
For I = 0 To Len(STR) \ lSpacing
sFormatString = sFormatString & sCharString
Next I
sFormatString = "!" & Left(sFormatString, Len(sFormatString) - 1)
sTemp = Format(STR, sFormatString)
If Right(sTemp, 1) = "," Then sTemp = Left(sTemp, Len(sTemp) - 1)
InsertChar = sTemp
End Function
Cell B5 (correct result of the VBA function when less than 255 characters):
1187226,1184370,1186581,1187335,1187562,3118765,9911875,4231191,5545119,1554511,9155461,1915547,1191568,1191568,1187226,1184370,1186581,1187335,1187562,1187659,1187542,1191554,1191555,1191554,1191554,1191568,1191568,1191568,1191568,1191456,1191621
Cell B5 (incorrect result of the VBA function when more than 255 characters):
#VALUE!