excel - 这个使 excelsheet 如此缓慢的 vba 代码出了什么问题?
问题描述
通过使用该站点上其他人提出的许多先前问题,我创建了一些对我来说非常有用的代码,只是我必须在其中出现问题,因为当我激活 vba 脚本时,excelsheet 尤其是脚本非常慢。
也许这是因为不同的子脚本?
Private Sub CommandButton1_Click()
Dim OutlookApp As Object
Dim Mess As Object, Recip, Datum, Aanhef, School, Leerlingen, Bezoekadres, Contact, Begintijd, Eindtijd
Recip = [k10].Value
n = SpinButton1.Value + 1
Datum = Format(Cells(n, 2), "dddd d mmmm yyyy")
Aanhef = Cells(n, 10)
School = Cells(n, 3)
Bezoekadres = Cells(n, 5)
Contact = Cells(n, 6)
Leerlingen = Cells(n, 12)
Begintijd = Format(Cells(n, 7), "hh:mm")
Eindtijd = Format(Cells(n, 8), "hh:mm")
Set OutlookApp = CreateObject("Outlook.Application")
Set Mess = OutlookApp.CreateItem(olMailItem)
With Mess
.Subject = "Afspraakherinnering op " & Datum & " op het " & School
.HTMLBody = " Beste " & Aanhef "
.To = Recip
.Display
'.send
End With
End Sub
Private Sub CommandButton3_Click()
Dim objWorksheet As Excel.Worksheet
Dim Mess As Object, Recip, Datum, Aanhef, School, Leerlingen, Bezoekadres, Contact, Begintijd, Eindtijd
Dim objOutlookApp As Outlook.Application
Dim objCalendar As Outlook.Folder
Dim objSchoolEvent As Outlook.AppointmentItem
Dim objRecurrencePattern As Outlook.RecurrencePattern
n = SpinButton1.Value + 1
Set objWorksheet = ThisWorkbook.Sheets(1)
nLastRow = objWorksheet.Range("A" & objWorksheet.Rows.Count).End(xlUp).Row
Set objOutlookApp = CreateObject("Outlook.Application")
Set objCalendar = objOutlookApp.Session.GetDefaultFolder(olFolderCalendar)
Set objSchoolEvent = objCalendar.Items.Add("IPM.Appointment")
With objSchoolEvent
.Subject = Cells(n, 3)
.AllDayEvent = False
.start = Cells(n, 2) + Cells(n, 7)
.End = Cells(n, 2) + Cells(n, 8)
.Location = Cells(n, 5)
.Body = Cells(n, 14)
.Save
End With
End Sub
Private Sub SpinButton1_Change()
n = SpinButton1.Value + 1
Range("C38").Value = Cells(n, 3)
Range("C39").Value = Format(Cells(n, 2), "dddd d mmmm yyyy")
Range("C40").Value = Format(Cells(n, 7), "hh:mm")
Range("C41").Value = Format(Cells(n, 8), "hh:mm")
End Sub
Private Sub CommandButton2_Click()
Dim OutlookApp As Object
Dim Mess As Object, Recip, Datum, Aanhef, School, Leerlingen, Bezoekadres, Contact, Begintijd, Eindtijd
Recip = [k10].Value
n = SpinButton1.Value + 1
Datum = Format(Cells(n, 2), "dddd d mmmm yyyy")
Aanhef = Cells(n, 10)
School = Cells(n, 3)
Bezoekadres = Cells(n, 5)
Contact = Cells(n, 6)
Leerlingen = Cells(n, 12)
Begintijd = Format(Cells(n, 7), "hh:mm")
Eindtijd = Format(Cells(n, 8), "hh:mm")
Set OutlookApp = CreateObject("Outlook.Application")
Set Mess = OutlookApp.CreateItem(olMailItem)
With Mess
.Subject = "Afspraakherinnering op " & Datum & " op het " & School
.HTMLBody = " Beste " & Aanhef & ",<br><br>"
.To = Recip
.Display
'.send
End With
End Sub
解决方案
您可以尝试调用这些潜艇。先调用“SpeedupProcessing”,然后在所有处理完成后调用“Back_to_Normal”。
Sub Speedup_Processing()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
End Sub
Sub Back_To_Normal()
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
End Sub
推荐阅读
- c++ - 如何创建自定义 INET 数据包?
- c# - 在monogame中添加背景不起作用
- android - 我的位置按钮未显示在导航抽屉中
- c - 如何使用 open() 安全地设置标志?
- ios - 使用 Swift 和 iOS 保存文件
- unity3d - Unity,我对 DontDestroyOnLoad 有问题,无法在不同的场景中保持跟踪
- spring-boot - Spring Cloud 网关在过滤器中发送响应
- vue.js - 如何在 Nuxt Project 文件夹中添加一个文件夹来运行 PHP 脚本?
- .htaccess - .htaccess 永久重定向 - 浏览器奇怪地将 GET 变量添加到 URL
- regex - 没有“解决”的 nginx 位置匹配扩展中的正则表达式