【发布时间】:2020-09-21 19:50:31
【问题描述】:
我们有一个小型读书俱乐部。我们读了这本书,然后给它评分。
为了跟踪书籍,我使用了一个宏,该宏将分数添加到最新书籍中,然后从最高点到最低点对列表进行排序。
我必须手动更新每本新书的字段。
我最近尝试使用以下方法自动化该过程:.Cells(.Rows.Count,“A”).End(xlUp).row 的含义,它向上计数空单元格的数量,直到它到达一个包含一些数据的单元格,然后对其执行操作。
修改后的宏一直有效,直到它尝试对列表进行排序然后停止工作。
如何修改排序?
这是修改后的宏,在排序停止之前一直有效。
Sub PositionIndex()
'
' PositionIndex Macro
' This macro sorts the ongoing position of the most popular books.
'
' Keyboard Shortcut: Ctrl+Shift+X
Dim wb As Workbook
Dim ws As Worksheet
Dim symbol As String
Dim n As Integer
Dim lastrow As Long
Sheets("Position Series").Select
Find the last used row in a Column: column K in this example
With ActiveSheet
lastrow = .Cells(.Rows.Count, "K").End(xlUp).Row
End With
MsgBox lastrow
Dim DataRange As Range
Set DataRange = Range("C7:K" & lastrow)
DataRange.Select
Selection.ClearContents
Sheets("Time Series").Select
'Find the last used row in a Column: column "Q" in this example
With ActiveSheet
lastrow = .Cells(.Rows.Count, "Q").End(xlUp).Row
End With
MsgBox lastrow
Range("Q9:Q" & lastrow).Select
Selection.Copy
Sheets("Position Series").Select
Range("K7").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Time Series").Select
'Find the last used row in a Column: column "I" in this example
With ActiveSheet
lastrow = .Cells(.Rows.Count, "I").End(xlUp).Row
End With
MsgBox lastrow
Range("C9:I" & lastrow).Select
Selection.Copy
Sheets("Position Series").Select
Range("C7").Select
Selection.PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
:=False, Transpose:=False
Sheets("Position Series").Select
With ActiveSheet
lastrow = .Cells(.Rows.Count, "K").End(xlUp).Row
End With
MsgBox lastrow
Range("C7:K" & lastrow).Select
Application.CutCopyMode = False
ActiveWorkbook.Worksheets("Position Series").Sort.SortFields.Clear
ActiveWorkbook.Worksheets("Position Series").Sort.SortFields.Add Key:=Range( _
"K7:K" & lastrow), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
xlSortNormal
With ActiveWorkbook.Worksheets("Position Series").Sort
.SetRange = Range("C7:K" & lastrow).Select
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
Range("A2").Select
Sheets("Time Series").Select
Range("A2").Select
End Sub
【问题讨论】:
-
当它“停止”时究竟会发生什么?
-
我只需要一个数据表,其中记录并添加了书籍列表 - 这意味着它可以保持时间顺序。然后是一个基于 large() 和 index() 的输出表,它总是从最高到最低的顺序,我也会使用 iferror() 以便输出表显示空白而不是错误。没有宏出错。
-
宏将运行正常,直到到达该行: .SetRange = Range(“C7 :K” & lastrow).select 然后停止:运行时错误'438':对象没有'不支持这个属性或方法