Text to columns based on value

andysh

Board Regular
Joined
Nov 8, 2019
Messages
111
Hi,

I think I'm pretty close with this. I'm trying to remove a leading letter S from product codes but not all cells in the column are product codes, so what i'd like the vba to do is:-

Look at cells in col C
If cell value is 'S' followed by 8 digits
remove 'S' and leave 8 digits
else
leave cell contents as they are

I have the below but it isn't working, any suggestions?


VBA Code:
Dim LR As Long
Dim i As Integer

LR = Cells(Rows.Count, 3).End(xlUp).Row

For i = 1 To LR
If InStr(Cells(i, 3), "S########") <> 0 Then
Cells(i, 3).TextToColumns Destination:=Range("C2"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _
Semicolon:=False, Comma:=False, Space:=False, Other:=True, OtherChar _
:="S", FieldInfo:=Array(Array(1, 9), Array(2, 1)), TrailingMinusNumbers:=True

End If
Next i
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
How about
VBA Code:
For i = 1 To LR
   If Cells(i, 3) Like "S########" Then Cells(i, 3) = Mid(Cells(i, 3), 2)
Next i
 
Upvote 0
See if the 1st char is "S" then see if the string is len(9), then see if the last 8 chars are numeric

VBA Code:
Sub Button1_Click()
Dim sh As Worksheet
Dim rng As Range
Dim c As Range

Set sh = ActiveSheet
With sh
Set rng = .Range("C2:C" & .Cells(.Rows.Count, "C").End(xlUp).Row)
For Each c In rng.Cells

If Left(c, 1) = "S" And Len(c) = 9 And IsNumeric(Right(c, 8)) Then
c = Right(c, 8)
End If
Next
End With


End Sub
 
Upvote 0
@davesexcel
Without knowing what the OP's data is like, that will give the wrong result if a cell has a value of S50090E88
 
Upvote 0
I consider digits to be numbers, but isnumeric will return true for 50090E88 even though it's not a number.
 
Upvote 0
Perfect, thanks Fluff, your initial idea works a treat

And it's a lot less code than text to columns, stored in the sponge for future use with other files
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,755
Members
448,989
Latest member
mariah3

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