Reputation: 45
I have a list that looks like
Col A
- FU010402350000
- FU0366090000
- FU0023750000
- FU010587090000
- FU0368420000
- FU010671340000
Everyday it needs sorting, the character count for a real string value is 14 characters, so the ones that are 14 characters are correct, but the 12 character strings need editing to become 'real'.
The thing is, it needs to have 00 at the beginning, after FU.
My train of thought is
If < 12 characters add 00 after FU, otherwise if it's 14 characters ignore cell
Here is my start
Sub charactercountfilter()
' Get rownumber of lastrow of data Col A
lastrow = Range("a65536").End(xlUp).Row
' Check row 1 to last row #, Col A
For i = 1 To lastrow
' If less than 12 chars
If Len(Cells(i, 1)) < 12 Then
' ...
Next i
End Sub
Upvotes: 0
Views: 108
Reputation: 152505
This will be quicker as it works with a variant array and only accesses the worksheet twice:
Sub FU0014character()
Dim ws As Worksheet
Set ws = Worksheets("Sheet12") 'Change to your sheet or ActiveSheet
Dim rng As Range
Set rng = ws.Range(ws.Cells(1, 1), ws.Cells(ws.Rows.Count, 1).End(xlUp))
Dim arr As Variant
arr = rng.Value
Dim i As Long
For i = 1 To UBound(arr, 1)
arr(i, 1) = Left$(arr(i, 1), 2) & Format(Val(Mid$(arr(i, 1), 3)), "000000000000")
Next i
rng.Value = arr
End Sub
Upvotes: 2
Reputation: 45
Sub FU0014character()
'
' selectbinsertcolumn Macro
'
Columns("B:B").Select
Selection.Insert Shift:=xlToRight, CopyOrigin:=xlFormatFromLeftOrAbove
' Get rownumber of lastrow of data Col A
lastrow = Range("a65536").End(xlUp).Row
' Check row 1 to last row #, Col A
For i = 1 To lastrow
' If less than 14 chars
If Len(Cells(i, 1)) < 14 Then
' Take data and apply it to right column
Cells(i, 1) = Range(Cells(i, 1), Cells(i, 2)).FillRight
End If
' Not less than 14 - get next row
Next i
Columns("B").Replace What:="FU", _
Replacement:="FU00", _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
MatchCase:=False, _
SearchFormat:=False, _
ReplaceFormat:=False
For i = 1 To lastrow
' If longer than 12 chars
If Len(Cells(i, 1)) > 12 Then
' move to right
Cells(i, 1) = Range(Cells(i, 1), Cells(i, 2)).FillRight
End If
' Not less than 12 - get next row
Next i
Columns("A:A").Select
Selection.Delete Shift:=xlToLeft
End Sub
Basically what this does is, creates a new column (new column b) it then checks if Column A values character count is 14 characters, if it is less than 14 it will move it to the right, in the new column B.
Then it replaces every (12 character) in column B's FU with FU00.
It runs the column A check again, this time looking if it's greater than 12 and if they are (obviously, because they would've been moved at the first check), it will move it to the right in column B.
It then deletes the blank Column A.
Upvotes: 0