将整个(姓氏)列转换为小写并删除空格

时间:2019-02-12 12:17:21

标签: excel vba

该代码可以实现预期的功能,但是当我将一个人添加到列表中时,它也应该可以使用。所以问题出在第一块

我尝试将第三行更改为xldown,但这不起作用。

Selection.AutoFill Destination:=Range("C2:xlDown")

我尝试使用Google搜索解决方案,但感到更加困惑。有关代码功能的说明在代码下方。

完整代码:

Sub btn_SortLastName()

'Add content of column B into colum C in lowercase
   Range("C2").Select
   ActiveCell.FormulaR1C1 = "=LOWER(RC[-1])"
   Selection.AutoFill Destination:=Range("C2:C26")


'Copy selection
   Range("C2").Select
   Range(Selection, Selection.End(xlDown)).Select
   Selection.Copy

'Paste selection without formating and remove spaces
   Selection.PasteSpecial Paste:=xlPasteValues
   Selection.Replace What:=" ", Replacement:=""

'Sort in decending order
   Range("C1") = "Index"
   Columns("A:C").Sort key1:=Range("C2"), _
   order1:=xlAscending, Header:=xlYes

'Hide column C and set title on C1
   Columns("C").Select
   Selection.EntireColumn.Hidden = True
   Range("C1").Value = "Hidden"

End Sub

在荷兰语中,有很多用空格分隔的姓氏,我想删除空格,将其复制到隐藏列中,然后将其转换为小写字母并进行排序。

示例:

B2:De Wolf    C2:dewolf
B3:De Bisscop C3:debisscop

1 个答案:

答案 0 :(得分:3)

您应该阅读how to avoid select

尝试一下

Sub btn_SortLastName()

With Range("C2:C" & Range("B" & Rows.Count).End(xlUp).Row)
    .FormulaR1C1 = "=LOWER(RC[-1])"
    .Value = .Value
    .Replace What:=" ", Replacement:=""
End With

Range("C1").Value = "Hidden"
Columns("A:C").Sort key1:=Range("C2"), order1:=xlAscending, Header:=xlYes
Columns("C").EntireColumn.Hidden = True


End Sub