Reputation: 120
I've been trying to find a way to split a variable that I have pulled in vba. The variable is "variable1" which will always be equaled to xx.xx (numbers). What I'm trying to do is if variable1 = 2.6 then I want to split it and have varsplit1 = 2 and varsplit2 = 6. I found an example on how to do it but I can't pull the database example to see how the code works, due to our network security.
Upvotes: 1
Views: 65
Reputation: 55831
If the variables are decimal numbers, you should handle them as such:
' Example value.
variable1 = 2.6
' Split in integer and decimal parts.
variable2 = variable1 - Int(variable1)
variable1 = Int(variable1)
Upvotes: 2
Reputation: 27
You can use the Split(expression, delimiter) function in VBA to split on the ".". It returns an array starting with 0, so make sure to specify which part of the array to return.
So something like this:
Public Sub testing()
Dim variable1 As String: variable1 = "2.6"
Dim varsplit1 As String: varsplit1 = Split(variable1, ".")(0)
Dim varsplit2 As String: varsplit2 = Split(variable1, ".")(1)
debug.print varsplit1
debug.print varsplit2
End Sub
https://msdn.microsoft.com/en-us/library/6x627e5f(v=vs.90).aspx
Upvotes: 3