VBA split() help
9 minutes ago, mariushm said:I'm wondering why you don't use the Replace function : https://www.techonthenet.com/excel/formulas/replace_vba.php
As for your code, I see a small problem....
You're checking for presence of double quotes and if you find some, you put the changed text in the correctedID variable.
But then, you go and check for "/" and you're checking in the old sampleID variable and if you find some... you put in correctedID the changed text but you're using the original sampleID as source.
Basically, if the text has both double quotes AND / , your code lets double quotes go through because the part where you check for / ignores whatever was before in correctedID.
At the very least I would change the code to something like this:
Dim idArray As Variant Dim x as Integer Dim y As Integer Dim correctedID as String dim tempString as String tempString = "" If InStr(sampleID, """") Then idArray = Split(sampleID, """") y = UBound(idArray) For x = 0 To y tempString = tempString & idArray(x) Next x End If ' if we made replacements, then tempString contains corrected text SO FAR ' if no changes made, then just copy the original text because we use this ' variable from this point, instead of sampleID if tempString = "" then tempString = sampleID correctedID = "" If InStr(tempString, "/") Then idArray = Split(tempString, "/") y = UBound(idArray) For x = 0 To y correctedID = correctedID & idArray(x) Next x End If If correctedID = "" Then correctedID = testString End If
And a small tip : Windows doesn't mind if you use / in file paths, like c:/files/my file.txt , which is great if you're coding in some languages where some combinations of characters get changed when you use \ ... for example "T:\TCNA ..." could be interpreted by some languages as "T: [TAB KEY] CNA ..." .... i don't think it's the case here but it's worth keeping in mind... you can probably easily say "t:/tcna ... " and should work just fine.
That's a great catch! I didn't think to change the sampleID on the second check for symbols. Also, I completely forgot about the replace function. I will change my additions to the replace and report back after testing. Thanks a ton!
As for the saving, anytime I have say "c373-5 samples 3" hexagonal.docx" (the technicians put the sampleID into excel and I'm pulling it directly from that cell) the " will throw an error which is why the block of code was included in the first place.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now