簡體   English   中英

當第一行、最后一行、第一列、最后一列僅保存變量時,對 Excel 工作表進行排序

[英]Sorting an Excel Sheet when First Row, Last Row, First Column, Last Column saved only variables

我發現了一些我認為可以幫助我通過 VBA 對工作表進行排序的文章(例如VBA Excel 對特定列的排序范圍),但我無法讓它們工作。

當我進行排序時,我打開了許多工作表,我認為這可能會使問題復雜化。 但是當我設置使用排序的范圍時,我 select 工作表,所以我只是不知道。

如果您查看代碼,您會發現我定義了許多可能有用的變量,因此請隨意使用任何有用的變量。

'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(Cells(FinalFirstRow,FinalFirstColumn), Cells(CurrentFinalRow-1,FinalLastColumn)).Sort _ key1:=Range(Cells(FinalFirstRow,ColNum),Cells(FinalFirstRow,ColNum)), _ order1:=xlAscending, Header:= xl否

對范圍進行排序

' 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM