如何解决VBA-根据其他列的值设置一列的值
早上好!我陷入了代码,我真的不知道如何解决。我想要的是列Area,Data_Input,Window和Lote。我需要根据通过Area和Window插入表格数据来填写Lot列。例如:在我的表中,从08/23的21:00开始的前10个案例将为1,其他的21个案例从21:03开始。将会是2。但是,如果所有这些都来自同一个窗口(在这种情况下为8),那么所有这些都将是另外一个窗口号,那么它将连续返回1,依此类推
我尽可能构建的代码是:
Sub Count()
Dim CatchTable As DAO.Recordset
Dim Lote As Integer
Dim QuantityValues As Integer
Dim DataInput() As Variant
Dim Area() As Variant
Dim Window() As Variant
Set CatchTable = CurrentDb.OpenRecordset("SELECT * FROM Table1")
CatchTable.MoveLast
QuantityValues = CInt(CatchTable.RecordCount)
CatchTable.MoveFirst
Lote = 1
ReDim DataInput(QuantityValues)
ReDim Area(QuantityValues)
ReDim Window(QuantityValues)
For i = 1 To QuantityValues
DataInput(i) = CatchTable.Fields("Data_Input").Value
Area(i) = CatchTable.Fields("Area").Value
Window(i) = CatchTable.Fields("Window").Value
CatchTable.MoveNext
Next
CatchTable.MoveFirst
For i = 1 To QuantityValues
If i < QuantityValues Then
If Area(i) = Area(i + 1) Then
If DateValue(DataInput(i)) = DateValue(DataInput(i + 1)) And Window(i) = Window(i + 1) Then
If TimeValue(DataInput(i)) <> TimeValue(DataInput(i + 1)) Then
Lote = Lote + 1
End If
End If
Else
Lote = 1
End If
CurrentDb.Execute "UPDATE Table1 SET Lote = " & Lote & " WHERE Area = '" & Area(i + 1) & "' AND Data_Input = #" & Format(DataInput(i + 1),"yyyy-mm-dd hh:nn:ss") & "#"
CatchTable.MoveNext
End If
Next
Set CatchTable = Nothing
End Sub
赞!
Data_Input Area Window Lote
08/23/2020 21:00:00 Logistics 8 1
08/23/2020 21:00:00 Logistics 8 1
08/23/2020 21:00:00 Logistics 8 1
08/23/2020 21:00:00 Logistics 8 1
08/23/2020 21:00:00 Logistics 8 1
08/23/2020 21:00:00 Logistics 8 1
08/23/2020 21:00:00 Logistics 8 1
08/23/2020 21:00:00 Logistics 8 1
08/23/2020 21:00:00 Logistics 8 1
08/23/2020 21:03:00 Logistics 8 2
08/23/2020 21:03:00 Logistics 8 2
08/23/2020 21:03:00 Logistics 8 2
08/23/2020 21:03:00 Logistics 9 1
08/23/2020 21:03:00 Logistics 8 2
08/23/2020 21:05:00 Accounting 8 1
08/23/2020 21:05:00 Accounting 8 1
08/24/2020 21:00:00 Logistics 8 3
08/24/2020 21:00:00 Logistics 8 3
08/24/2020 21:00:00 Logistics 8 3
08/24/2020 21:00:00 Logistics 8 3
08/24/2020 21:00:00 Logistics 8 3
08/24/2020 21:00:00 Logistics 8 3
08/24/2020 21:00:00 Logistics 8 3
08/24/2020 21:00:00 Logistics 8 3
08/24/2020 21:00:00 Logistics 8 3
08/24/2020 21:03:00 Logistics 8 3
08/24/2020 21:03:00 Logistics 8 4
08/24/2020 21:03:00 Logistics 8 4
08/24/2020 21:03:00 Logistics 8 4
08/24/2020 21:03:00 Logistics 8 4
08/24/2020 21:05:00 Accounting 8 2
08/24/2020 21:05:00 Accounting 8 2
09/24/2020 21:05:00 Accounting 9 1
检查是否我把这一行:08/23/2020 21:03:00 Logistica 9 1
与其他行不同,只是为了根据Window的变化来区分它,并返回到1
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。