【发布时间】:2017-03-31 21:01:01
【问题描述】:
我整天都在处理这段代码,终于让一切都完美地工作了。唯一的问题是代码运行速度很慢。考虑到它将用于具有数千行的工作簿,我想更改它。我对 vba 非常陌生,所以这里可能有些东西是错误的,或者看起来像是一个糟糕的捷径。我想我添加了几种可以加快速度的方法,但我不知道是否可以做任何其他事情。
Sub Degree_Workboook_Names_major1()
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.EnableEvents = False
'Inserts a new column after column H named department names
range("I1").EntireColumn.Insert
range("I1").Value = "DeptName"
Dim abbrRange As range 'range to hold the columns with the department names
Set abbrRange = range("H:H")
'Writes the department name in the the department name column next to the associated abbr
For Each cell In abbrRange
If cell.Value = "ACC" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Accounting"
End If
If cell.Value = "ACS" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Adolescent, Career and Special Education"
End If
If cell.Value = "AES" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Animal and Equine Science"
End If
If cell.Value = "AGR" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Agricultural Science"
End If
If cell.Value = "AHS" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Applied Health Sciences"
End If
If cell.Value = "AHT" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Veterinary Technology and Pre-Veterinary Medicine"
End If
If cell.Value = "Art" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Art and Design"
End If
If cell.Value = "BIO" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Biology"
End If
If cell.Value = "BPA" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Management, Marketing and Business Administration"
End If
If cell.Value = "CCD" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Center for Communication Disorders"
End If
If cell.Value = "CEAO" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Bachelor of Integrated Studies Program"
End If
If cell.Value = "CHE" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Chemistry"
End If
If cell.Value = "CLH" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Community Leadership and Human Services"
End If
If cell.Value = "COM" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Organizational Communication"
End If
If cell.Value = "CSC" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Computer Science and Information Systems"
End If
If cell.Value = "ECO" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Economics and Finance"
End If
If cell.Value = "ELE" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Early Childhood and Elementary Education"
End If
If cell.Value = "ENPH" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of English and Philosophy"
End If
If cell.Value = "ELSC" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Educational Studies, Leadership and Counseling"
End If
If cell.Value = "GSC" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Geosciences"
End If
If cell.Value = "HFA" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Liberal Arts"
End If
If cell.Value = "HIS" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of History"
End If
If cell.Value = "INDC" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Institute of Engineering"
End If
If cell.Value = "IOE" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Institute of Engineering"
End If
If cell.Value = "JMC" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Journalism and Mass Communications"
End If
If cell.Value = "MAT" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Mathematics and Statistics"
End If
If cell.Value = "MLA" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Modern Languages"
End If
If cell.Value = "MMB" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Management, Marketing and Business Administration"
End If
If cell.Value = "MSP" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Military Science Program"
End If
If cell.Value = "MUS" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Music"
End If
If cell.Value = "NUR" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Nursing"
End If
If cell.Value = "OSH" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Occupational Safety and Health"
End If
If cell.Value = "POL" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Political Science and Sociology"
End If
If cell.Value = "PSY" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Psychology"
End If
If cell.Value = "THR" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Theatre"
End If
Next
'Inserts a new column after column H named department names
range("M1").EntireColumn.Insert
range("M1").Value = "DeptName"
'Dim abbrRange As range 'range to hold the columns with the dpeartment names
Set abbrRange = range("L:L")
'Writes the department name in the the department name column next to the associated abbr
For Each cell In abbrRange
If cell.Value = "ACC" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Accounting"
End If
If cell.Value = "ACS" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Adolescent, Career and Special Education"
End If
If cell.Value = "AES" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Animal and Equine Science"
End If
If cell.Value = "AGR" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Agricultural Science"
End If
If cell.Value = "AHS" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Applied Health Sciences"
End If
If cell.Value = "AHT" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Veterinary Technology and Pre-Veterinary Medicine"
End If
If cell.Value = "Art" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Art and Design"
End If
If cell.Value = "BIO" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Biology"
End If
If cell.Value = "BPA" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Management, Marketing and Business Administration"
End If
If cell.Value = "CCD" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Center for Communication Disorders"
End If
If cell.Value = "CEAO" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Bachelor of Integrated Studies Program"
End If
If cell.Value = "CHE" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Chemistry"
End If
If cell.Value = "CLH" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Community Leadership and Human Services"
End If
If cell.Value = "COM" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Organizational Communication"
End If
If cell.Value = "CSC" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Computer Science and Information Systems"
End If
If cell.Value = "ECO" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Economics and Finance"
End If
If cell.Value = "ELE" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Early Childhood and Elementary Education"
End If
If cell.Value = "ENPH" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of English and Philosophy"
End If
If cell.Value = "ELSC" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Educational Studies, Leadership and Counseling"
End If
If cell.Value = "GSC" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Geosciences"
End If
If cell.Value = "HFA" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Liberal Arts"
End If
If cell.Value = "HIS" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of History"
End If
If cell.Value = "INDC" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Institute of Engineering"
End If
If cell.Value = "IOE" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Institute of Engineering"
End If
If cell.Value = "JMC" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Journalism and Mass Communications"
End If
If cell.Value = "MAT" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Mathematics and Statistics"
End If
If cell.Value = "MLA" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Modern Languages"
End If
If cell.Value = "MMB" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Management, Marketing and Business Administration"
End If
If cell.Value = "MSP" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Military Science Program"
End If
If cell.Value = "MUS" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Music"
End If
If cell.Value = "NUR" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Nursing"
End If
If cell.Value = "OSH" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Occupational Safety and Health"
End If
If cell.Value = "POL" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Political Science and Sociology"
End If
If cell.Value = "PSY" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Psychology"
End If
If cell.Value = "THR" Then
cell.Activate
ActiveCell.Offset(0, 1).Activate
ActiveCell.Value = "Department of Theatre"
End If
Next
range("I:I").HorizontalAlignment = xlLeft
range("M:M").HorizontalAlignment = xlLeft
Application.ScreenUpdating = True
Application.DisplayStatusBar = True
Application.EnableEvents = True
End Sub
【问题讨论】:
-
您的代码运行缓慢的原因是因为您不断地直接激活工作表。这是你应该不惜一切代价避免做的事情。相反,您可以尝试 thiscell.value = this。直接分配一个值是 INF 更有效。记住 - 尝试尽可能少地直接与 excel 对象交互
-
这里有 3 个主要问题:循环遍历整个列(1000000 个单元格)- 使用 .End(xlUp) 定位使用的范围,遍历单元格 - 使用 Variant Array 代替,使用 Select /Activate - 改为使用变量
-
cell.Offset(0,1).Value = "" 是您将值直接应用于单元格而不是激活它的方式