Excel 2003:不同工作簿中的编程排序表

时间:2012-01-09 16:32:58

标签: vba excel-vba excel-2003 excel

从当前工作簿中,我想在源工作簿中打开一个工作表,关闭可能存在的任何过滤,按一列排序,然后按另一列排序,然后按另一列排序。我得到了1004和其他错误。这需要在2003年运行。任何帮助将不胜感激!

Dim WB As Workbook, WasWBOpen As Boolean, srcfile As String, srcpath As String,    
onecol as integer, twocol as integer, thrcol as integer

srcpath = "blahblah"
srcfile = "blah.xls"

On Error Resume Next
Set WB = Workbooks(srcfile)
WasWBOpen = True
On Error GoTo 0
If WB Is Nothing Then
Set WB = Workbooks.Open(srcpath & srcfile, UpdateLinks:=False)
WasWBOpen = False
End If

'code before this opens source wkbook

lstrow = Worksheets("Sheet1").UsedRange.Row - 1 + Worksheets("Sheet1").UsedRange.Rows.Count
lstcol = Worksheets("Sheet1").UsedRange.Column - 1 + Worksheets("Sheet1").UsedRange.Columns.Count

onecol=3
twocol=5
thrcol=8

With WB.Sheets("Sheet1")

.AutoFilterMode = False
.Range("1:1").AutoFilter
'Here's where error occurs--
.Range(Cells(1, 1), Cells(lstrow, lstcol)).Sort _
    Key1:=Columns(onecol), Order1:=xlAscending, _
    Key2:=Columns(twocol), Order2:=xlAscending, _
    Key3:=Columns(thrcol), Order3:=xlAscending, Header:=xlYes

End With

If WasWBOpen = False Then
WB.Close
End If

3 个答案:

答案 0 :(得分:1)

根据我的经验,您只能对活动工作表进行排序。尝试在.Activate之后添加WB.Sheets("Sheet1")

有关最后一种排序的信息存储在工作表中。我有时怀疑这是问题而不是要排序的表不活跃。但.Activate一直对我有用,所以我从未进一步调查过。

额外信息

我原以为是生成错误的Sort,但它是AutoFilter

我可以通过将第1行留空来生成1004错误。

AutoFilter声明的目的是什么?使用AutoFilterMode = False我希望它返回Nothing。为什么不删除这个陈述?

我也很担心你要排序的范围。您将在左侧的顶部和未使用的列中减去未使用的行数来计算lstrow和lstcol,然后在排序中包括那些未使用的行和列。结果是底部的行和右侧的列不会被排序。

如果左侧没有任何未使用的行,而左侧未使用的列,则无关紧要,但您需要确定要排序的范围。

额外信息2

在我发现破坏原始代码的第四种方法之后添加了这一部分。以下代码似乎是防弹的。

Option Explicit
Sub TestSort2()

Dim WB As Workbook, WasWBOpen As Boolean, srcfile As String, srcpath As String
Dim onecol As Integer, twocol As Integer, thrcol As Integer

' Undeclared or new variables

Dim InxWB As Long
Dim lstrow As Long
Dim lstcol As Long
Dim srcpathfile As String

' Report the name of the active workbook
Debug.Print "Original active workbook " & ActiveWorkbook.Name

' I created two workbooks named "Failed sort 1.xls" and "Failed sort 2.xls".
' Both are in the same directory. "Failed sort 1.xls" contains this macro.
' "Failed sort 2.xls" contains the data.
srcpath = Application.ActiveWorkbook.Path
srcfile = "Failed sort 2.xls"

srcpathfile = srcpath & "\" & srcfile

WasWBOpen = False
' Check the open workbook for srcfile
For InxWB = 1 To Workbooks.Count
  If Workbooks(InxWB).Name = srcfile Then
    ' Required workbook already open
    Set WB = Workbooks(InxWB)
    WB.Activate                     ' Activate it
    WasWBOpen = True
    Exit For
  End If
Next
If Not WasWBOpen Then
  ' Files was not open
  If Dir(srcpathfile) <> "" Then
    ' File exists
    ' Do you need UpdateLinks:=False?  If there are links
    ' with the sort be affected if they are not updated? 
    Set WB = Workbooks.Open(srcpathfile, UpdateLinks:=False)
  Else
    ' File does not exist
    Call MsgBox(srcpathfile & " does not exist", vbOKOnly)
    Exit Sub
  End If
End If

' WB is now the active workbook whether it was open before or not
Debug.Print "Final active workbook " & ActiveWorkbook.Name      ' Confirm

With Sheets("Sheet1")
  .Activate
  .AutoFilterMode = False

  ' Get the last used row and cell of the worksheet
  lstrow = Cells.SpecialCells(xlCellTypeLastCell).Row
  lstcol = Cells.SpecialCells(xlCellTypeLastCell).Column

  onecol = 3
  twocol = 5
  thrcol = 8

  If onecol > lstcol Or twocol > lstcol Or thrcol > lstcol Then
    Call MsgBox("The sort range does include the sort columns", vbOKOnly)
    If Not WasWBOpen Then
      Close
    End If
    Exit Sub
  End If

  Range(Cells(1, 1), Cells(lstrow, lstcol)).Sort _
        Key1:=Columns(onecol), Order1:=xlAscending, _
        Key2:=Columns(twocol), Order2:=xlAscending, _
        Key3:=Columns(thrcol), Order3:=xlAscending, Header:=xlYes
End With

If Not WasWBOpen Then
  Close
End If

End Sub

答案 1 :(得分:1)

我唯一可以做的就是你的选择可能不包括第3列,第5列或第8列。错误之前lstrowlstcol的值是多少?

答案 2 :(得分:1)

.Range(Cells(1, 1), Cells(lstrow, lstcol)).Sort _ 

最好写成:

.Range(.Cells(1, 1), .Cells(lstrow, lstcol)).Sort _