Ala*_*ain 3 excel optimization vba dictionary excel-vba
清空Scripting.DictionaryExcel表格的最快方法是什么?这就是我现在正在做的事情,但是对于一个包含大约3000个元素的字典,它显然很慢.我做了我能想到的每一个优化.
这是我所拥有的一个简单的版本:
'wordCount and emailCount are late bound "Scripting.Dictionary" objects
Private Sub DictionaryToExcel(ByRef wordCount As Object, emailCount As Object)
oExcel.EnableEvents = False
oExcel.ScreenUpdating = False
Set oWorkbook = oExcel.Workbooks.Add
oExcel.Calculation = -4135
With oWorkbook.Sheets(1)
iRow = 1
For Each strKey In wordCount.Keys()
iWordCount = wordCount.Item(strKey)
iEmailCount = emailCount.Item(strKey)
If iWordCount > 2 And iEmailCount > 1 Then
.Cells(iRow, 1) = strKey
.Cells(iRow, 2) = iEmailCount
.Cells(iRow, 3) = iWordCount
iRow = iRow + 1
End If
Next strKey
End With
oExcel.ScreenUpdating = True
End Sub
Run Code Online (Sandbox Code Playgroud)
这是完整版本,包括我正在采取的每一个动作(主要是格式化,但有一个相对昂贵的动作,strKey对它进行拼写检查(虽然我认为这已经被优化了尽可能多:
'wordCount and emailCount are late bound "Scripting.Dictionary" objects
Private Sub DictionaryToExcel(ByRef wordCount As Object, emailCount As Object)
Dim oExcel As Object, oWorkbook As Object
Dim strKey As Variant, iRow As Long
Dim iWordCount As Long, iEmailCount As Long, spellCheck As Boolean
Set oExcel = CreateObject("Excel.Application")
oExcel.EnableEvents = False
oExcel.ScreenUpdating = False
Set oWorkbook = oExcel.Workbooks.Add
oExcel.Calculation = -4135
With oWorkbook.Sheets(1)
iRow = 1
.Columns(1).NumberFormat = "@"
For Each strKey In wordCount.Keys()
iWordCount = wordCount.Item(strKey)
iEmailCount = emailCount.Item(strKey)
spellCheck = False
If iWordCount > 2 And iEmailCount > 1 Then
.Cells(iRow, 1) = strKey
.Cells(iRow, 2) = iEmailCount
.Cells(iRow, 3) = iWordCount
spellCheck = oExcel.CheckSpelling(strKey)
If Not spellCheck Then spellCheck = oExcel.CheckSpelling(StrConv(strKey, vbProperCase))
.Cells(iRow, 4) = IIf(spellCheck, "Yes", "No")
iRow = iRow + 1
End If
Next strKey
.Sort.SortFields.Clear
.Sort.SortFields.Add Key:=.Columns(4), Order:=1
.Sort.SortFields.Add Key:=.Columns(2), Order:=2
.Sort.SortFields.Add Key:=.Columns(3), Order:=2
.Sort.SetRange .Range(.Columns(1), .Columns(4))
.Sort.Apply
.Rows(1).Insert
.Rows(1).Font.Bold = True
.Cells(1, 1) = "Word"
.Cells(1, 2) = "Emails Containing"
.Cells(1, 3) = "Total Occurrences"
.Cells(1, 4) = "Is a common word?"
.Range(.Columns(1), .Columns(4)).AutoFit
If .Columns(1).ColumnWidth > 20 Then .Columns(1).ColumnWidth = 20
.Range(.Columns(2), .Columns(4)).HorizontalAlignment = -4152
End With
oExcel.Visible = True
oExcel.ScreenUpdating = True
End Sub
Run Code Online (Sandbox Code Playgroud)
我知道有一种非常快速的方法可以将2D数组激发到一系列单元格中,但是我不确定是否有类似于Dictionaries的东西.
*编辑*
到目前为止,我已经通过将值添加到数组而不是直接添加到excel单元格,然后将数组激发到excel来进行改进:
Private Sub DictionaryToExcel(ByRef wordCount As Object, emailCount As Object)
Dim arrPaste() As Variant
Set oWorkbook = oExcel.Workbooks.Add
iRow = 1: total = wordCount.count
ReDim arrPaste(1 To total, 1 To 4)
For Each strKey In wordCount.Keys()
iWordCount = wordCount.Item(strKey)
iEmailCount = emailCount.Item(strKey)
spellCheck = False
If iWordCount > 2 And iEmailCount > 1 Then
arrPaste(iRow, 1) = strKey
arrPaste(iRow, 2) = iEmailCount
arrPaste(iRow, 3) = iWordCount
iRow = iRow + 1
End If
count = count + 1
Next strKey
With oWorkbook.Sheets(1)
.Range(.Cells(1, 1), .Cells(total, 4)) = arrPaste
Run Code Online (Sandbox Code Playgroud)