返回列表 上一主題 發帖

大量資料整理

大量資料整理

各位好
如附件及圖1說明
工作表"比對" I3~IN3儲存格及I3~IN3儲存格填入上限數據及下限數據
比對工作表"資料庫"中各列數據是否位於上下限數據中
若在上下限數據中(含上下限)顯示1
若數據大於上限值或小於下限值則顯示0
若上下限數據其中1個為空白,就不進行比對
並將各列結果合計
圖1.jpg

1101005-(上下限).rar (114.53 KB)

回復 1# oak0723-1

借用你的原來寫的程式小修改一下如下,請測試看看,謝謝

Sub test()
Dim Arr, Drr, Brr(), Crr(), T, XMax, XMin, R%, C%, i&
Tm = Timer
With Sheets("資料庫")
    R = .[b6].End(4).Row
    C = .Cells(5, Columns.Count).End(1).Column
    Arr = .Range(.[b6], .Cells(R, 2))
    Drr = .Range(.[i6], .Cells(R, C))
End With
With Sheets("比對")
    .[b6].Resize(UBound(Arr)) = Arr
    ReDim Brr(1 To UBound(Arr), 1 To UBound(Drr, 2))
    ReDim Crr(1 To UBound(Arr), 1 To 1)
    C = .Cells(3, Columns.Count).End(1).Column
    Arr = .Range(.[i3], .Cells(4, C))
    For i = 1 To UBound(Drr)
        For j = 1 To UBound(Arr, 2)
            T = Drr(i, j): XMin = Arr(1, j): XMax = Arr(2, j)
            If XMin = "" Or XMax = "" Then Brr(i, j) = "": GoTo 99
            If T < XMin Or T > XMax Then
                Brr(i, j) = 0
            ElseIf T >= XMin Or T <= XMax Then
                 Brr(i, j) = 1: Crr(i, 1) = Crr(i, 1) + 1
            End If
99:     Next j
    Next i
    .Range("h6").Resize(UBound(Crr)) = Crr
    .Range("i6").Resize(UBound(Brr), UBound(Brr, 2)) = Brr
End With
MsgBox Timer - Tm
End Sub

TOP

Sub 比對()
Dim Brr() As Variant
Dim Arr() As Variant
Dim Crr()
Tm = Timer
Brr = Worksheets("比對").Range("I3:IN4").Value
Arr = Worksheets("資料庫").[I6:IN6].Value
ReDim Crr(1 To UBound(Arr, 2), 1 To 1)
n = 1
For Each s In Arr
    If s >= Brr(1, n) And s <= Brr(2, n) Then
        Crr(n, 1) = 1
    ElseIf Brr(1, n) = "" Or Brr(2, n) = "" Then
        Crr(n, 1) = ""
    Else
        Crr(n, 1) = 0
    End If
    n = n + 1
Next
MsgBox Timer - Tm
End Sub

TOP

本帖最後由 singo1232001 於 2022-6-3 18:24 編輯

回復 1# oak0723-1

Sub 比對db()
T = Timer
Set s = Sheets("比對")
rw = s.Cells(Rows.Count, "i").End(3).Row
  If rw <= 5 Then rw = 5
   s.Range("h5:in" & rw).ClearContents
    ar = s.[I3:IN5]
   
br = Sheets("資料庫").Range("H5:IN" & Sheets("資料庫").Cells(Rows.Count, "i").End(3).Row)
For i = 1 To UBound(br): br(i, 1) = 0: Next
  For i = 1 To UBound(br, 2): br(1, i) = 0: Next
      
For i = 1 To UBound(ar, 2)
k = i + 1
  If ar(1, i) <> "" Or ar(2, i) <> "" Then
      For j = 2 To UBound(br)
        If ar(1, i) <= br(j, k) Then
         If br(j, k) <= ar(2, i) Then
          br(1, k) = br(1, k) + 1
           br(j, 1) = br(j, 1) + 1
            br(j, k) = 1
         Else
          br(j, k) = 0
         End If
        Else
          br(j, k) = 0
        End If
      Next
  Else
    For j = 2 To UBound(br): br(j, k) = "": Next
  End If
Next

br(1, 1) = "合計"
s.[h5].Resize(UBound(br), UBound(ar, 2) + 1) = br
  MsgBox Format(Timer - T, "0.0") & "秒"
End Sub


優化過 怕你量過大

TOP

Sub TEST_A1()
Dim Arr, Brr, Crr, R&, C%, V(3)
With Sheets("資料庫")
     Arr = Range(.Cells(Rows.Count, 2).End(3), .Cells(5, Columns.Count).End(1)(2))
     R = UBound(Arr): C = UBound(Arr, 2)
End With
Brr = Sheets("比對").[b3].Resize(2, C)
ReDim Crr(1 To R, 1 To C)
For j = 8 To C
    V(1) = Brr(1, j): V(2) = Brr(2, j)
    If V(1) = "" Or V(2) = "" Then GoTo j01
    For i = 1 To R
        If j = 8 Then Crr(i, 1) = Arr(i, 1)
        V(3) = Arr(i, j): If V(3) = "" Then GoTo i01
        V(0) = (V(3) >= V(1)) * (V(3) <= V(2))
        Crr(i, j) = V(0)
        Crr(i, 7) = Crr(i, 7) + V(0)
i01: Next i
j01: Next j
Sheets("比對").[b6].Resize(R, C) = Crr
End Sub

TOP

回復 2# samwang [/b

感恩
謝謝

TOP

回復 3# lpk187


    感恩
謝謝

TOP

回復 4# singo1232001


    感恩
謝謝

TOP

回復 2# samwang


若想針對h攔內數值做一個排名
公式如  =IF(H6="",0,SUMPRODUCT(($H$6:$H$500000>=H6)*(1/COUNTIF(H$6:H$500000,H$6:H$500000))))
應該如何?
圖2.jpg

TOP

回復  samwang


若想針對h攔內數值做一個排名
公式如  =IF(H6="",0,SUMPRODUCT(($H$6H$500000>=H6 ...
oak0723-1 發表於 2022-6-5 23:21

新增紅字如下,請測試看看,謝謝
Set xD = CreateObject("Scripting.Dictionary")
...
...
...
     .Range("h6").Resize(UBound(Crr)) = Crr
    .Range("i6").Resize(UBound(Brr), UBound(Brr, 2)) = Brr

'排名
    With .Range(.[h6], .[h6].End(4))
        Arr = .Value
        .Sort Key1:=.Item(1), Order1:=2, Header:=2
        Brr = .Value: .Value = Arr
    End With
    For i = 1 To UBound(Brr)
        T = Brr(i, 1): If Not xD.Exists(T) Then n = n + 1: xD(T) = n
    Next
    For i = 1 To UBound(Arr): Arr(i, 1) = xD(Arr(i, 1)): Next
    .[g6].Resize(UBound(Arr)) = Arr
End With

TOP

        靜思自在 : 【時日莫空過】一個人在世間做了多少事,就等於壽命有多長。因此必須與時間競爭,切莫使時日空過。
返回列表 上一主題