我的目标是在状态工作表上列出来自不同工作表的用户请求。 (并非所有工作表都需要用户输入。)
示例工作表:创建、更新、扩展、删除。
我想出的程序从 Create 获取值,但是一旦更新值被填充,它就会覆盖 Create 中的值。
Sub commit()
Dim a As Integer
Dim b As Integer
Dim c As Integer
Dim d As Integer
Dim count As Integer
Sheet1.Range("M1").Value = "=counta(F5:F8)+3"
Sheet4.Range("A1").Value = "=counta(E3:E100)+2"
Sheet13.Range("A1").Value = "=counta(E3:E100)+2"
count = WorksheetFunction.CountA(Sheet24.Range("A:A"))
For b = 3 To Sheet4.Range("A1").Value
Sheet24.Range("XFD1").Value = "=counta(a2:a100)+2"
a = Sheet24.Range("XFD1").Value
c = Sheet1.Range("M1").Value
d = Sheet13.Range("A1").Value
'create
Sheet24.Range("A" & count).Value = Sheet4.Range("B" & b).Value
Sheet24.Range("C" & count).Value = Sheet4.Range("D" & b).Value
Sheet24.Range("D" & count).Value = Sheet4.Range("F" & b).Value
Sheet24.Range("E" & count).Value = Sheet4.Range("J" & b).Value
Sheet24.Range("G" & count).Value = Sheet1.Range("F" & c).Value
'update-description
Sheet24.Range("A" & count + 1).Value = Sheet13.Range("B" & d).Value 'maintenance request code
Sheet24.Range("C" & count + 1).Value = Sheet13.Range("D" & d).Value 'line number
Sheet24.Range("D" & count + 1).Value = Sheet4.Range("F" & b).Value 'mattype code
Sheet24.Range("E" & count + 1).Value = Sheet4.Range("G" & b).Value
因此,有几种方法可以简化内容,我可以提供一个框架:
Dim ws as Worksheet: For Each ws in Worksheets
Select Case sheets(ws.name)
Case "Create", "Update", "Extend", "Delete"
With Sheets(ws.Name)
'May want to put a loop for the last row on the ws
Dim StatusLastRow as Long: StatusLastRow = Sheets("Status").Cells(Sheets("Status").Rows.Count,1).End(XLUp).Row
Sheets("Status").Cells(StatusLastRow+1,1).Value = .Cells(.Cells(.Rows.Count,1),1).Value
End With
End select
Next ws
我将其设置为仅从每张纸中取出列(1)中的最后一行并附加到状态...您可以选择有一个循环,或者可能只是行(x).value = rows(y).value如果所有工作表上的格式都相同,则为每个。 这会产生影响,并可能使您免于引用单个单元格。