【问题标题】:Prevent string array from being reformatted to scientific防止字符串数组被重新格式化为科学格式
【发布时间】:2015-02-04 19:36:51
【问题描述】:

我正在研究一个宏,它采用一列单元格,其中包含由“”分隔的标准列表,如果它们是唯一值,则将它们添加到数组中。我询问用户他们想将创建的数组放在哪里,然后将其粘贴到指定位置。

问题 - 几乎所有标准都以“1E”开头,每当将数组粘贴到工作表中时,任何末尾没有字母的值都会转换为科学计数法。之后我无法将它们格式化为文本,因为它会破坏原始值。例如,数组中的“1E0011”粘贴为“1.00E+11”。如果我将其格式化为文本(粘贴后),则显示为“1E+11”。数组作为字符串变暗。

Sub Remove_1E_Duplicates()
'Looks at every "1E" standard in a column and puts every unique value in an array at a user-selected destination

Dim CurrentCell As String               'Used to temporarily store active cell's values
Dim strArray() As String                'Final string with only unique values
Dim tempArray() As String               'Temp array created by splitting CurrentCell's values
Dim i As Integer                        'Counter used for each value in tempArray
Dim elem As Integer                     'Counter for each element in strArray
Dim lLoop As Long, lLoop2 As Long       'Counter for sorted array (ex: if lLoop=5, each element 0-4 is already sorted and 5+ needs to be sorted)
Dim str1 As String, str2 As String      'Place holder for switching two elements' positions in an array when one has a lower sorted value
Dim rListPaste As Range                 'Stores location of the user-input destination

ReDim Preserve strArray(0)

Do While ActiveCell <> vbnullvalue

    If ActiveCell.Value = "Access Denied" Then
        GoTo Denied
    End If

    CurrentCell = ActiveCell.Value
    tempArray = Split(CurrentCell, " ")

    For i = LBound(tempArray) To UBound(tempArray)

        For elem = LBound(strArray) To UBound(strArray)
            'Used if strArray is empty
            If strArray(elem) = "" Then
                strArray(elem) = tempArray(i)
                GoTo NextTemp

            'If the current element in tempArray is already in strArray, go to next element in tempArray
            ElseIf InStr(tempArray(i), strArray(elem)) Then
                GoTo NextTemp

            'If all the elements in strArray have been searched and the element in tempArray is longer than 2 characters, _
            'then resize strArray and add the current tempArray element to strArray (after trimming extra spaces and returns)
            ElseIf elem = UBound(strArray) And Len(tempArray(i)) > 2 And tempArray(i) <> Chr(13) Then
                ReDim Preserve strArray(LBound(strArray) To UBound(strArray) + 1) As String
                strArray(UBound(strArray)) = Replace(Trim(tempArray(i)), vbLf, "")
            End If

        Next elem

NextTemp:       'Goes to next element in tempArray
    Next i

Denied:         'After both arrays have been compared, goto next cell below current active cell
    ActiveCell.Offset(1, 0).Select

Loop

'This loop sorts the strArray array
For lLoop = 0 To UBound(strArray)

    For lLoop2 = lLoop To UBound(strArray)

         If UCase(strArray(lLoop2)) < UCase(strArray(lLoop)) Then
             str1 = strArray(lLoop)
             str2 = strArray(lLoop2)
             strArray(lLoop) = str2
             strArray(lLoop2) = str1
         End If

    Next lLoop2

Next lLoop

Set rListPaste = Application.InputBox _
    (prompt:="Please select destination cell", Type:=8)

i = UBound(strArray) + 1
Range(rListPaste, rListPaste.Offset(i)).Value2 = WorksheetFunction.Transpose(strArray)

End Sub

【问题讨论】:

  • 这些不是数字,为什么不能将它们格式化为字符串?也可以用撇号作为首字母吗?
  • 请分享您正在使用的代码。这将有助于为您所看到的问题提供更好的答案。例如,了解宏如何分离数据很重要。
  • 在每个值前面加上'
  • 已发布代码。我将如何添加撇号?像:“'” & tempArray(i) ?

标签: excel vba


【解决方案1】:

我没有在每个值前面加上',而是在之前将数组放入目标范围而不是之后格式化文本的目标范围。我用Range(rListPaste, rListPaste.Offset(lLoop - 1)).NumberFormat = "@"。这将消除对另一个循环步骤的需要。感谢您的帮助!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-29
    • 1970-01-01
    • 2012-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多