目标是我有第二个工作表(CalcValues),基本上是查找值(如果fieldA = x,fieldB = y,fieldC = z,则value = a)。
我拥有的骨架(我不在VBA工作,所以这可能是错的):
Dim calcArray
calcArray = Array(FIELDA, FIELDB, FIELDC, FIELDD, FIELDE)
If calcArray.Contains(Target.Column) Then
Dim fieldAVal, fieldBVal, fieldCVal, fieldDVal, fieldEVal
fieldAVal = Target.Worksheet.Cells(Target.Row, FIELDA)
...
fieldEVal = Target.Worksheet.Cells(Target.Row, FIELDE)
End If
使用FIELDA-E作为查找字段列索引。
不起作用,但目标是如果我设置其中一个字段进行检查,在伪C#实体框架中混入我从Excel的VBA中得知的东西,如:
var data = Worksheets("CalcValues").Where(c => c.ColumnA == fieldAVal);
data = data.Where(c => c.ColumnB == fieldBVal);
...
data = data.Where(c => c.ColumnE == fieldEVal);
' Ideally, grabbing a row number from data. Or row itself, either or.
' with GRABBED_ROW = the last remaining 'data' object's row number, provided one exists.
Target.Worksheet.Cells(Target.Row, UPDATEDFIELDA) = Worksheet("CalcValues").Cells(GRABBED_ROW, UPDATEDFIELDAINDEX);
Target.Worksheet.Cells(Target.Row, UPDATEDFIELDB) = Worksheet("CalcValues").Cells(GRABBED_ROW, UPDATEDFIELDBINDEX);
...
问题是我如何在var数据中实现过滤... data = data.Where(c => c.ColumnE == fieldEVal);第一节伪编码?
编辑:我正在做一些评论,但看起来我最好的选择是以某种方式拥有类似于C#中的List的动态数组,并使用Application.WorkSheet.Match来实现,这是正确的吗?edit2:好的,我更新了它(所以很可能忽略了看到意图之外的第二部分),但我不确定代码是否正确(没有触发):
For i = 1 To Worksheet("CalcValues").ListRows.Count
If (Worksheet("CalcValues").Cells(i, 1).Value = fieldAVal And Worksheet("CalcValues").Cells(i, 2).Value = fieldBVal And Worksheet("CalcValues").Cells(i, 3).Value = fieldCVal And Worksheet("CalcValues").Cells(i, 4).Value = fieldDVal And Worksheet("CalcValues").Cells(i, 5).Value = fieldEVal) Then
If (Worksheet("CalcValues").Cells(i, 6).Value = "Column A") Then
Target.Worksheet.Cells(Target.Row, COLUMNA).Value = Worksheet("CalcValues").Cells(i, 7).Value
ElseIf (Worksheet("CalcValues").Cells(i, 6).Value = "Column B") Then
Target.Worksheet.Cells(Target.Row, COLUMNB).Value = Worksheet("CalcValues").Cells(i, 7).Value
End If
End If
Next
答案 0 :(得分:0)
好的,我终于明白了。我最初做了很多假设,并发现它在功能上有点不同(也许有更好的方法,但这似乎有效):
Dim calcArray(1 To 5) As Integer
calcArray(1) = FIELDA
calcArray(2) = FIELDB
calcArray(3) = FIELDC
calcArray(4) = FIELDD
calcArray(5) = FIELDE
Dim it As Integer, found As Boolean
it = 1
Do While it <= UBound(calcArray) And Not found
If (calcArray(it) = Target.Column) Then
found = True
Else
it = it + 1
End If
Loop
If (found) Then
Dim fieldAVal, fieldBVal, fieldCVal, fieldDVal, fieldEVal
fieldAVal = Target.Worksheet.Cells(Target.Row, FIELDA)
fieldBVal = Target.Worksheet.Cells(Target.Row, FIELDB)
fieldCVal = Target.Worksheet.Cells(Target.Row, FIELDC)
fieldDVal = Target.Worksheet.Cells(Target.Row, FIELDD)
fieldEVal = Target.Worksheet.Cells(Target.Row, FIELDE)
For i = 1 To Worksheets("CalcValues").UsedRange.Rows.Count
If (Worksheets("CalcValues").Cells(i, 1).Value = fieldAVal And Worksheets("CalcValues").Cells(i, 2).Value = fieldBVal And Worksheets("CalcValues").Cells(i, 3).Value = fieldCVal And Worksheets("CalcValues").Cells(i, 4).Value = fieldDVal And Worksheets("CalcValues").Cells(i, 5).Value = fieldEVal) Then
If (Worksheets("CalcValues").Cells(i, 6).Value = "Column A") Then
Target.Worksheet.Cells(Target.Row, COLUMNA).Value = Worksheets("CalcValues").Cells(i, 7).Value
ElseIf (Worksheets("CalcValues").Cells(i, 6).Value = "Column B") Then
Target.Worksheet.Cells(Target.Row, COLUMNB).Value = Worksheets("CalcValues").Cells(i, 7).Value
End If
End If
Next
End If