Word Macro for separating a comma-separated list to columns - vba

I have a very large set of data that represents cartesian coordinates in the form x0,y0,z0,x1,y1,z1...xn,yn,zn. I need to create a new line at the end of each xyz coordinate. I have been trying to record a macro that moves a certain number of spaces from the beginning of each line, then creates a new line. This, of course, will not work since the number of digits in each xyz coordinate differs.
How can I create a macro to do this in Microsoft Word?

Try this:
Public Sub test()
Dim s As String
Dim v As Variant
Dim t As String
Dim I As Long
s = "x0,y0,z0,x1,y1,z1,xn,yn,zn"
v = Split(s, ",")
t = ""
For I = LBound(v) To UBound(v)
t = t + v(I)
If I Mod 3 = 2 Then
t = t + vbCr
Else
t = t + ","
End If
Next I
t = Left(t, Len(t) - 1)
Debug.Print t
End Sub
The Split function splits a string along the delimiter you specify (comma in your case), returning the results in a 0-based array. Then in the For loop we stitch the pieces back together, using a carriage return (vbCR) every third element and a comma otherwise.
The final (optional) step is to remove the trailing carriage return.
Hope that helps

The question placed before us was most clearly asked
“Please produce a macro sufficient to the task
I have Cartesian coordinates, a single line of these
Array them in many lines, triplets if you please!”
Instinctively we start to code, a solution for this quest
Often without asking, “Is this way truly best?”
But then another scheme arises from the mind
That most venerated duo: Word Replace and Find
Provide the two textboxes each an encantation
Check the Wildcard option and prepare for Amazation!
Forgive me!
In Word open Find/Replace
Click the More button and check the Use wildcards box
For Find what enter ([!,]{1,},[!,]{1,},[!,]{1,}),
For Replace with enter \1^p
Use Find Next, Replace and Replace All as usual
How it works
With wildcards, [!,]{1,} finds one or more chars that are NOT commas. This idiom is repeated 3 times with 2 commas separating the 3 instances. This will match 3 comma-delimited coordinates. The whole expression is then wrapped in parentheses to created an auto-numbered group (in this case Group #1). Creating a group allows us to save text that matches the pattern and use it in the Replace box. Outside of the parentheses is one more comma, which separates one triplet of coordinates from the next.
In the Replace box \1 retrieves auto-numbered group 1, which is our coordinate triplet. Following that is ^p which is a new paragraph in Word.
Hope that helps!

Related

In Excel VBA, how do I refer to a named column that contains a single quote

I am investigating a bug in an Excel spreadsheet where the following formula is inserted into every cell in a column.
=AGGREGATE( 3, 5, InputData[#[Foo]:[Bar]]) > 0
The VBA is as follows:
Let AddColumn.DataBodyRange.formula = "=AGGREGATE( 3, 5, [#[Foo]:[Bar]]) > 0"
this will evaluate to FALSE if all of the cells on the current row between columns Foo and Bar are empty, otherwise it evaluates to TRUE
The problem I'm seeing is that the names Foo and Bar are variable and not under my control and the formula fails with Run-time error 1004 if a name contains a single quote:
Let AddColumn.DataBodyRange.formula = "=AGGREGATE( 3, 5, [#[Foo's name]:[Bar]]) > 0"
Is there a way I can escape the name in such a way that single quotes won't create the run-time error? Adding double quotes around the name gives me the same error.
Are there likely to be further problems if the names contain other characters that have special meaning in Excel?
I could also refer to the columns by address instead of name. Would that work with the current row '#' notation?
Excel version:14.0.7188.5002
I hear you when you say the naming convention is "not under my control". This really puts you in a bind when anything can be pumped into your code.
Sadly, the only solution is to scrub the input when they finally hand it over to you. This involves you having to make your own vba function that takes in a string and returns a string that has special characters removed (or replaced with something else).
In your case, you are going to have to scrub the data in possibly two places.
First, you will need to change all the column names so they don't have special characters in them. You'll need to access each name and send it through the 'scrub' function and then replace the name with the scrubbed name.
Second, when someone inputs a column name for your AGGREGATE, you'll need to capture that input into a string variable and then pass that through the 'scrub' function. Then you'll need to validate that the input they gave you matches up with a valid column name. If it's not valid, send them an error message asking them to enter a valid name or to cancel out.
After you have valid values for foo and bar, you can add them to your AGGREGATE function and let it execute.
If you can't scrub/change your column names, then you'll have to make a list of scrubbed column names and associate them with the column address. Then when you get your input, scrub it, and then match to the list to grab the correct address. Then you can use hard addresses instead of variable naming schemes.
It's a lot of work. But it's necessary when you have naming conventions that are not under your control.
The other answers and comments put me on the right track:
Function escapedColumnName(columnName As String) As String
columnName = Replace(columnName, "'", "''")
columnName = Replace(columnName, "#", "'#")
columnName = Replace(columnName, "[", "'[")
columnName = Replace(columnName, "]", "']")
escapedColumnName = columnName
End Function

How to categorise a column in excel based on another column containing value in string with delimiter ; where string can have two separate words

This question is a follow up to a previous query: How to categorise a column in excel based on another column that contains value in string separated by semicolons
I have the following spreadsheet (please click on link below for image):
Raw data and expected output
My question is I want to categorise the raw data so the output is as pictured in B16:B34 and C16:C34. I am trying to categorise people by their interests when their interests are in a column containing strings separated by semicolon with multiple words. The Name can come up multiple times according to their interests where in this case Movie, Action Movie, Music, Rock Music, Jazz Music and Radio.
I have tried the answer provided by #Glitch_doctor:
{=IFERROR(INDEX($B$1:$B$11,SMALL(IF(ISNUMBER(SEARCH(B$15,$C$1:$C$11)),ROW($C$1:$C$11)-ROW(INDEX($C$1:$C$11,1,1))+1),ROW(1:1))),"")}
The issue with the answer is that when a person's interest is actually Action Movie they show up in the Movie categorisation. I am trying to match when their interest is Action Movie they only show up in Action Movie after categorisation. Multiple words doesn't seem to work with SEARCH function.
I tried to replace the SEARCH function with a VBA code:
Function ProjectSearch(ByVal strProj As String, ByVal strVal As String, _
Optional ByVal delimiter As String = ";") As Boolean
Dim i As Long
Dim strSplit() As String
strSplit = Split(strProj, delimiter)
ProjectSearch = False
For i = LBound(strSplit) To UBound(strSplit)
If strSplit(i) = strVal Then
ProjectSearch = True
Exit Function
End If
Next i
End Function
Since I am a newbie it doesn't seem to work. My question is there a way where I can do what I want without VBA? If I do need to use VBA what do I need to do?
Thanks kindly in advance.
Okay, that was much simpler than I was expecting it to be:
=IFERROR(INDEX($C$1:$C$11,SMALL(IF(ISNUMBER(IF(SEARCH(E$15,$D$1:$D$11)=1,SEARCH(E$15,$D$1:$D$11),SEARCH(CONCATENATE("; ",E$15),$D$1:$D$11))),ROW($D$1:$D$11)-ROW(INDEX($D$1:$D$11,1,1))+1),ROW(1:1))),"")
I only changed the ISNUMBER(SEARCH(B$15,$C$1:$C$11)) part to ISNUMBER(IF(SEARCH(E$15,$D$1:$D$11)=1,SEARCH(E$15,$D$1:$D$11),SEARCH(CONCATENATE("; ",E$15),$D$1:$D$11)))
The addition IF statement is saying that if the search finds the single word as the first character in the cell to accept that search into the array, otherwise search beginning with "; " instead.

excel VBA - refurn variable length array from function

I want to emulate the results of the builtin "text-to-fields" in a UDF function.
I need to do this because my original data comes from a web query, and I need to use the results on a separate page and plot those results.
For plotting, I need missing values to parse to empty cells, since that is the only option for excel graphs to show missing values as gaps.
You cannot do that with the builtin, because of two limitations;
1) It cannot target the parsed fields onto another sheet.
2) Trying to copy the data values to the destination sheet to parse them there fails, because text-to-fields parses the referencing expression, instead of the value it references.
3) I cannot parse on the original data sheet, and then copy the parsed fields to the target sheet, because no expression can copy an empty cell, it gets converted into a zero value. (after all, an expression resulting in an empty cell would erase itself!)
So I need a DIY field parser, and in any case using a formula is better for my overall needs than having to macro-ize the builtin function (even if it would work).
My fields look like this:
calm
S 10
S 10 G 20
And I want them to parse just as a text-to-fields would, which would give numerics for numbers, strings for text, and empty for missing fields (i.e. shorter readings.)
So I used this code;
Function Explode(texte As String, Optional ByVal delimiter As String = " ") _
As String()
' mimic the text-to-fields,
' but allow inter-sheet references
Explode = Split(texte, delimiter)
End Function
But to use it, I have to pre-define the function calling cell as part of an array, which is fixed size, and I don't know how to have this return a variable number of parsed fields into a fixed size array. What I want from the sample data above is:
But what I get instead is:
Note that empty cells must be empty - not just look empty (not "" strings).
Edit:
I suspect that I may have to instead create a sub which sets the values of the parsed fields and clears the remainder of cells for missing fields (I always have a maximum of four fields) instead of returning them, but am not very VBA proficient. For example, something that gets two cell references, one for the source reference, another for the target list of parsed fields. Then call that from a function which I can embed in the sheet. Side-effect based programming...
You could make your function return a fixed array size using Redim Preserve.
{=Explode($A$1,50,",")}
Function Explode(texte As String, ArraySize As Integer, Optional ByVal delimiter As String = " ") _
As String()
Explode = Split(texte, delimiter)
ReDim Preserve Explode(ArraySize- 1)
End Function

VB.NET - Substring function that stops reading at first integer, possible?

I currently have a text file that has a little over 500 lines of paths.
(i.e., N:\Fork\Cli\Scripts\ABC01.VB)
Some of these file names vary in length (i.e., ABC01.VB, ABCDEF123.VB, etc)
How can I go about using substring function to remove the path name, numbers, and file type, leaving just the letters.
For example, processing N:\Fork\Cli\Scripts\ABC01.VB, and returning ABC.
Or N:\Fork\Cli\Scripts\ZUBDK22039.VB and returning ZUBDK.
I've only been able to retrieve the first 3 letters using this code
Dim comp As String = sLine.Substring(28, 3)
sw.WriteLine(comp)
As Plutonix points out, the best way to isolate the file name from a path is with System.IO.Path.GetFileNameWithoutExtension.
You can extract just the letters (not digits or other characters) from a filename like this:
Dim myPath As String = "N:\Fork\Cli\Scripts\AB42Cde01.VB"
Dim filename As String = System.IO.Path.GetFileNameWithoutExtension(myPath)
Dim letters As String = filename.Where(Function(c) Char.IsLetter(c)).ToArray
The above code sets letters to ABCde.
The code relies on the fact that Strings are treated like arrays of characters. The Where method processes all the characters in the string (array) and selects only the ones that are letters (using the Char.IsLetter method). The selected characters are converted to an array (string) that is assigned to the letters variable.
I see from your latest comment that it is not possible for numerals to be mixed with the letters (as in my example). However, the code should still work in your case.

Test to see if first letter is capitalized

I was wondering if there is a way to test if the first letter in a table cell is capitalized without ripping the letter and comparing it to an array full of CHR codes or looping 26 instr() functions for every cell.
Basically, we have clients that send us tables in which the stub cell (far left) has part of the sentences on one line and then the rest on the line below, indented.
The issue is that I can't use the indents to test for these scenarios because other cells are indented for other reasons. I need to apply row shading depending on these scenarios and I'm having a hard time finding an efficient way to test for this.
This code returns 1
MsgBox (StrComp("This sentence continues", UCase("This sentence continues"), vbBinaryCompare))
This code returns 1 too
MsgBox (StrComp("this sentence continues", UCase("This sentence continues"), vbBinaryCompare))
Assuming you already have the character stored in a string strFirst:
StrComp(strFirst, UCase(strFirst), vbBinaryCompare)
would return 0 if the letter is uppercase.
If you do not already have the first character from the text then you'll need to extract it using Left(string, 1).