我有两个表,一个表包含项目的所有信息,另一个表显示主表中过期或几乎过期的行。
我想做的是每次打开工作表时,使用主表中的所有过期变量更新过期表。
我面临的问题是,在使用For循环时,它会在将下一个值添加到Expired表之前添加许多空单元格。
过期表代码:
Private Sub warning_list()
Dim wsExp As Worksheet: Set wsExp = Worksheets("EXPIRED_LIST")
Dim wsList As Worksheet: Set wsList = Worksheets("LIST")
Dim digsafe As String
Dim exp_date As Date
Dim time_left As Long
Dim i As Integer
Dim warning_val As String: warning_val = "OK"
Dim warning_tbl As ListObject: Set warning_tbl = wsExp.ListObjects("Table3")
Dim warning_or As ListRow
Dim list_tbl As ListObject: Set list_tbl = wsList.ListObjects("Table1")
Dim list_or As ListRow
Set warning_or = warning_tbl.ListRows.Add
For i = 1 To wsList.ListObjects("Table1").DataBodyRange.Rows.Count
If wsList.ListObjects("Table1").DataBodyRange.Cells(i, 24).Value = warning_val Then
digsafe = list_tbl.DataBodyRange.Cells(i, 1).Value
exp_date = list_tbl.DataBodyRange.Cells(i, 2).Value
warning_or.Range(1, 1).Value = digsafe
warning_or.Range(1, 2).Value = exp_date
End If
Next i
End Sub
编辑: 我还具有以下代码来自动运行宏。
'Action whenever sheet is opened
Private Sub Worksheet_Activate()
Call warning_list
End Sub