简体   繁体   中英

Grab data from another sheet based on values matching in Excel with VBA

The goal is that I have a second worksheet (CalcValues) with essentially lookup values (if fieldA = x, fieldB = y, fieldC = z, then value = a).

The skeleton of what I have (I don't work in VBA, so this may be wrong):

 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

With FIELDA-E being the lookup fields column indexes.

Not functional, but the goal is that if I set one of those fields a check is done, in pseudo-C# entity framework mixed in with what little I know from Excel's VBA something like:

 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);
 ...

With the question being how do I achieve the filtering in the var data... data = data.Where(c => c.ColumnE == fieldEVal); section I pseudo coded?

edit: I'm doing some reviewing, but it looks like my best bet is to somehow have a dynamic array akin to a List in C#, and use Application.WorkSheet.Match to get there, would that be a correct?

edit2: Okay, I updated it (so can most likely ignore the 2nd section outside of seeing intent), but I'm not certain the code is correct (isn't firing):

 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

Okay, I finally figured it out. A lot of assumptions were made on my part initially and come to find out it was quite a bit different functionally (perhaps there's a better way, but this seems to work):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM