Excel 2010双变量运行时错误16

时间:2013-03-12 15:19:14

标签: excel-vba runtime-error excel-2010 vba excel

我们在办公室的一台用户计算机上运行此代码时出现问题 - 所有其他用户计算机运行正常(Windows XP操作系统,Excel 2010标准版或专业版) - 此计算机是运行Excel 2010 Professional的Windows XP。运行时错误16出现在标记为 - >的行上。问题似乎是变量i - 突出显示提示显示i = -1。#IND

Sub FormatSheet(strResultSheet As String)
    Dim oCol As Excel.Range
    Dim i As Double
    Dim R As String
    Dim iColumn As Integer

    ' Special rountine to convert text column into numeric
    Sheets(strResultSheet).Select
    iColumn = 0
--> For i = 1 To Worksheets(strResultSheet).Cells.SpecialCells(xlLastCell).Column
        If UCase(Cells(1, i).Text) = "QUANTITY" Then
            iColumn = i
            Exit For
        End If
    Next
    Sheets(strResultSheet).Select
    If iColumn > 0 Then
        Columns(iColumn).Select
        Selection.NumberFormat = "#,##0.00"
        Selection.HorizontalAlignment = xlHAlignRight
        For i = 2 To Sheets(strResultSheet).Cells.SpecialCells(xlLastCell).Row
            If Cells(i, iColumn).Text <> "" Then
                Cells(i, iColumn).Value = Cells(i, iColumn).Value * 1
            End If
        Next
    End If

End Sub

任何人都知道我们需要做些什么来修复用户机器来处理?宏嵌入在第三方日常电子邮件中,因此无法调整代码进行修复。

1 个答案:

答案 0 :(得分:0)

尝试以下代码:

Sub FormatSheet(strResultSheet As String)

    Dim rng As Range, lastRow As Integer

    With Sheets(strResultSheet)
        .Select
        Set rng = .Rows("1:1").Find("QUANTITY")

        If Not rng Is Nothing Then
            rng.EntireColumn.NumberFormat = "#,##0.00"
            rng.HorizontalAlignment = xlHAlignRight

            lastRow = Cells(65000, rng.Column).End(xlUp).Row
            For i = 2 To lastRow
                Cells(i, rng.Column).Value = Cells(i, rng.Column).Value * 1
            Next
        End If
    End With

End Sub