如何计算单元格中的整数并在VBA中进行排序?

时间:2014-10-01 09:22:21

标签: excel vba

我有一个具有不同数值的列。有些格式:

0.56
1.52

有些格式如:

6,352.00

要在Excel中进行计算,我希望在数字格式为6,352.00时替换"," with "",而替换所有其他替换"." with ",".

所以在结果中我应该得到:

0,56
1,52
6352,00

然后从Max到Min对它们进行排序。

我试着写一个VBA代码(这不是真的正确),也许有人可以帮忙吗?

Sub ChangeFormat1()
    Dim lngNumberOfCharacters As Long
    Set ws4 = ActiveWorkbook.Sheets("atm_hh")

    ws4.Select
    Columns("C:C").Select

lngNumberOfCharacters = Len("C:C")

    If lngNumberOfCharacters > 8
    Selection.Replace What:=",", Replacement:="", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
       Else
           Selection.Replace What:=".", Replacement:=",", LookAt:=xlPart, _
        SearchOrder:=xlByRows, MatchCase:=False, SearchFormat:=False, _
        ReplaceFormat:=False
        End If

    ActiveWorkbook.Worksheets("atm_hh").Sort.SortFields.Clear
    ActiveWorkbook.Worksheets("atm_hh").Sort.SortFields.Add Key:=Range("C1"), _
        SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
    With ActiveWorkbook.Worksheets("atm_hh").Sort
        .SetRange Range("A2:D66842")
        .Header = xlNo
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
    End With

        End Sub

1 个答案:

答案 0 :(得分:0)

如果您真的想使用VBA

Sub ChangeFormat1()

Application.ScreenUpdating = False


Dim ws4 As Worksheet
Set ws4 = Sheets("atm_hh")

' If you only want to replace in column A for example
With ws4.Columns("A:A")

    .Replace What:=",", Replacement:="", LookAt:=xlPart, SearchOrder:=xlByRows, _
            MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False

    .Replace What:=".", Replacement:=",", LookAt:=xlPart, SearchOrder:=xlByRows, _
            MatchCase:=False, SearchFormat:=False, ReplaceFormat:=False

End With

Application.ScreenUpdating = True

End Sub