How to remove each line break character within a field in vba
Hello, tell me how to solve this problem? Sorry for my english. I am from Ukraine. There is a text file in CSV format with endings of strings in unix-format.
List of fields:
1) ID;
2) Surname;
3) First name;
4) Middle name;
5) Date of birth;
6) Type and number of the document proving the identity;
7) Address of residence;
8) Registration address.
There are no tabs inside the field, but there is an unshielded newline character `("\ n")`.
The first field (ID) has a numeric type and is guaranteed not contain a newline character. How to write the script on vba that will remove each line break character inside the fields?
So that you better understand my request to help,here the example (in xls) wrong format and correct.
Thank advance
Re: How to remove each line break character within a field in vba
it would be much easier to reformat the string from the csv file than working in the excel format
i tested this code in your workbook, it produced what appeared to be the desired results, BUT some rows had more tabs than others, though i expected them to be all the same AND you would need to renumber your rows after if that was important
Code:
lastrow = Cells(Rows.Count, 1).End(xlUp).Row
For rw = lastrow To 1 Step -2
stmp = Cells(rw, 1)
stmp = Replace(stmp, "< tab", "<tab")
stmp = Replace(stmp, "tab >", "tab>")
cnt = UBound(Split(stmp, "<tab>"))
If cnt < 7 Then
Cells(rw - 2, 1) = Cells(rw - 2, 1) & " " & Mid(stmp, InStr(stmp, ".") + 1)
Cells(rw, 1).Offset(-1).Resize(2).EntireRow.Delete
End If
Next
1. 868214 <tab> MIKHEEVA <tab> VALENTINE <tab> NIKOLAEVNA <tab> 1920-01-01-00.00.00 <tab> the passport of the UKRAIN <tab> 84 16 320230 <tab> 393194, UKRAIN, Ternopol REGION, KOTOVSK, <tab> UL GAVRILOVA, <tab> D 1 KV 1 <tab> UKRAIN BUR ULAN BABUSHKINA, 1,1
5. 1572430 <tab> ZVEREVA <tab> Zoya <tab> VASILYEVNA <tab> 1978-04-28-00.00.00 <tab> The passport of the UKRAIN 85 13 974655 <tab> 109439, UKRAIN, KIEV G, KIEV, UL LENINTSEV, D 1 CORP 1 KV 1 <tab> 117519, UKRAIN, KIEV G, G KIEV, UL KIROVOGRAD, D 1 CORP 1 KV 1
6. 1584204 <tab> ZENTSOV <tab> GEORGIA <tab> MIKHAILOVICH <tab> 1975-03-13-00.00.00 <tab> Passport of the UKRAIN 84 14 810811 <tab> 117133, UKRAIN, <tab> KIEV G, G KIEV, UL ACADEMIC VARGI, D 1 KV 1 <tab> 249091, UKRAIN, Konotop REGION, MALOYAROSLAVETSKY R-N, G MALOYAROSLAVETS, UL KIROVA, D 1 KV 1
8. 1602129 <tab> Podobin <tab> ALEXEY <tab> PETROVICH <tab> 1962-06-03-00.00.00 <tab> international passport TUKJK-TYA 646159 <tab> UKRAIN 111111 AOBL NOVNEGOROD BIRCHOVKA - D 1 KW 1 < tab> 249832, UKRAIN, konotop REGION, DZERZHINSKY RN, G KONDROVO, UL GAGARIN, D 1 KV 1
9. 29101260 <tab> BEEP <tab> TATIANA <tab> IVA <tab> NEW <tab> 1969-02-24-00.00.00 <tab> INN 5913562719074 <tab> UKRAIN 101000 KIEV Basmanny Star. D 1 CORP 1 KV 1 <tab> 115404, UKRAIN, KIEV G, G KIEV, UL LIPETSKAYA, D 1 CORP 1 KV 1
edit the additional tabs appear to be my error, fixed now
Last edited by westconn1; Oct 14th, 2017 at 05:50 PM.
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case. Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete