【发布时间】:2020-11-03 17:30:22
【问题描述】:
这就是我的最终结果应该是什么样子。如果没有四位数字移到第二列,则用 4 个零填充。
如果第一列只有 5 位数字,如何将一列中的邮政编码拆分为 2 列并填充第 2 列中的空单元格?
这是我一直在使用的东西
Dim ws As Worksheet
Dim cell As Range
Set ws = Worksheets("sheet1")
For Each cell In ws.Range("K2:K500").Cells
cell.Offset(0, 1).Value = Left(cell.Value, 5)
Next cell
Dim cel As Range, rngC As Range, rngB As Range
Dim lastRowA As Long, lastRowB As Long
With ws
lastRowK = .Cells(.Rows.Count, "K").End(xlUp).Row 'last row of column A
lastRowL = .Cells(.Rows.Count, "L").End(xlUp).Row 'last row of column B
For Each cel In .Range("K2:K" & lastRowL) 'loop through column L
'check if cell in column A exists in column B
If WorksheetFunction.CountIf(.Range("K2:K" & lastRowL), cel) = 0 Then
cel.Offset(0, 3).Value = Right(cel.Value, 4)
'.Range("M" & cel.Row) = Right(cell.Value, 4)
Else
.Range("M" & cel.Row) = "0000"
End If
Next
End With
【问题讨论】:
-
请解释您的代码在做什么,但有错误。
-
您愿意接受公式解决方案吗?
-
为什么选择 VBA?这可以很容易地用一个公式来解决......