VBA how to select specific characters from a string

ivavok

New Member
Joined
Sep 1, 2009
Messages
11
Hello,

could somebody help me with this please? In one column I have different objects separated by a comma. I need to select one of these: 11,20,30,60,61 and copy it into another column. I have used this code:

For counter = 0 To not_empty_cells

For counter_dep = 1 To 5

position = InStr(1, (Cells(counter + 3, 4)), department(counter_dep))

If position > 0 Then
symbol_dep = Mid(Cells(counter + 3, 4), position, 2)

Cells(counter + 3, 10).Value = symbol_dep
End If

Next counter_dep

Next counter

It works, however, once in the first column there are the following objects: 60,6128,CZ, it takes 61 but it should take 60. Unfortunately, the position of the object can vary, it is not always on the first position.

Thank you
 
For counter = 0 To not_empty_cells

Cells(counter + 3, 4).Offset(counter + 3, 6).Value = get_element(Cells(counter + 3, 4).Value)

Next counter
 
Upvote 0

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Full code means literally all your code you are using - so I wish to see what you are using for the get_elemnt code, also how the counter value has been populated.

The exact code that I had in my post for get_element (ie if you copy & pasted it into a code module) should have worked.
 
Upvote 0
Ok:)

Public Sub modifying_table()
Dim row1 As Long
Dim column1 As Long
Dim not_empty_cells As Long
Dim counter As Long

row1 = 3
column1 = 5
not_empty_cells = 0

Do While Not IsEmpty(ActiveSheet.Cells(row1, column1))
row1 = row1 + 1
not_empty_cells = not_empty_cells + 1
Loop

For counter = 0 To not_empty_cells

Cells(counter + 3, 4).Offset(counter + 3, 6).Value = get_element(Cells(counter + 3, 4).Value)

Next counter

End sub
 
Upvote 0
Hi

Try the following. Copy everything into your code module (omit nothing):

Code:
Public Sub modifying_table()
Dim cell As Range
Dim rngWhole As Range
Set rngWhole = Range("D3:D" & Cells(Rows.Count, "E").End(xlUp).Row)
For Each cell In rngWhole
    cell.Offset(0, 6).Value = get_element(cell.Value)
Next cell
End Sub
Function get_element(ByVal s As String) As String
Static num_list
Dim i As Long
If IsEmpty(num_list) Then _
    num_list = VBA.Array(11, 20, 30, 60, 61)   'list of numbers to find
    
For i = 0 To UBound(num_list)
    If InStr(1, "," & s & ",", "," & num_list(i) & ",") > 0 Then _
        get_element = num_list(i): Exit Function    'if one of our numbers found, return found number and exit the loop
Next i
get_element = "Not found!"   'if we get here, none of the list of numbers has been located
End Function
 
Upvote 0

Forum statistics

Threads
1,215,045
Messages
6,122,840
Members
449,096
Latest member
Erald

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top