How do you split part of an excel cell into another cell? - vba

There's a list of cells that looks like [a-z ]* [1-9.]*. I want to split the numric part into the adjacent cell. How can I do this?

For another formula approach, you could try this to extract the numeric part of A1:
=MID(A1,MIN(FIND({0,1,2,3,4,5,6,7,8,9},A1&"0123456789")),255)

here's a vba snippet, in case this helps:
Dim s As String: s = "wergyuklwgh9235783850298"
Dim i As Long:
For i = 1 To Len(s)
If Mid(s, i, 1) Like "#" Then Exit For
Next
sLeft = Left(s, i - 1) ' now sLeft contains "wergyuklwgh"
sRght = Mid(s, i) ' now sRght contains "9235783850298"
could put this into a public function to make a little UDF, useable from the formula bar

Related

Substring with Excel VBA

I have been using this code as a starting point: https://danwagner.co/how-to-transpose-horizontal-data-to-vertical-data-for-easy-pivot-tables/
One one of my cells Ax (x referring to the number), the content is ABCDEFGHI and I want to substring the cells every 2 characters, and the last set is 3 characters. Final result looks like:
AB CD EF GHI
At line 44, using the variable
varDetails = .Range(.Cells(lngIdx, 1), .Cells(lngIdx, 4))
and think that is where I need to modify the code. I am not fluent enough with VBA and need some help.
To split the data from your string you can use the following code
Sub SplitStringEveryTwoCharacters()
Dim arrayWithValuesByTwo() As String
Dim myString As String
'Just replace with your data
myString = "ABCDEFGHIJKLM"
'Resize
ReDim arrayWithValuesByTwo(Len(myString) - 1)
'For each 2 character in string
For i = 1 To Len(myString) Step 2
'Add in array
If (i <= Len(myString) - 1) Then
arrayWithValuesByTwo(i - 1) = Mid$(myString, i, 2)
End If
If (i = Len(myString)) Then
arrayWithValuesByTwo(i - 1) = Mid$(myString, i, 1)
End If
Next
End Sub
What you need to change
Here I have set my string into a variable with myString = "ABCDEFGHIJKLM" but you can easily change this and take it directly from a cell with something like myString = Range("A5").
You can access you data with arrayWithValuesByTwo(1) for example. Just loop through it to get all of the values.

VBA excel: How do I compare a string array to a string?

I have a script that takes the contents of a cell, and puts the first 2 characters of the cell into a string array. I need to later compare that string array to a string, but I can't seem to get that to work. Here's what I have:
For i = 2 To 600
colStr = Sheets("smartlist").Cells(i, "A").Value
If colStr <> "" Then
ReDim charArray(Len(colStr) - 1)
For j = 1 To Len(colStr)
charArray(j - 1) = Mid$(colStr, j, 1)
Next
strArray = LCase(charArray(0)) & LCase(charArray(1))
If CStr(Join(strArray)) = CStr(Join(pwArray)) Then
Now, I've tried:
If charArray = "ab"
If Join(charArray) = "ab"
If CStr(Join(charArray)) = "ab"
I'm pretty lost at this point. Any suggestions would be welcome!
Edit: added the whole function up until I get the 'Type mismatch'
You could use Join(charArray, "") - without "" it joins the elements with space so the result of your initial try was "a b"
Firstly, you really need to clarify what you're doing. You say that later you need to check what's in the string. If that is the case, then you don't need an array, you simply need another string...
Dim chars As String
chars = Left$(cellToTest, 2)
Later you can test more simply using the InStr function, like so...
Dim loc As Integer
loc = Instr(colStr, chars)
If loc > 0 Then
...
If you want to turn this into a function on your spreadsheet you can use the following in the cells as formulas...
=LEFT(A1, 2)
=SEARCH(A3, A1)
Here's a little screen shot of what I mean...

VBA to seach string in cell and create new data in another cell

The task I'm trying to complete is as follows:
Each of the letters represent a specific training an individual has received. The problem being we have some symbols that after you achieve more than one, become another symbol. So what I need to do is break down the symbols in a cell, and have them returned to their full values.
Example:
D2: {#DWRZ}
R= B+X
Result after code: {#DWBXZ}
I'm not sure how to go about doing this in VBA, I'm assuming something like a string search and if R is found then write to a new cell the current contents without the R and add BX. Thoughts?
Without knowing exactly how your data is structured, it's a little difficult to code a fully functional solution. For instance, it would be helpful to know where "R = B+X" is defined in your table relative to "{#DWRZ}". That being said, here is a general solution that I think might work for you.
Function ReplaceValues(OldString As String)
n = 3 ' <----------------Adjust the number of replacements as needed.
ReDim arr(1 To n, 1 To 2) ' ^
arr(1, 1) = "R": arr(1, 2) = "BX" ' |
arr(2, 1) = "--": arr(2, 2) = "--" ' |
arr(3, 1) = "--": arr(3, 2) = "--" ' |
' <------------Add more definitions as needed... ---------- +
For x = 1 To n
If InStr(1, OldString, arr(x, 1)) Then 'If the leter "R" is found in "#DWRZ"
NewString = Replace(OldString, arr(x, 1), arr(x, 2)) 'Replace it with "BX"
End If
Next
ReplaceValues = NewString
End Function
If you place this function in your project, then you will be able to call it in the same way that you would any of the functions built into Excel. For example =ReplaceValues(A1) where cell A1 contains your string, "{#DWRZ}" Hopefully this is useful to you.

Copy part of text from the same cell

I have a problem when I downloaded a file containing different kind of information that should be stored in different cells but all is written in the same cell.
For example A:9 contains:
2016.03.16,"8982266507","QLGJG","AHGLG","OKK","IK","ODEADKIK","DK57200028982561607","485979,12","65164,94","485979,12","65164,94","485979,12","65164,94","","","","","","",
I would like to have a macro that copies specific parts of this string for example the last part "65164,94" and paste in to cell A:10.
Thank you in advance
As well as Seb's answer, you can use the split function. So:
Sub splitting_string()
Dim arr1 As Variant, var1 As String
var1 = Range("A9")
arr1 = Split(var1, ",")
For i = 0 To UBound(arr1)
Cells(10 + i, 1) = arr1(i)
Next i
End Sub
This will separate the long string in A9 into smaller ones by splitting them every time there's a comma, placing them in the cells below.

Word VBA code to cut numbers from one column and paste them in another

I am looking for some code that can search cell by cell in the 2nd column of a table for numbers and decimal points, cut them and paste them in the cell to the left whilst leaving the text behind.
For example:
1(tab space)Test
1.1(tab space)Test
1.1.1(tab space)Test
1.1.1.1(tab space)Test
Where the bullet points represent separate cells in different columns.
In all instances the numbers are separated from the text by a tab space "Chr9" (as indicated in the example)
Any help or useful snippets of code would much appreciated!
EDIT: I have some code that scans each cell in a column but I dont know the code to tell it to only cut numbers and decimal points up to the first tab space.
The Split function delivers what you are after. Sample code:
Dim inputString As String
Dim splitArray() As String
Dim result As String
inputString = "1 Test"
splitArray = Split(inputString, " ")
If(UBound(splitArray) >= 1) Then 'Making sure that it found something before using it
result = splitArray(1) 'Text
End If
inputString = "1.1 Test"
splitArray = Split(inputString, " ")
If(UBound(splitArray) >= 1) Then
result = splitArray(1) 'Text
End If
'etc.
UPDATE
Code delivering the functionality you want:
Dim splitArray() As String
Dim curTable As Table
Set curTable = ActiveDocument.Tables(1)
For Row = 1 To curTable.Rows.Count
With curTable
splitArray = Split(.Cell(Row, 2).Range.Text, " ")
If (UBound(splitArray) >= 1) Then
.Cell(Row, 2).Range.Text = splitArray(1)
.Cell(Row, 1).Range.Text = splitArray(0)
End If
End With
Next Row