简体   繁体   中英

vba excel : sorting other workbooks

Sort command works when the excel macro doesn't open a new worksheet. The same sort command does not work if the excel macro is trying to manipulate a worksheet which was opened by the macro.

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

Statement used to open workbook

Workbooks.Open (Cells(1, 1))

I'm assuming you have a link to a workbook in Cell (1,1)? If so try:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM