我在vb中创建了一个用于excel及其工作的宏,但问题是40000行的工作速度非常慢(在c2d e7500 @ 2,9 GHz上大约2分钟)。我认为它可以更快地完成但我不知道如何:)有人帮助我吗?
Dim bufor As String
Dim condition As Boolean
Cells.Find(What:="Month", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False).Activate
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.Select
Do While condition = False
bufor = Right(ActiveCell.Value, 1)
If bufor = "1" Then
ActiveCell.Value = "January"
ElseIf bufor = "2" Then
ActiveCell.Value = "February"
ElseIf bufor = "3" Then
ActiveCell.Value = "March"
ElseIf bufor = "4" Then
ActiveCell.Value = "April"
ElseIf bufor = "5" Then
ActiveCell.Value = "May"
ElseIf bufor = "6" Then
ActiveCell.Value = "June"
ElseIf bufor = "7" Then
ActiveCell.Value = "July"
ElseIf bufor = "8" Then
ActiveCell.Value = "August"
ElseIf bufor = "9" Then
ActiveCell.Value = "September"
ElseIf bufor = "10" Then
ActiveCell.Value = "October"
ElseIf bufor = "11" Then
ActiveCell.Value = "November"
ElseIf bufor = "12" Then
ActiveCell.Value = "December"
End If
ActiveCell.Offset(1, 0).Range("A1").Select
ActiveCell.Select
If ActiveCell.Value = "" Then
condition = True
End If
Loop
答案 0 :(得分:0)
您可以使用数组:
Dim bufor As Long
Dim rHeader As Range
Dim vData
Dim lColumn As Long
Dim startRow As Long
Dim endRow As Long
Dim n As Long
Set rHeader = Cells.Find(What:="Month", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False)
If Not rHeader Is Nothing Then
lColumn = rHeader.Column
startRow = rHeader.Row + 1
endRow = Cells(Rows.Count, lColumn).End(xlUp).Row
vData = Range(.Cells(startRow, lColumn, endRow, lColumn)).Value2
For n = LBound(vData, 1) To UBound(vData, 1)
bufor = CLng(Val(Right$(vData(n, 1), 1)))
Select Case bufor
Case 1 To 12
vData(n, 1) = MonthName(bufor)
End Select
Next n
Range(.Cells(startRow, lColumn, endRow, lColumn)).Value2 = vData
End If
答案 1 :(得分:0)
在宏的开头使用以下代码
Application.ScreenUpdating = False
并且在代码结束时不要忘记启用它
Application.ScreenUpdating = True
像
sub Test()
Application.ScreenUpdating = False
---- your code------
Application.ScreenUpdating = True
End Sub
答案 2 :(得分:0)
摆脱选择也会加速选择。
此外,由于您的Right命令仅提取一个字符,因此您的宏在十月,十一月和十二月的月份无法正常工作。
Dim tmpCell As Range
Application.ScreenUpdating = False
Set tmpCell = Cells.Find(What:="Month", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False).Offset(1, 0)
Do While tmpCell.Value <> ""
tmpCell.Value = MonthName(Right(tmpCell.Value, 1))
Set tmpCell = tmpCell.Offset(1, 0)
Loop
Application.ScreenUpdating = True
答案 3 :(得分:0)
所以最终,最快和最正确的版本:(相同的计算机在6s内150k行)
Sub edycja_miesiecy()
Application.ScreenUpdating = False
Dim tmpCell As Range
Set tmpCell = Cells.Find(What:="Month", After:=ActiveCell, LookIn:=xlFormulas, LookAt:= _
xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
, SearchFormat:=False).Offset(1, 0)
Do While tmpCell.Value <> ""
tmpCell.Value = month_name(tmpCell.Value)
Set tmpCell = tmpCell.Offset(1, 0)
Loop
Application.ScreenUpdating = True
End Sub
Public Function month_name(bufor As String) As String
If Left(Right(bufor, 2), 1) = "m" Then
bufor = Right(bufor, 1)
Else
bufor = Right(bufor, 2)
End If
If bufor = "1" Then
month_name = "January"
ElseIf bufor = "2" Then
month_name = "February"
ElseIf bufor = "3" Then
month_name = "March"
ElseIf bufor = "4" Then
month_name = "April"
ElseIf bufor = "5" Then
month_name = "May"
ElseIf bufor = "6" Then
month_name = "June"
ElseIf bufor = "7" Then
month_name = "July"
ElseIf bufor = "8" Then
month_name = "August"
ElseIf bufor = "9" Then
month_name = "September"
ElseIf bufor = "10" Then
month_name = "October"
ElseIf bufor = "11" Then
month_name = "November"
ElseIf bufor = "12" Then
month_name = "December"
Else
month_name = "#Error."
End If
End Function