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 (将#修改为@)