我有一个类似于
的列表Col A
- FU010402350000
- FU0366090000
- FU0023750000
- FU010587090000
- FU0368420000
- FU010671340000
醇>
每天需要排序,实际字符串值的字符数为14个字符,因此14个字符的字符数是正确的,但12个字符串需要编辑才能变为“真实”。
问题是,在FU之后需要00开始。
我的思路是
如果<在FU之后12个字符添加00,否则如果它的14个字符忽略单元格
这是我的开始
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
答案 0 :(得分:2)
这将更快,因为它适用于变量数组并且只访问工作表两次:
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
答案 1 :(得分:0)
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
基本上这样做,创建一个新列(新列b)然后检查列A值字符数是否为14个字符,如果小于14则将它移动到右侧,在新列B中
然后用FU00替换B列FU中的每个(12个字符)。
它再次运行A列检查,这次查看它是否大于12,如果它们是(显然,因为它们会在第一次检查时被移动),它会将其移动到B栏右侧。
然后删除空白列A.