我希望有人能帮我解决这个复杂的问题!
这段代码检查E列中的名称,然后为E列中的每个名称创建D列中的第一个字母,并添加一个点。
例如,如果E列中有一个名为Dave的名称,则会在D列中创建D.
。
例如,如果E列中有一个名为Dave Rick的名字,则在D列中创建D.E.
。
所以我想在这段代码中改变的是,如果D列中已经有了第一个字母,或者第二个或第三个字母,那么除了放一个点之外,它不应该做任何事情。如果没有第一个字母,并且E列中没有第一个字母,它应该自动将E列中姓名的第一个字母放在D列中,并加一个点。
Dim cell As Range
lastRow = ActiveSheet.Cells(ActiveSheet.Rows.Count, "E").End(xlUp).Row
For Each cell In ActiveSheet.Range("E2:E" & lastRow)
S = ""
If cell.Value <> "" Then
V = Split(cell.Value, " ")
For Each W In V
S = S & Left$(W, 1) & "."
Next W
cell.Offset(ColumnOffset:=-1).Value = S
End If
Next cell
截图:
额外的截图:
发布于 2021-04-19 14:16:09
如果您的cell
包含一个值,只需将空格替换为点并在末尾附加一个点即可。
For Each cell In ActiveSheet.Range("E2:E" & lastRow)
S = ""
If ActiveSheet.Range("B" & cell.Row) <> "" Then 'Adjust "B" to be the column with the initials
'If initals exist re-format them with dots.
Dim FixValue As String
FixValue = cell.Value
FixValue = Replace$(FixValue , ". ", ".") 'this will turn `D. Z. K.` into `D.Z.K.` as the other patterns
FixValue = Replace$(FixValue , " ", ".") & "." 'this will turn `D Z K` into `D.Z.K.`
cell.Value = FixValue
ElseIf cell.Value <> "" Then
'If no initals exist but a first name exists then turn name into initials
V = Split(cell.Value, " ")
For Each W In V
S = S & Left$(W, 1) & "."
Next W
cell.Offset(ColumnOffset:=-1).Value = S
End If
Next cell
就这样。
https://stackoverflow.com/questions/67163084
复制相似问题