Forum Discussion
Trying to embed special characters into referenced values
- Nov 23, 2022
AlokBeheria That work is certainly doable using a custom VBA function. In fact, I wrote two. Put either (or both) of the functions below into a standard code module (i.e., where macros are located). To reduce visual clutter, I used an ellipsis (...) in place of some of the text. Here are samples of the Excel formulas and results on some data I made up:
For the first function (AlokBeheria1), I wrote the code to do "exactly" the requested work:
Public Function AlokBeheria1(ByVal DelimitedList As String _ , Optional ByVal Delimiter As String = ",") As Variant ' This function parses a delimited list of entries, and wraps the ' second entry, plus all others, within another set of strings. Dim vntReturnValue As Variant Dim strReturnValue As String Dim strEntries() As String Dim in4UpperBound As Long Dim in4Index As Long Dim strRemainingEntriesAsList As String '---- Parse the delimited list into an array of strings. strEntries = Split(DelimitedList, Delimiter) in4UpperBound = UBound(strEntries) If in4UpperBound < 1 Then '...the list does not have two entries. vntReturnValue = CVErr(5) GoTo AB1_Exit End If '---- Construct the desired output. ' Create the first part. strReturnValue = "... -preferred " & strEntries(1) 'Entry #2 is _ referenced by index 1, because the array is zero-based. ' Create a new delimited list of entries with all but the ' second entry. strRemainingEntriesAsList = strEntries(0) For in4Index = 2 To in4UpperBound strRemainingEntriesAsList = strRemainingEntriesAsList _ & Delimiter & strEntries(in4Index) Next in4Index ' Put the pieces together. strReturnValue = strReturnValue & " -a " & strRemainingEntriesAsList ' vntReturnValue = strReturnValue AB1_Exit: '---- Return the result. AlokBeheria1 = vntReturnValue Exit Function End FunctionFor the function's argument, specify a cell containing the delimited list (or a string literal; e.g., =AlokBeheria1("A,B,C") ).
But that function has hard-coded values, so you would have to create a separate custom function for each different set of string literals for which you wanted to use the code. And it is always the second entry in the delimited list that gets special treatment.
Alternatively, you could use the second function (DelimitedListEntry) -- along with built-in Excel functions and operators -- for this purpose as well as for other purposes:
Public Function DelimitedListEntry(ByVal DelimitedList As String _ , ByVal EntryNum As Integer _ , Optional ByVal Delimiter As String = ",") As Variant ' This function parses a delimited list of entries, and returns ' either the entry whose ordinal position number is EntryNum (if ' EntryNum is positive) or a modified delimited list that excludes ' entry number EntryNum*-1 (if EntryNum is negative). Dim vntReturnValue As Variant Dim strReturnValue As String Dim strEntries() As String Dim in4UpperBound As Long Dim in4Index As Long Dim strRemainingEntriesAsList As String '---- Do some validation of the parameters. If EntryNum = 0 Then vntReturnValue = CVErr(5) GoTo DLE_Exit End If '---- Parse the delimited list into an array of strings. strEntries = Split(DelimitedList, Delimiter) in4UpperBound = UBound(strEntries) '---- If a single entry is requested, return it. If EntryNum > 0 Then If EntryNum - 1 <= in4UpperBound Then strReturnValue = strEntries(EntryNum - 1) vntReturnValue = strReturnValue Else vntReturnValue = CVErr(5) End If GoTo DLE_Exit End If '---- Create a new delimited list of entries with all but the ' specified entry. If EntryNum = -1 Then ' For improved performance, handle this case separately. strReturnValue = Mid$(DelimitedList, 1 + Len(strEntries(0)) _ + Len(Delimiter)) Else ' Create the first part. strRemainingEntriesAsList = strEntries(0) ' Append additional entries as appropriate. For in4Index = 1 To in4UpperBound If in4Index <> Abs(EntryNum) - 1 Then strRemainingEntriesAsList = strRemainingEntriesAsList _ & Delimiter & strEntries(in4Index) End If Next in4Index ' Put that into the String return value. strReturnValue = strRemainingEntriesAsList End If vntReturnValue = strReturnValue DLE_Exit: '---- Return the result. DelimitedListEntry = vntReturnValue Exit Function End FunctionFor the function's first argument, specify a cell with the delimited list (or a string literal). For the second argument, specify a positive number to return just a single entry whose (1-based) position is that number; or if you specify a negative number, the function returns the list excluding the single entry whose position is the absolute value of that number.
Note: For both functions, I allow a delimiter other than a comma to be specified. as the last argument.
I expected DelimitedListEntry to be much slower, as Excel has to make references to twice as many other cells, has to invoke the function twice, and has to do the concatenation itself. But the execution times were reasonably close for this and for AlokBeheria1.
Please find below my answer..
I realize it could be sheet_modify_First! but would like confirmation
>> Yes
What is an example of a value to be fetched?
>> Yes
you give "&sheet_modify_First!B8&" but that is not a complete formula
>> I am trying to add this into bunch of words using = "this is some text, and there are more texts, I also need this "&sheet_modify_First!B8&"" to translated like
this is some text, and there are more texts, I also need this ('%FSD_SCHEMA %').
And the special characters you are wanting to add before and after appear to be "%" -- but are there any others
>> Yes, correct. like I given the example above.
Regards
Would you please copy the actual formula you are attempting to use. You have twice now described it, but not actually shown it. It's impossible to know what's not working without seeing that which isn't working.
Or--even better--is it possible for you to post a copy of the actual workbook/spreadsheet on OneDrive or GoogleDrive, where I and others seeking to help you could see and work with it? Just make sure it contains no confidential information. And post a link to that workbook/spreadsheet here that grants us editing access.
- AlokBeheriaNov 09, 2022Brass Contributor
Hello Mathetes : I have taken the screen shot and copied here. The first screen from where it's picking up the name and in another sheet , it's replacing with the actual name. Here, I just want to display like '%FSD_SCHEMA%' and not FSD_SCHEMA. hope, this gives you more clarity.
Regards
- AlokBeheriaNov 11, 2022Brass Contributor
My Apologies for the delayed response. Thanks Mathetes for your inputs, Your suggestions worked like a charm. One last thing, I am still wondering as how to .work I know how to pull values from other sheet using referencing .. but what I need here to pick value which is separated by comma and use this to build customized command. like in below sheet I am trying to append values coming from different sheet. Like in below sheet , I want create command like srvctl add service -db $DBNAME -service "CTISA_PERM_01_LAC_L1_S1" -pdb "CTISA_PERM_01_LAC_L1" -preferred T0002082 -a T0002081,T0002083. The value enclosed in double quotes , I was mange to pull , but in the last lines , I need to pick values using "cluster names" column given in below screen shot and need to create command as shown above.
Regards
- mathetesNov 12, 2022Silver ContributorI'm sorry; I really am not able to follow what you're trying to do. The picture you have doesn't have headings, which is part of the issue, but it's also just the way you've described it. It would help with clarity if you could write your desires in separate bullet sentences or numbered sentences like I did in my very first reply to your first question.
- mathetesNov 09, 2022Silver Contributor
You are so close, I'm not sure why you haven't figured it out, unless I'm still not understanding what you really want.
This formula
="This is testing and I need schema to be displayed "&"%"&Sheet_Modify!B9&"%."
does what I think you want, unless you also want the single apostrophe to appear around the string,
in which case
="This is testing and I need schema to be displayed "&"'%"&Sheet_Modify!B9&"%'."
works