【问题标题】:How do I modify a macro so it sorts?如何修改宏以使其排序?
【发布时间】:2020-09-21 19:50:31
【问题描述】:

我们有一个小型读书俱乐部。我们读了这本书,然后给它评分。
为了跟踪书籍,我使用了一个宏,该宏将分数添加到最新书籍中,然后从最高点到最低点对列表进行排序。
我必须手动更新每本新书的字段。

我最近尝试使用以下方法自动化该过程:
.Cells(.Rows.Count,“A”).End(xlUp).row 的含义,它向上计数空单元格的数量,直到它到达一个包含一些数据的单元格,然后对其执行操作。
修改后的宏一直有效,直到它尝试对列表进行排序然后停止工作。

如何修改排序?

这是读书俱乐部文件:第 1 页。

这是读书俱乐部文件:第 2 页。

这是修改后的宏,在排序停止之前一直有效。

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':对象没有'不支持这个属性或方法

标签: excel vba


【解决方案1】:

我真的很喜欢你的这个小项目 :)! 我花了一些时间重写了代码,以便您了解正在发生的事情以及它如何变得更加稳定和高效。

我想我明白你想要达到的目标。

1 . - 您想定义排名的范围(K 列):

ActiveWorkbook.Worksheets("Position Series").Sort.SortFields.Add Key:=Range( _
        "K7:K" & lastrow), SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:= _
        xlSortNormal

2 。 - 然后对定义范围右侧的列(日期、标题、作者、提议者)进行排序。

With ActiveWorkbook.Worksheets("Position Series").Sort
        .SetRange = Range("C7:K" & lastrow).Select
        .Header = xlGuess
        .MatchCase = False
        .Orientation = xlTopToBottom
        .SortMethod = xlPinYin
        .Apply
End With

这两部分可以改写为:

   Postion_Series.Range("C7:K" & lastrow).Sort key1:=Range("K7:K" & lastrow), _
   order1:=xlDescending, Header:=xlNo

我看到你有以下几行:

Sheets("Position Series").Select

你不需要那个。

首先我们定义您的工作表:

Dim Time_Series As Worksheet
Dim Postion_Series As Worksheet
Set Time_Series = ActiveWorkbook.Worksheets("Time Series")
Set Postion_Series = ActiveWorkbook.Worksheets("Position Series")

因此,每次我们使用变量 Time_Series 时,我们都指的是 Excel 工作表“时间序列”。 因此我们可以重写代码:

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

为了更稳定和更高效的代码,因为您总是希望在这部分中 ActiveSheet 成为“职位系列”工作表,我们可以为此调用我们的变量 Postion_Series

With Postion_Series
    lastrow = .Cells(.Rows.Count, "K").End(xlUp).Row
End With

发生的情况是,我们告诉您的代码,您的代码行应该应用于变量 Postion_Series = 表示“将我引导到工作表“位置系列”并执行代码”,即 Sheets("Position Series").Select


完整修订代码:

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 Time_Series As Worksheet
Dim Position_Series As Worksheet
Dim symbol As String
Dim n As Integer
Dim lastrow As Long

Set Time_Series = ActiveWorkbook.Worksheets("Time Series") 'Define the worksheet "Time Series" to a variable
Set Position_Series = ActiveWorkbook.Worksheets("Position Series") 'Define the worksheet "Position Series" to a variable

'Find the last used row in a Column: column K in this example
    With Position_Series
        lastrow = .Cells(.Rows.Count, "K").End(xlUp).Row 'Find the last row for Column K in the worksheet "Position Series"
    End With
    
    MsgBox lastrow
       
    Dim DataRange As Range

    Set DataRange = Position_Series.Range("C7:K" & lastrow + 50) 'Define the datarange in the Sheet "Position Series" to clear the old data
    DataRange.ClearContents 'Clear the data for the defined range
 
'Find the last used row in a Column: column "Q" in this example
    With Time_Series
        lastrow = .Cells(.Rows.Count, "Q").End(xlUp).Row 'Find the last row for Column Q in the worksheet "Time Series"
    End With
    
    MsgBox lastrow
    
    'Copy column with Score rankings (column Q) from worksheet "Time Series" and paste it to "Position Series" for the column K, "Position"
    Time_Series.Range("Q9:Q" & lastrow).Copy 'Copy column until the last value
    'Paste the column to the new place
    Position_Series.Range("K7").PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    
    'Find the last used row in a Column: column "I" in this example
     With Time_Series
        lastrow = .Cells(.Rows.Count, "I").End(xlUp).Row 'Find the last row for Column I in the worksheet "Time Series"
    End With
    
    
    MsgBox lastrow
    'Copy relevant data about the Date, Title, Author, Propser for from the "Time Series" to the "Position Series"
    Time_Series.Range("C9:I" & lastrow).Copy
    'Paste it to the new place
    Position_Series.Range("C7").PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:=xlNone, SkipBlanks _
        :=False, Transpose:=False
    Application.CutCopyMode = False 'This line will deselect the copy range. I think you should move to hear to deselect the copy range as soon you have pasting your data. So I moved the line to here.
    
    'Find the last row for Column K to decide which row to sort
    With Position_Series
        lastrow = .Cells(.Rows.Count, "K").End(xlUp).Row
        MsgBox lastrow
    End With

    Position_Series.Activate    
        'Sort the range "Range("C7:K" & lastrow)" by the values of column K. From Largest to Smallest, and the first row is not header.
        Position_Series.Range("C7:K" & lastrow).Sort key1:=Position_Series.Range("K7:K" & lastrow), _
        order1:=xlDescending, Header:=xlNo
    
End Sub

【讨论】:

  • 您好 Wizhi,非常感谢您的努力。我将下载您编写的新代码并将其放入宏中,然后将其用于“试驾”。我会在几天后回复你结果。特里
  • 嗨特里!请这样做:)。如果您遇到任何问题或有任何疑问,请随时提出!! :)
  • 您好 Wizhi,对不起,它仍然无法排序。它停在这条线上。 Postion_Series.Range("C7:K" & lastrow).Sort key1:=Position_Series.Range("K7:K" & lastrow), _ order1:=xlDescending, Header:=xlNo.代码为“424”。很抱歉占用您这么多时间。
  • Hi Wizhi...成功!!! .你我的朋友是个天才。我为您的代码更改了代码,然后“繁荣”就来了。我想我的代码中一定有一个小故障,导致它无法执行最终排序。读书俱乐部永远不会知道你的意见有多大用处,因为他们对这个程序没有兴趣,但他们喜欢细细咀嚼我们读过的书籍的统计数据。有多少本书,谁的分数最高,一年中最受欢迎的书是什么,等等。但我会知道你得到了多少帮助。由于我是该网站的新手,我不知道如何奖励积分以获得帮助。非常感谢..
  • 再次感谢乔纳森。我打算留在该网站并尝试提高我的编码技能。如果你到了法国,我很想给你买一瓶(或两瓶)啤酒。也感谢您的图书选择。保重。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-28
相关资源
最近更新 更多