我们如何使用宏将数字转换为文本?

时间:2016-10-18 06:24:56

标签: excel vba excel-vba

我想在将逻辑连接到列之前将列E转换为文本格式。

目前,E栏同时包含文字和数字值。

现有代码:

Private Sub CommandButton1_Click()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim lrw As Integer
Dim rgn As Range
Dim x As Integer
Dim str As String


lrw = ActiveSheet().Range("A1").End(xlDown).Row
Set rgn = Range("A1:E" & lrw)
rgn.Select
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("A2:A100000") _
    , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortTextAsNumbers   '1th order by ID
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("C2:C100000") _
    , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal        '2th order by date
ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=Range("D2:D100000") _
    , SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortTextAsNumbers   '3th order by Counter

With ActiveWorkbook.Worksheets("Sheet1").Sort
    .SetRange rgn
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin

    .Apply
End With
    For i = 2 To lrw
        x = Application.WorksheetFunction.CountIfs(Worksheets("Sheet1").Range("E2:E" & lrw), "*", Worksheets("Sheet1").Range("A2:A" & lrw), Cells(i, 1), Worksheets("Sheet1").Range("C2:C" & lrw), Cells(i, 3))
        If x > 1 Then

            cmnts = CStr(Cells(i, 5))
          For J = 1 To x - 1
          cmnts = cmnts & " " & CStr(Cells(i + J, 5))
            Rows(i + J).Select
            Selection.ClearContents
            Next J
            Cells(i, 5) = cmnts
        End If
     Next i
With ActiveWorkbook.Worksheets("Sheet1").Sort
    .SetRange rgn
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlPinYin
    .Apply
End With
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic

End Sub

我想在将逻辑连接到列之前将列E转换为文本格式。

2 个答案:

答案 0 :(得分:1)

试试这个

Sub Example()
    With ActiveWorkbook.Worksheets("Sheet1").Range("E:E")
        .NumberFormat = "@"
    End With
End Sub

答案 1 :(得分:0)

尝试使用CStr()功能。