我不是一个程序员,但我已经将一个大致完成我需要的宏一起攻击了。我有两个小问题,为了改善,我希望得到你的帮助。
总之,我有预约,我需要安排。我有大约120天的时间,我每天都在循环。电子表格公式负责每天的约会分配,然后我将结果剪切并粘贴到摘要页面。
宏需要很长时间才能运行,我相信因为我有很多复制/粘贴。你会如何更改代码以加快速度?代码中的其他新手错误是什么?
另外,我发现当我从“Daytoday”以外的任何工作表运行宏时,宏在Worksheets("DaybyDay").Range("A2", Columns("A").End(xlDown)).ClearContents
行失败。是什么原因造成的?
无论如何这里是代码:
Sub Datadump()
Dim rng As Range, cell As Range
Dim dest As Range, j As Integer, k As Integer
Dim vSlotsforday As Integer, vCapacityforday As Integer
Dim sht As Worksheet
Dim MyArray(150) As Integer
Application.ScreenUpdating = False
Set sht = ThisWorkbook.Sheets("Day analyser")
Set rng = Worksheets("Slot distribution").Range("$B$12:$B$131")
If MsgBox("Do you want to delete existing data on DaytoDay tab?", vbYesNo) = vbYes Then
Worksheets("DaybyDay").Range("A2", Columns("A").End(xlDown)).ClearContents
Else
End If
For Each cell In rng
Worksheets("Day analyser").Range("C2").Value = cell
vSlotsforday = Worksheets("Day analyser").Range("$E$17").Value
'vCapacityforday = Worksheets("Day analyser").Range("$D$78").Value
If vSlotsforday <> 0 Then
Worksheets("Day analyser").Range(sht.Cells(80, 5), sht.Cells(79 + vSlotsforday, 9)).Copy
Sheets("DaybyDay").Range("A" & Rows.Count).End(xlUp).Offset(1, 0).PasteSpecial xlPasteValues
Else
End If
' If vCapacityforday < vSlotsforday Then
' MsgBox "There are more slots than capacity on " & cell
' Else
' End If
Next cell
Application.ScreenUpdating = True
End Sub