I have a numeric text file whose data need to be tabulated. Unfortunately, depending on the input data, this text file shows numbers which are not separed, one to each other, from a fixed number of spaces and unfortunately this number of space could not be the same from a row to another.
I tried this:
For Each line In RichTextBox2.Lines
If Not line.Trim.ToString = "" Then
Dim item() As String = line.Trim.Split(" "c)
DataGridView1.Rows.Add(item)
End If
Next
and then I used to erase the blank columns. But I realised that when the number of space is different on the rows, I have data, that should fill the same column, that are on two or three different columns (with spaces). Let me provide an example:
Here there are the numbers coming from the text file and here are the tabulated numbers.
What I would do, is to erase all the spaces except one, in order to have these numbers equally spaced one from another. Hoping to have been clear, I thank you so much for the support
Related
I have a string that often looks like this:
X-POTATO-2D-AB3F-N
It will always be in that format, and I want to compare "AB3F"to another string of four characters. Here's the catch, certain values in this string may change in length, so I cannot compare simply "the first four of the last 6 characters"
What will always stay the same is the number of dashes. Always at least 4 in total (on rare occasions 5), and the string that I want to compare will always be between the last two. Is it possible to use these to isolate the 4 or 5 characters that I want to compare?
I know Stack overflow doesn't like vague questions. Sorry! :D
Here you go:
http://excel.tips.net/T003324_Finding_the_Nth_Occurrence_of_a_Character.html
Essentially, this formula:
=FIND("¬",SUBSTITUTE(A1,"-","¬",4))
Where A1 is your string. This will return the position of the 4th "-". (Because substitute replaces the 4th "-" with a "¬", which is unlikely to appear in the code, and find then finds the "¬")
Edit, just realised that the 4th group may also have more than 4 digits, so you could retrieve it with the overcomplicated:
=MID(A1,FIND("¬",SUBSTITUTE(A1,"-","¬",3))+1,FIND("¬",SUBSTITUTE(A1,"-","¬",4))-FIND("¬",SUBSTITUTE(A1,"-","¬",3))-1)
Hope this helps!
Since you mentioned there could be four or five hyphens, and that your desired value will always be between the last two, I suggest the following:
=TRIM(LEFT(RIGHT(SUBSTITUTE(A1,"-",REPT(" ",99)),198),99))
Replace each hyphen with 99 spaces (much longer than the longest substring)
The Right most 198 characters will surely have the last two substrings
Left (...,99) will surely return the first of those two
Trim to get rid of the extra spaces
This approach will work with four or five hyphens.
If you are going to use this in a Macro, the following code will return the "Next To Last" hyphen-delineated substring within a string:
Dim V As Variant
V = Split(S, "-")
NextToLast = V(UBound(V) - 1)
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!
Information
I have a column of about 300 part numbers that is constantly expanding. These part numbers can be only numbers, only letters, or a combination of both. I'm using dynamic named ranges with the part numbers to fill combobox lists. When the user types in the part number in the combobox, if we've done that part before it will autofill as they keep typing. I've recently ran into a problem with this however with a recent group of parts that was added.The part number is just a six digit number, and are the only part part numbers that solely consist of numbers, every other one has letters, or if its just numbers there are hyphens in the part number. The combobox I'm filling with this named range does show these numerical part numbers in its list, but does not autofill them when the user types and does not autofill the other information associated with the part number in other textboxes when the part number is done being typed in.
Question
Can anyone help me understand why it isn't autofilling the numerical value but it fills every other value?
Code
Here is the code I'm using to fill the combobox list and the dynamic range formula. Let me know if you need anything else to help you give an answer.
=OFFSET('Part List'!$A$1,0,0,COUNTA('Part List'!$A:$A),1)
comboxPartNumber.List = Range("Part_Number").Value
List of Sample Parts
X-600-ASSY-SM
LO-5093-020-023
LO-5093-020-025
AB-1541754
764761
766415
I cannot add a comment, sice I am 2 points short :(, so I am forced to post this as a solution:
Try to make all the numbers strings.
Dim R As Range
For Each R In Range("Part_Number").Cells
If IsNumeric(R.FormulaLocal) Then R.FormulaLocal = "'" & R.FormulaLocal
Next
Let me know if it worked.
I want to create a makro in Excel which performs - after pressing a button - the below. I attached some dummy data that is formatted like the actual sheet.
There are several data blocks that are seperated by headlines in Sheet 1. From these headlines, I want to get a string of numbers and put it into column S for each line with data below that heading. In line 6 the heading says "2000", thus lines 8-19 should have a "2000" in column S and so on. The number I want to get is always after the word "Monthlyaccount" but the lenght of the number can be different - from 1 to 7 digits.
For lines where no data is given (data lines might be recognized by looking if in A is a valid date given) there should just be "ERR" instead of the number.
Can anyone help out?
Thanks so much upfront!
Say the string (in cell A1) contains "Monthlyaccount" followed by a blank followed by a number followed by another blank. To extract the number, use:
=--LEFT(MID(A1,FIND("Monthlyaccount",A1)+15,9999),-1+FIND(" ",MID(A1,FIND("Monthlyaccount",A1)+15,9999)))
This does as requested. Paste the following into S3 and drag down
=IF(AND(ISNUMBER(RIGHT(A2,4)*1),ISNUMBER(RIGHT(A3,4)*1)),S2,IF(ISNUMBER(RIGHT(A3,4)*1),LEFT(RIGHT(A1,LEN(A1)-FIND("Monthlyaccount",A1)-14),FIND(" ",RIGHT(A1,LEN(A1)-FIND("Monthlyaccount",A1)-14))),"ERR"))
I have some data which I run through, which generates a textfile.
The data is all pulled correctly, but it doesn't format correctly.
Right now, I am using TAB + Variable to space between each column but it is obviously made uneven as different variables differ in character length. Here is the layout:
RECORD NAME ADDRESS TELEPHONE SOMETHING SOMETHING
... Data is here.
Any ideas?
String.Format is your friend here.
It's very powerfull and gives you the function to align your output.
For example:
(EDIT: removed the txt prefix because could be confusing, now I suppose that data to be formatted is contained in string vars)
Dim result as string
result = String.Format("{0,-10}{1,-30}{2,-30}{3,-10}{4,20}", Record, Name, Address, Telephone, Something)
The result will be aligned to the left in a 10 space column for the first element (txtRecord) and so on for the remainders, the last element will be formatted in a column with 20 space and right aligned
If that's not enough look at composite formatting to get other useful options