如何将A列转换为多个工作表中的文本

时间:2019-06-24 23:11:12

标签: excel vba

如果我手动将单元格从数字更改为文本,则它会从784768956303更改为7.84769E + 11

因此,我目前正在使用下面的代码将数字字段转换为当前工作表上的文本。

public int maxGain(TreeNode currNode, int[] res) {
        if(currNode == null) { return 0; }
        int leftBestSum = Math.max(maxGain(currNode.left, res), 0);
        int rightBestSum = Math.max(maxGain(currNode.right, res), 0);
        // update best result if it's better to start a new path
        int currNodeAndChildSum = currNode.val + leftBestSum + rightBestSum;
        // if currSum is better than the best result, start new path
        res[0] = Math.max(res[0], currNodeAndChildSum);
        // else if currSum is not better than the best result, pass back the best result path to
        // its parent for later compare use
        int currBestPathSum = currNode.val + Math.max(leftBestSum, rightBestSum);
        return currBestPathSum;
    }
    public int maxPathSum(TreeNode root) {
        int[] res = new int[] {Integer.MIN_VALUE};
        maxGain(root, res);
        return res[0];
    }

这似乎有些矫kill过正,但是如果我使用

Const MaxTextLength = 255

Sub ConvertColumnAtoText()
'    StartNewTask "Converting all cells to text"
    Application.ScreenUpdating = False

    Dim MyRange As Range
    Set MyRange = ActiveSheet.UsedRange.Columns("A")
    Dim cache() As Long
    cache = GetColumnWidths(MyRange)

    With MyRange
        .ColumnWidth = MaxTextLength
        Dim Values() As Variant
        ReDim Values(.Rows.Count, .Columns.Count)

        Dim col As Long
        Dim row As Long

        For row = 0 To UBound(Values, 1)
            For col = 0 To UBound(Values, 2)
                Dim temp As String
                temp = .Cells(row + 1, col + 1).Text
                If Len(temp) <= MaxTextLength Then
                    Values(row, col) = temp
                End If
            Next col
        Next row
        .NumberFormat = "@"
    End With

    MyRange = Values
    SetColumnWidths MyRange, cache
    Application.ScreenUpdating = True
End Sub

Private Function GetColumnWidths(Target As Range) As Long()
    Dim output() As Long
    ReDim output(1 To Target.Columns.Count)
    Dim index As Long
    For index = 1 To Target.Columns.Count
        output(index) = Target.Columns(index).ColumnWidth
    Next index
    GetColumnWidths = output
End Function

Private Sub SetColumnWidths(Target As Range, widths() As Long)
    Dim index As Long
    For index = LBound(widths) To UBound(widths)
        Target.Columns(index).ColumnWidth = widths(index)
    Next index
End Sub

OR

Columns("A:A").NumberFormat = "@"

我仍然收到7.84769E + 11

我想做的是运行脚本以转换3个不同工作表上A列的使用范围

我知道我需要更改此位

[A:A].Select
With Selection
Dim col As Long
    .NumberFormat = "@"
    .Value = .Value
End With

但是我相信我的范围不能覆盖多张纸。

一旦所有这些工作正常,我想将其添加到已经调用3个不同宏的按钮中,并在上一个宏完成后按顺序运行所有内容。

Set MyRange = ActiveSheet.UsedRange.Columns("A")

但是,我上面的原始代码有2个SUB和一个功能(一个SUB且该功能为Private)。我需要将按钮更改为

Sub Button1_Click()
    Call courier1 'Macro1
    Call courier2 'Macro2
    Call courier3 'Macro3
    MsgBox "Done"
End Sub

谢谢

1 个答案:

答案 0 :(得分:2)

假设您的excel文件只有3个工作表,下面的代码将遍历所有工作表:

Sub ConvertColumnAtoText()
'    StartNewTask "Converting all cells to text"
    Application.ScreenUpdating = False

    Dim WS As Worksheet 'new line added

    Dim MyRange As Range

    For Each WS In Worksheets ' new line added

        WS.Activate ' new line added

        Set MyRange = ActiveSheet.UsedRange.Columns("A")
        Dim cache() As Long
        cache = GetColumnWidths(MyRange)


        With MyRange
            .ColumnWidth = MaxTextLength
            Dim Values() As Variant
            ReDim Values(.Rows.Count, .Columns.Count)

            Dim col As Long
            Dim row As Long

            For row = 0 To UBound(Values, 1)
                For col = 0 To UBound(Values, 2)
                    Dim temp As String
                    temp = .Cells(row + 1, col + 1).Text
                    If Len(temp) <= MaxTextLength Then
                        Values(row, col) = temp
                    End If
                Next col
            Next row
            .NumberFormat = "@"
        End With

        MyRange = Values
        SetColumnWidths MyRange, cache
    Next WS ' new line added

    Application.ScreenUpdating = True
End Sub

关于其他人