简体   繁体   中英

Compare arrays and assigning values Excel VBA

I have two calendars, one for 10 cars, second for 15 drivers. I want to assign drivers to each day of cars calendar.

Here is image of cars calendar

And here is imade of drivers calendar

Here is my code, which explane my goal, but of course doesn't work because I'm not familiar with vba I can figure out how to comparce values of both arrays by column, but can't assign value from drivers names column to cars calendar

Private Sub CommandButton1_Click()

Dim cars() As Variant
Dim drivers() As Variant
cars = Range("A1:F10")
drivers = Range("M1:R15")

For Each carDay In cars
   For Eeach driverDay In drivers
      Dim driver As Long
      Set driver = driverDay(1)
         If carDay(2) = driverDay(2) Then
            carDay.Value = driver
            driverDay.Value = "used"
         End If
   Next driverDay
Next carDay


End Sub

See Collection Object

Option Explicit

Sub CommandButton1_Click()

    Dim coA As New Collection, coI As New Collection, coY As New Collection
    Dim n As Integer, d As Integer, sDriver As String, sCar As String
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("Sheet1")

    For d = 1 To 5
        For n = 1 To 15
            sDriver = ws.Cells(n, "M")
            Select Case ws.Cells(n, "M").Offset(0, d)
                Case "A": coA.Add sDriver
                Case "I": coI.Add sDriver
                Case "Y": coY.Add sDriver
            End Select
        Next

        For n = 1 To 10
            sCar = ws.Cells(n, "A")
            Select Case ws.Cells(n, "A").Offset(0, d)
                Case "A": sDriver = coA(1): coA.Remove 1
                Case "I": sDriver = coI(1): coI.Remove 1
                Case "Y": sDriver = coY(1): coY.Remove 1
            End Select
            ws.Cells(20 + n, "A").Offset(0, d) = sCar & ":" & sDriver
        Next

        Set coA = Nothing
        Set coI = Nothing
        Set coY = Nothing

    Next

End Sub

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