String Concatenation in VBA - vba

I've got this function:
Function GetFullNameCSV() As String
GetFullNameCSV = Replace(ThisWorkbook.FullName, ".xlsm", ".txt")
End Function
If the code looked like this hard coded:
Filename:= "C:\directory\filename.txt"
I could just replace it with this and get the same result:
Filename:= GetFullNameCSV()
Problem is it appears in the case of this string:
Connection:= "TEXT;C:\directory\filename.txt"
I need to use string concatenation, guessing something like this:
Connection:= "\"TEXT;+GetFullNameCSV()+\""
What the code I need to use? Thanks.

Assuming that all the sections are always delimited with a ;, just split the string into it's parts and then do your changes and then join it together again.
So with your sample I think it would be something like (please note, written from memory, so might not be completely correct):
Dim parts() as String
parts = Split(str, ";")
parts(2) = GetFullNameCSV() ' assuming that part 2 is the one you want to replace
str = Join(parts, ";")

Related

VB.net/C# Datatable.Select with special characters (brackets)

I have a vb.net Project with a Dataset with some Cells with Strings like "JB-Y[ST]Y". My Problem is you can't use datatable.select("Column like 'JB-Y[ST]Y'") because of the brackets (the select thinks this is a pattern, but I need an exact match). Anyone has an idea on this topic? I can't find a solution... Thank you!
I can't find a solution using Regex.Replace so I have a brute force solution like this
Public Function ReplaceBrackets(search as String) As String
Dim sb As StringBuilder = New StringBuilder()
For Each c In search
If c = "[" Then
sb.Append("[[]")
Else if c = "]" Then
sb.Append("[]]")
Else
sb.Append(c)
End If
Next
return sb.ToString()
End Function
And you can call it in your Select statement like this
datatable.Select("Column like '" & ReplaceBrackets(search) & "'")

VB: Search in String

How do I search in a string from ";" to ";"?
Dim strInput as String = "text1;text2;text3"
Solution should look like this:
strOutput1 = "text1"
strOutput2 = "text2"
strOutput3 = "text3"
The lenght of the single "parts" is not fix, strInput can also be like "12345;name;Christoph;"
I just want to get the parts in a own string.
Does anybody knows how to?
Quickest way is probably using SPLIT to populate an array then set the individual variables to each element in the array. The only issue that might arise is if you have ;'s inside the strings, but that would pretty much prevent anything not based on length.
Starting with your string:
Dim strInput as String = "text1;text2;text3"
Then do a split:
Dim inputArray() As String = Split(strInput, ";")
This will split your string up using ; as the delineator. So you will end up with this:
inputArray(0) = "text1"
inputArray(1) = "text2"
inputArray(2) = "text3"
More on split()...
https://msdn.microsoft.com/en-us/library/6x627e5f%28v=vs.90%29.aspx?f=255&MSPPError=-2147217396
Using .Split you can separate the original string into an array of strings like this
Dim strInput As String = "text1;text2;text3"
Dim tempStringList() As String = strInput.Split(";"c)
So now
tempStringList(0) contains "text1"
tempStringList(1) contains "text2"
and so on.
The beauty of .split is that it doesnt care how many sections there are to separate.

How to extract numbers UNTIL a space is reached in a string using Excel 2010?

I need to pull the code from the following string: 72381 Test 4Dx for Worms. The code is 72381 and the function that I'm using does a wonderful job of pulling ALL the numbers from a string and gives me back 723814, which pulls the 4 from the description of the code. The actual code is only the 72381. The codes are of varying length and are always followed by a space before the description begins; however there are spaces in the descriptions as well. This is the function I am using that I found from a previous search:
Function OnlyNums(sWord As String)
Dim sChar As String
Dim x As Integer
Dim sTemp As String
sTemp = ""
For x = 1 To Len(sWord)
sChar = Mid(sWord, x, 1)
If Asc(sChar) >= 48 And _
Asc(sChar) <= 57 Then
sTemp = sTemp & sChar
End If
Next
OnlyNums = Val(sTemp)
End Function
If the first character in the description part of your string is never numeric, you could use the VBA Val(string) function to return all of the numeric characters before the first non-numeric character.
Function GetNum(sWord As String)
GetNum = Val(sWord)
End Function
See the syntax of the Val(string) function for full details of it's usage.
You're looking for the find function.. Example:
or in VBA instr() and left()
Since you know the pattern is always code followed by space just use left of the string for the number of characters to the first space found using instr. Sample in immediate window above. Loop is going to be slow, and while it may validate they are numeric why bother if you know pattern is code then space?
In similar situations in C# code, I leave the loop early after finding the first instance of a space character (32). In VBA, you'd use Exit For.
You can get rid of the function altogether and use this:
split("72381 Test 4Dx for Worms"," ")(0)
This will split the string into an array using " " as the split char. Then it shows us address 0 in the array (the first element)
In the context of your function if you are dead set on using one it is this:
Function OnlyNums(sWord As String)
OnlyNums = Split(sWord, " ")(0)
End Function
While I like the simplicity of Mark's solution, you could use an efficient parser below to improve your character by character search (to cope with strings that don't start with numbers).
test
Sub test()
MsgBox StrOut("72381 Test 4Dx")
End Sub
code
Function StrOut(strIn As String)
Dim objRegex As Object
Set objRegex = CreateObject("vbscript.regexp")
With objRegex
.Pattern = "^(\d+)(\s.+)$"
If .test(strIn) Then
StrOut = .Replace(strIn, "$1")
Else
StrOut = "no match"
End If
End With
End Function

How to get rid of the zero '0' numeric from a string?

BEFORE:
Johnson0, Yvonne
AFTER:
Johnson, Yvonne
String functions for Access can be found at http://www.techonthenet.com/access/functions/string/replace.php
In your example, code like
Replace("Johnson0", "0", "")
will do the trick for the particular string Johnson0. If you need to only remove the zero if it is the last character, play with the additional start and count parameters described in the link above.
You can try executing following query..
UPDATE table set
columnName = REPLACE(columnName,'0','')
WHERE columnName LIKE "%0%";
This will replace all occurrence of "0" with "".
The answer you submitted clarifies your requirement. Based on that, you don't need to create a user-defined function if your Access version is 2000 or later. You can get the same result with the Replace() function.
MsgBox Replace("Jonson0, Yvonne", "0,", ",")
One approach is to create a custom function
See http://www.techonthenet.com/access/functions/misc/alphanumeric.php for an example. You could do something similar, but in the loop you would only keep the alpha characters.
Public Sub xxx()
MsgBox RemoveStr0("Jonson0, Yvonne")
End Sub
Public Function RemoveStr0(sString As String) As String
Dim ipos As Long, sTemp As String
ipos = InStr(1, sString, "0,")
sTemp = Mid$(sString, 1, ipos - 1)
sTemp = sTemp & Mid$(sString, ipos + 1)
RemoveStr0 = sTemp
End Function
if you can pull it out to java or another OO lang you can just do a matching using regexes.

VB.NET REPLACE function

I am using replace function to replace a character in the file
sw.WriteLine(Regex.Replace(strLine, "\\", Chr(13)))
This code is working fine, but now I want to replace two times and I want to use the replace function twice. Something like this, but it is not working . Can anyone tell me how to use Replace function multiple times?
sw.WriteLine(Regex.Replace(strLine, "\\", Chr(13)).Replace(strLine, Chr(13), ""))
Your second Replace is using the String.Replace extension, not the Regex.Replace method.
The Regex.Replace function returns a string, not a regex, which is why your second regex call isn't working. For multiple Regex.Replace calls, you would have to do each one individually or modify your replacement statement.
You could probably just use the String.Replace function for this:
sw.WriteLine(strLine.Replace("\\", Chr(13)).Replace(Chr(13), ""))
sw.WriteLine(Regex.Replace(Regex.Replace(strLine, "\\", Chr(13)), Chr(13), "")
Here it is more laid out, so you can see what's going on:
Dim firstIteration = Regex.Replace(strLine, "\\", Chr(13))
Dim secondIteration = Regex.Replace(firstIteration, Chr(13), "")
sw.WriteLine(secondIteration)
Replace a carriage return in a string may be the following ways:
str_souce = str_source.Replace(vbCrLf, "")
str_souce = str_source.Replace(chr(13) & chr(10), "")
str_souce = str_source.Replace(environment.newline, "")
if none above works, try the following one. It can even works for a third party software
str_souce = str_source.Replace(vbCr, "").Replace(vbLf, " ")