比较数组和赋值 Excel VBA
Compare arrays and assigning values Excel VBA
我有两个日历,一个用于 10 辆车,第二个用于 15 个司机。我想为汽车日历的每一天分配司机。
Here is image of cars calendar
And here is imade of drivers calendar
这是我的代码,它解释了我的目标,但当然行不通,因为我不熟悉 vba 我可以弄清楚如何按列比较两个数组的值,但是可以'将驾驶员姓名列中的值分配给汽车日历
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
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
我有两个日历,一个用于 10 辆车,第二个用于 15 个司机。我想为汽车日历的每一天分配司机。
Here is image of cars calendar
And here is imade of drivers calendar
这是我的代码,它解释了我的目标,但当然行不通,因为我不熟悉 vba 我可以弄清楚如何按列比较两个数组的值,但是可以'将驾驶员姓名列中的值分配给汽车日历
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
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