【问题标题】:Sorting an Excel Sheet when First Row, Last Row, First Column, Last Column saved only variables当第一行,最后一行,第一列,最后一列仅保存变量时对Excel工作表进行排序
【发布时间】:2021-08-26 19:20:32
【问题描述】:

我发现了一些我认为可以帮助我通过 VBA 对工作表进行排序的文章(例如 VBA Excel sort range by specific column),但我就是无法让它们发挥作用。

当我进行排序时,我打开了许多工作表,我认为这可能会使问题复杂化。但是当我设置使用排序的范围时,我选择了工作表,所以我不知道。

如果您查看代码,您会发现我定义了许多可能有用的变量,因此请随意使用任何有用的变量。

'These are created elsewhere as Public, or passed into Sub
Dim SourceSheet As Worksheet
Dim MyWB As String
Dim MyWS As String
Dim ColNum As Integer
Dim SourceFirstRow As Integer
Dim SourceLastRow As Integer
Dim FinalFirstRow As Integer
Dim FinalFirstColumn As Integer
Dim FinalLastColumn As Integer

'These are created and used in the subroutine
Dim FinalSheet As Worksheet
Dim AllCells As Range
Dim SortColumn As Range
Dim SourceRow As Integer
Dim CurrentFinalRow As Integer

'These are actually set elsewhere and passed in, shown here for example
Set SourceSheet = ThisWorkbook.Worksheets(1)
Set SourceFirstRow = 2
Set SourceLastRow = 15
Set MyWB = "DataFile"
Set MyWS = "Data"
Set FinalFirstRow = 2
Set FinalFirstColumn = 1
Set FinalLastColumn = 20
Set ColNum = 4

'These is the relevant code from the subroutine
Set FinalSheet = Workbooks(MyWB).Worksheets(MyWS)
Set CurrentFinalRow = FinalFirstRow
Set AllCells = FinalSheet.Cells
Set SortColumn = FinalSheet.Columns(ColNum)

For SourceRow = SourceFirstRow To SourceLastRow
    'Fill in a whole bunch of data into the rows and columns on the final sheet
    'not all sourcerows are transferred
    CurrentFinalRow = CurrentFinalRow + 1 'Sets it ready for the next blank line to be filled
Next SourceRow

Range(AllCells & CurrentFinalRow - 1).Sort key1:=Range(SortColumn & CurrentFinalRow - 1), order1:=xlAscending, Header:=xlYes

【问题讨论】:

  • Range(AllCells & CurrentFinalRow - 1) 看起来有点不对劲。你想在这里做什么?如果您要传递行号,那么您需要在其前面有一个列字母。 AllCellsRange
  • 我在链接到的帖子中使用了 Dmitry Pavliv 的代码。他使用 lastrow 以及一个范围,所以我认为你需要定义范围的“底部”。不是这样吗?
  • Range(AllCells).Sort key1:=Range(SortColumn), order1:=xlAscending, Header:=xlYes 也不起作用。 :(
  • 了解如何使用Range 及其参数可能会很有用。
  • 有很好的参考资料吗?我查看了 5 个关于 Range 的不同网站,但无法理解它们。

标签: excel vba sorting range


【解决方案1】:

因此,通过进一步研究和上面发布的代码,答案是:

范围(单元格(FinalFirstRow,FinalFirstColumn),单元格(CurrentFinalRow-1,FinalLastColumn))。排序_ key1:=范围(单元格(FinalFirstRow,ColNum),单元格(FinalFirstRow,ColNum)),_ order1:=xlAscending, Header:=xlNo

【讨论】:

    【解决方案2】:

    对范围进行排序

    ' Declare all whole numbers as 'Long', no need to use 'Integer'.
    ' The 'Set' keyword is used for objects like workbooks, worksheets
    ' and ranges. Remove it from the other variable types.
    ' Set or add values to the variables close to their declarations.
    ' Qualify your objects: 'FinalSheet.Range(...)' vs 'Range(...)'.
    ' Use variables to make the code more readable ('drg').
    
    'These are actually set elsewhere and passed in, shown here for example
    Dim SourceSheet As Worksheet: Set SourceSheet = ThisWorkbook.Worksheets(1)
    Dim SourceFirstRow As Long: SourceFirstRow = 2
    Dim SourceLastRow As Long: SourceLastRow = 15
    Dim MyWB As String: MyWB = "DataFile.xlsx" ' or whatever extension
    Dim MyWS As String: MyWS = "Data"
    Dim FinalFirstRow As Long: FinalFirstRow = 2
    Dim FinalFirstColumn As Long: FinalFirstColumn = 1
    Dim FinalLastColumn As Long: FinalLastColumn = 20
    Dim ColNum As Long: ColNum = 4
    
    'These is the relevant code from the subroutine
    Dim FinalSheet As Worksheet
    Set FinalSheet = Workbooks(MyWB).Worksheets(MyWS)
    Dim CurrentFinalRow As Long: CurrentFinalRow = FinalFirstRow
    Dim AllCells As Range: Set AllCells = FinalSheet.Cells ' useless
    Dim SortColumn As Range: Set SortColumn = FinalSheet.Columns(ColNum) ' useless
    
    Dim SourceRow As Long
    For SourceRow = SourceFirstRow To SourceLastRow
        'Fill in a whole bunch of data into the rows and columns on the final sheet
        'not all source rows are transferred
        CurrentFinalRow = CurrentFinalRow + 1 'Sets it ready for the next blank line to be filled
    Next SourceRow
    
    Dim drg As Range
    With FinalSheet
        Set drg = .Range(.Cells(FinalFirstRow, FinalFirstColumn), _
            .Cells(CurrentFinalRow - 1, FinalLastColumn))
    End With
    
    drg.Sort Key1:=drg.Cells(ColNum), Order1:=xlAscending, Header:=xlNo
    

    【讨论】:

      猜你喜欢
      • 2023-02-02
      • 2017-10-23
      • 1970-01-01
      • 1970-01-01
      • 2015-06-24
      • 2019-03-24
      • 2017-07-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多