Excel"溢出" 500,000行的错误

时间:2015-02-27 15:17:41

标签: excel vbscript

我有一张包含500,000行的Excel表格。它有以下格式的3列

 Staff    Locations     Roles
   1      Location1     Role1
   1      Location2     Role1
   2      Location2     Role2
   3      Location3     Role3
   3      Location3     Role4

输出格式如下

 Staff      Locations                 Roles
   1        Location1, Location2      Role1
   2        Location2                 Role2
   3        Location3                 Role3
   3        Location3                 Role4

如果行数很小,我有以下我正在使用的vbscript。

Sub Sort_Duplicates()

Dim lngRow, lngRow2 As Long

With ActiveSheet
    Dim flag As Integer: flag = 0
    Dim i As Integer
    Dim columnToMatch As Integer: columnToMatch = 1
    Dim column2ToMatch As Integer: column2ToMatch = 3
    Dim columnToConcatenate As Integer: columnToConcatenate = 2

    lngRow = .Cells(538537, columnToMatch).End(xlUp).Row
    .Cells(columnToMatch).CurrentRegion.Sort key1:=.Cells(columnToMatch), Header:=xlYes

    Do
        If .Cells(lngRow, columnToMatch) = .Cells(lngRow - 1, columnToMatch) Then
         'flag = 1
         i = 1
         lngRow2 = lngRow
         Do While Cells(lngRow2, columnToMatch) = .Cells(lngRow2 - i, columnToMatch)
          If .Cells(lngRow2, column2ToMatch) = .Cells(lngRow2 - i, column2ToMatch) Then
            .Cells(lngRow2, columnToConcatenate) = .Cells(lngRow2, columnToConcatenate) & ", " & .Cells(lngRow2 - i, columnToConcatenate)
            .Rows(lngRow2 - i).Delete
          End If
         i = i + 1
         Loop
         lngRow2 = lngRow2 - 1
        End If

       ' If flag = 1 Then
       '  lngRow = lngRow2
       '  flag = 0
       ' Else
         lngRow = lngRow - 1
        'End If
    Loop Until lngRow = 1
End With
End Sub

当我尝试使用500,000行的工作表时,它会开始排序,但会抛出弹出错误“溢出”而没有其他消息。我尝试用90,000这样做,仍然是相同的。有什么建议我可以解决这个问题吗?

提前致谢

1 个答案:

答案 0 :(得分:1)

首先将所有 整数变量的 Dim 更改为 Long