簡體   English   中英

如何在 VBA 的范圍數組中引用單個單元格以對其進行排序?

[英]How do I reference a single cell within an array of ranges in VBA to sort by it?

所以我有幾個不同的組件,其中一些在不同列的同一張表中列出了不同的子組件,如下所示:

1    C
2    B
           bi            10%
           bii           30%
           biii          60%
3    A
4    D
           di            20%
           dii           80%
etc.

我想要做的是按字母順序按 A、B、C、D 對它們進行排序,而不會弄亂添加的信息。 現在我有一個代碼來計算每個元素,適當地調整數組大小,然后通過選擇每個項目的所有關聯行並循環范圍在每個范圍內循環。但是,當我嘗試排序時,我似乎無法得到它工作。 知道我怎么能 go 關於這個? 謝謝。


Dim everything() As Range
Dim check As Range
Dim count As Range

Dim lr As Long
Dim x As Long
Dim y As Long
Dim z As Long
Dim q As Long

Dim Temptxt1 As String
Dim Temptxt2 As String

y = 0
z = 0
q = 0
With ThisWorkbook.Sheets("Names and Vendors")
lr = .Cells(.Rows.count, "B").End(xlUp).Row

    'Counts number of elements to size the "everything" array
    For z = 2 To lr
    Set count = .Range("B" & z)
        If IsEmpty(count) = False Then
            q = q + 1
            End If
    Next z
    ReDim everything(z) As Range 'Resizes array

    'Loops all RM info into array by each distinct range
    For x = 2 To lr
        Set check = .Range("B" & x).EntireRow
        If IsEmpty(.Range("B" & 1 + x)) = True Then
            Do While IsEmpty(.Range("B" & 1 + x)) = True And x < lr
                    Set check = Union(check, .Range("B" & 1 + x).EntireRow)
                    x = x + 1
                Loop
        End If
        Set everything(y) = check
        y = y + 1
        Next x

    'This is where the code breaks. It gives me a type mismatch.
    For x = LBound(everything) To UBound(everything)
    For y = x To UBound(everything)
      If UCase(everything(y)) < UCase(everything(x)) Then
        Temptxt1 = everything(x).Range(1, 2)
        Temptxt2 = everything(y).Range(1, 2)
        everything(x) = Temptxt2
        everything(y) = Temptxt1
      End If
     Next y
  Next x
End With
End Sub

我這樣做的方式有點厚顏無恥:

Dim sorting As Range
Dim everything() As Range

'Values looped into everything() in different code not pasted here
'y is likewise calculated elsewhere

Set sorting = everything(y)

sorting.Offset(0, 1).Select
ActiveCell.Select

'The two values I need to sort by are below
Temp1 = "" & ActiveCell.Value
ven1 = "" & ActiveCell.Offset(0, 1).Value

暫無
暫無

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

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