標題:
如何自動比對數字帶出標準值
[打印本頁]
作者:
luke
時間:
2012-4-3 00:04
標題:
如何自動比對數字帶出標準值
各位大大
小弟錄製乙個巨集, 分別篩選出單一數字或多個數字儲存格的值
我想用VBA去比對一個事先做好的標準值, 再帶出結果
煩請先進指導
[attach]10279[/attach]
作者:
hugh0620
時間:
2012-4-3 09:17
本帖最後由 hugh0620 於 2012-4-3 09:19 編輯
回復
1#
luke
用一個最簡單的方式來寫[if]
Sub ex()
For j = 2 To 4 Step 2 '列數,資料要跑第2&4列
For i = 1 To 10 '欄位從A到J
If 30.5 < Cells(j, i) And Cells(j, i) <= 40 Then
Cells(j + 1, i) = 40 '資料放在第3&5列
ElseIf 25.5 < Cells(j, i) And Cells(j, i) <= 30.5 Then
Cells(j + 1, i) = 30
ElseIf 20.5 < Cells(j, i) And Cells(j, i) <= 25.5 Then
Cells(j + 1, i) = 25
ElseIf 16.5 < Cells(j, i) And Cells(j, i) <= 20.5 Then
Cells(j + 1, i) = 20
ElseIf 12.1 < Cells(j, i) And Cells(j, i) <= 16.5 Then
Cells(j + 1, i) = 16
ElseIf 10.1 < Cells(j, i) And Cells(j, i) <= 12.1 Then
Cells(j + 1, i) = 12
ElseIf 8.1 < Cells(j, i) And Cells(j, i) <= 10.1 Then
Cells(j + 1, i) = 10
ElseIf 6.1 < Cells(j, i) And Cells(j, i) <= 8.1 Then
Cells(j + 1, i) = 8
ElseIf 4.1 < Cells(j, i) And Cells(j, i) <= 6.1 Then
Cells(j + 1, i) = 6
ElseIf 2# < Cells(j, i) And Cells(j, i) <= 4.1 Then
Cells(j + 1, i) = 4
ElseIf 0 < Cells(j, i) And Cells(j, i) <= 2.1 Then
Cells(j + 1, i) = 2
End If
Next
Next
End Sub
複製代碼
作者:
register313
時間:
2012-4-3 11:23
回復
1#
luke
加入R6~R16,作為比對依據
[attach]10280[/attach]
Sub XX()
For Each a In [A2:J2,A4:J4]
For Each b In [R6:R16]
If a <= b Then
a.Offset(1, 0) = Int(b)
Exit For
End If
Next
Next
End Sub
複製代碼
作者:
Hsieh
時間:
2012-4-3 14:45
回復
1#
luke
Sub ex()
Dim Ay()
a = Array(0, 2.1, 4.1, 6.1, 8.1, 10.1, 12.1, 16.5, 20.5, 25.5, 30.5)
b = Array(2, 4, 6, 8, 10, 12, 16, 20, 25, 30, 40)
For Each c In [A1:J1]
If InStr(c, ",") > 0 Then
ar = Split(c, ",")
For Each x In ar
ReDim Preserve Ay(s)
Ay(s) = x / [P1]
s = s + 1
Next
d = Application.Max(Ay)
e = Application.Sum(Ay)
Erase Ay: s = 0
Else
d = c / [P1]: e = c / [P1]
End If
f = Application.Lookup(d, a, b)
g = Application.Lookup(e, a, b)
c.Offset(1).Resize(4, 1) = Application.Transpose(Array(d, f, e, g))
Next
End Sub
複製代碼
作者:
luke
時間:
2012-4-3 20:27
回復
3#
register313
非常實用(測試OK)
謝謝"register313"前後兩次的解答
歡迎光臨 麻辣家族討論版版 (http://forum.twbts.com/)