vba excel:排序其他工作簿

时间:2012-11-11 08:35:46

标签: excel-vba vba excel

当excel宏未打开新工作表时,

排序命令有效。如果excel宏试图操作由宏打开的工作表,则相同的排序命令不起作用。

With ActiveWorkbook.Worksheets("Sheet").Sort
  .SetRange Range("A1:D" & replacementRow - 1)
  .Header = xlNo
  .MatchCase = False
  .Orientation = xlTopToBottom
  .SortMethod = xlPinYin
  .Apply
End With

用于打开工作簿的语句

Workbooks.Open (Cells(1, 1))

1 个答案:

答案 0 :(得分:1)

我假设你有一个Cell(1,1)工作簿的链接?如果是这样,试试:

Dim wb As Workbook
Set wb = Workbooks.Open(Cells(1, 1))

With wb.Worksheets("Sheet1").Sort
  .SetRange Range("A1:D" & replacementRow - 1)
  .Header = xlNo
  .MatchCase = False
  .Orientation = xlTopToBottom
  .SortMethod = xlPinYin
  .Apply
End With