Smallish VBA Macro运行速度很慢

时间:2017-03-08 10:53:43

标签: excel vba excel-vba

我写的My Macro运行速度很慢,我不知道为什么。宏删除区域,然后将一列位置作为一行再现,删除任何重复项,然后在一列日期中复制,然后将每个日期的相应平均值放在正确的功能位置。

以下是代码:

Sub Macro1()
'
' Macro1 Macro


' Delete Previous Data
Range("J1:AQ1000").Clear

' Functional Location

Dim toAdd As Boolean, uniqueTag As Integer, i As Integer, j As Integer
Cells(1, 11).Value = Cells(2, 2).Value
uniqueTag = 11
toAdd = True
For i = 3 To 1000
For j = 11 To uniqueTag
If Cells(i, 2).Value = Cells(1, j).Value Then
toAdd = False
End If
Next j
If toAdd = True Then
Cells(1, uniqueTag + 1).Value = Cells(i, 2).Value
uniqueTag = uniqueTag + 1
End If
toAdd = True
Next i

' Date

Dim k As Integer
For k = 2 To 1000
Cells(k, 10).Value = Cells(k, 6).Value
Next k

' Mean Value
Dim l As Integer, m As Integer
For l = 2 To 1000
For m = 11 To 100
If Cells(l, 2).Value = Cells(1, m).Value Then
Cells(l, m).Value = Cells(l, 5).Value
End If
Next m
Next l



End Sub

是否有任何可以让它运行得更快的优化,或者我还没有包含的内容? 第一部分都非常快速地工作,它只是复制平均值,这似乎需要时间。

1 个答案:

答案 0 :(得分:1)

将您的代码插入我评论的位置:

def student_validate(row, this_tenant, counter, batch):
    data="st"
    today=dt.date.today()
    today_string=today.strftime('%y%m%d')
    next_student_number='{0:03d}'.format(counter)
    last_student=Student.objects.filter(tenant=this_tenant).\
            filter(key__contains=today_string).order_by('key').last()
    if last_student:
        last_student_number=int(last_student.key[8:])
        next_student_number='{0:03d}'.format(last_student_number +     counter)
    key=data+str(today_string)+str(next_student_number)
    toslug=str(this_tenant)+" " +str(key)
    slug=slugify(toslug)
    item=None
    row.append(key)
    row.append(slug)
    row.append(this_tenant)
    row[12]=batch
    if (row[0] == None or row[0] == "" or row[1] == None or row[1] == "") :
        transaction.rollback()
        return HttpResponse("There is error in uploaded excel")
    if (row[3] != "M" and row[3] != "F" and row[3] != "O"):
        transaction.rollback()
        return HttpResponse("There is error in uploaded excel")
    if (row [2] != None and row [2] != ""):
        if (type(row [2]) != dt.date):
            transaction.rollback()
            return HttpResponse("There is error in uploaded excel")
    return row