我有一个标签,我在其中使用VBA运行多项检查'如果'声明。我想添加一些检查这个的东西,但我似乎无法找到一个简单的方法来做到这一点。我的文件看起来像:
A B
1 Yellow Banana
2 Yellow Banana
3 Blue Banana
4 Red Tomato
5 Orange Tomato
6 Green Pear
7 Green Pepper
例如:我必须检查B中的每个水果是否是香蕉,然后A中的颜色是黄色。
香蕉还可以有另一种颜色/或第二种颜色(例如紫色),但紫色不能用于B中的其他水果。 (这些颜色只是示例,它与水果和颜色无关。)
TLDR:一旦A的颜色/值与B中的某个水果/值相关联,A中的颜色/值就不能再用于其他水果/值。
在我目前的VBA中,有没有一种简单的方法可以在VBA中执行此操作?如果'声明?
答案 0 :(得分:1)
您是否尝试在VBA中使用收藏夹?
如果不使用两个集合,一个包含'values',另一个包含'fruits'。
选项1)从“fruits”和“values”集合中的所有唯一值开始,然后从中删除。
以下是一些我未测试过的伪代码示例。
Dim vals As Collection
Dim fruits As Collection
Private Sub Init_Collections()
Set vals = New Collection
Set fruits = New Collection
'Add all unique values
vals.Add("Purple")
vals.Add("Red")
'Add all unique fruits
fruits.Add("Orange")
fruits.Add("Apple")
End Sub
Private Sub CheckPairs()
Dim Sheet As Worksheet
Dim row As Integer
Set Sheet = ThisWorkbook.Worksheets("Sheet1")
Call Init_Collections()
row = 1
while(Sheet.Cells(row,1).Value <> "")
if(fruits.Contains(Sheet.Cells(row,2).Value) AND _
vals.Contains(Sheet.Cells(row,1).Value)) Then
'Success - Pair doesn't exist
fruits.Remove(Sheet.Cells(row,2).Value)
vals.Remove(Sheet.Cells(row,1).Value)
else
'Fail - Pair one of the values or fruits is already being used
end if
row = row + 1
wend
End Sub
选项2)您从两个空集合开始,并继续添加它们。
以下是一些我未测试过的伪代码示例。
Private Sub CheckPairs()
Dim vals As New Collection
Dim fruits As New Collection
Dim Sheet As Worksheet
Dim row As Integer
Set Sheet = ThisWorkbook.Worksheets("Sheet1")
row = 1
while(Sheet.Cells(row,1).Value <> "")
if(fruits.Contains(Sheet.Cells(row,2).Value) OR _
vals.Contains(Sheet.Cells(row,1).Value)) Then
'Fail - Pair one of the values or fruits is already being used
else
'Success - These items don't exist, add them to their collections
fruits.Add(Sheet.Cells(row,2).Value)
vals.Add(Sheet.Cells(row,1).Value)
end if
row = row + 1
wend
End Sub
答案 1 :(得分:0)
这是一个宏来检查您的值,并突出显示以前的内容之前的任何单元格。选择你的颜色和水果范围(两列),然后运行宏。
Sub TestFruitsAndColors()
Dim d As Object, cell As Range
Set d = CreateObject("Scripting.Dictionary")
For Each cell In Selection.Columns(1).Cells
If Not d.Exists(cell.Value) Then
d.Add cell.Value, cell.Offset(0, 1).Value
ElseIf d.Item(cell.Value) <> cell.Offset(0, 1).Value Then
cell.Interior.Color = vbYellow
End If
Next
End Sub
答案 2 :(得分:0)
如果你走下水果寻找以前与当前颜色匹配的水果与不匹配的水果,你可以很容易地识别后者的不匹配。
Sub dup_frute()
Dim frt As Range, bgnfrt As Range, endfrt As Range
With Worksheets("Sheet5")
Set bgnfrt = .Range("B1")
Set endfrt = .Cells(Rows.Count, bgnfrt.Column).End(xlUp)
For Each frt In .Range(bgnfrt, endfrt)
If CBool(Application.CountIfs( _
.Range(bgnfrt, frt), "<>" & frt.Value2, _
.Range(bgnfrt, frt).Offset(0, -1), frt.Offset(0, -1).Value2)) Then
Debug.Print frt.Value2 'Pepper
Debug.Print frt.Offset(0, -1).Value2 'Green
End If
Next frt
End With
End Sub
答案 3 :(得分:0)
这是您可以从VBA调用以测试范围的功能:
Function Check(a As Range, b As Range) As String
Dim i&, k&, s$, L$, R$, p, v
v = Evaluate(a.Address & "&""|""&" & b.Address)
s = Join(Application.Transpose(v), "|") & "|"
For i = 1 To UBound(v)
k = 2: p = Split(v(i, 1), "|"): L = p(0): R = p(1)
Do: k = InStr(k, s, L & "|")
If k Then
If Mid$(s, k + 1 + Len(L), Len(R) + 1) <> R & "|" Then: Check = L: Exit For
k = k + 1
End If
Loop While k
Next
End Function
那就是它。
以下是如何调用它:
Sub TestIt()
s = Check([a1:a7], [b1:b7])
If Len(s) Then
s = s & " is matched with more than one value."
Else
s = "It's good."
End If
MsgBox s
End Sub
答案 4 :(得分:0)
你可以用一个简单的公式来做到这一点。
如图所示输入C2并填写。
import CoreMotion
class GameScene: SKScene, SKPhysicsContactDelegate {
var motionManager = CMMotionManager()
var destX:CGFloat = 0.0
override func didMoveToView(view: SKView) {
if motionManager.accelerometerAvailable {
motionManager.startAccelerometerUpdatesToQueue(NSOperationQueue.currentQueue()!, withHandler:{
data, error in
let currentX = self.steve.position.x
if data!.acceleration.x < 0 {
self.destX = currentX + CGFloat(data!.acceleration.x * 500)
//self.rolling = true
//self.steve.removeAllActions()
self.steve.runAction(rotate)
}
else if data!.acceleration.x > 0 {
self.destX = currentX + CGFloat(data!.acceleration.x * 500)
//self.rolling = true
// self.steve.removeAllActions()
self.steve.runAction(rerotate())
}
})
}
override func update(currentTime: CFTimeInterval) {
let action = SKAction.moveToX(destX, duration: 0.9)
steve.runAction(action)
}