你好,
我們提供了一個 VBA 程式碼來幫助您逐節對行進行排序,請參閱以下內容:
Sub ExtendOffice_Sort()
Dim xExtendRg, xOfficeSRgC As Range
Dim xRg As Range
Dim xRangeAddress As String
Dim xNum, xCSNum, xCENum, xRSNum, xRSNum2, xRENum As Integer
Dim xRCount As Integer
Dim xBol, xBolWS As Boolean
Dim xStr1, xStr2 As String
Dim xWSh As Worksheet
Dim xSortColumn As Integer
On Error Resume Next
Set xExtendRg = Application.InputBox("Please select the range with data to be sorted:", "ExtendOffice - Kutools for Excel", , , , , , 8)
If xExtendRg Is Nothing Then Exit Sub
Set xOfficeSRgC = Application.InputBox("Please select the column with values to be sorted from smallest to largest:", "ExtendOffice - Kutools for Excel", , , , , , 8)
If xOfficeSRgC Is Nothing Then Exit Sub
xNum = Application.InputBox("Please enter a number of rows to be sorted in a section:", "ExtendOffice - Kutools for Excel", , , , , , 1)
Set xRg = xExtendRg
Set xWSh = xRg.Worksheet
xWSh.Activate
xSortColumn = xOfficeSRgC.Column
xRCount = xRg.Rows.Count
xCSNum = xRg.Column
xCENum = xCSNum + xRg.Columns.Count - 1
xRSNum = xRg.Row
xRENum = xRSNum + xRCount - 1
xRSNum2 = xRSNum
xRSNum = (xRSNum + xNum) - 1
xBol = True
xBolWS = Application.ScreenUpdating
Application.ScreenUpdating = False
Do While xBol
xStr1 = xWSh.Cells(xRSNum2, xCSNum).Address & ":" & xWSh.Cells(xRSNum, xCENum).Address
xStr2 = xWSh.Cells(xRSNum2, xSortColumn).Address & ":" & xWSh.Cells(xRSNum, xSortColumn).Address
'Debug.Print xStr1 & " - " & xStr2
xWSh.Sort.SortFields.Clear
xWSh.Sort.SortFields.Add Key:=Range(xStr2) _
, SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With xWSh.Sort
.SetRange Range(xStr1)
.Header = xlGuess
.MatchCase = False
.Orientation = xlTopToBottom
.SortMethod = xlPinYin
.Apply
End With
xWSh.Sort.SortFields.Clear
If (xRSNum + xNum) >= xRENum Then
If xRSNum = xRENum Then
xBol = False
Else
xRSNum2 = xRSNum + 1
xRSNum = xRENum
End If
Else
xRSNum2 = xRSNum + 1
xRSNum = (xRSNum + xNum)
End If
Loop
Application.ScreenUpdating = xBolWS
End Sub
抱歉,上面的程式碼沒有突出顯示單元格 Q 的最小數量和單元格“I”中的供應商名稱,因為我們最近一直忙於開發我們的產品。
希望你能理解。
阿曼達